C library functions are always like: "SYNOPSIS. This function converts foos into bars depending on the user locale. ARGUMENTS. src and dest pointers must be distinct; it is undefined behavior if they are not QPU-aligned. RETURN VALUE. Returns the number of foos converted. A zero value indicates failure, or that zero foos were converted. A negative value indicates that the final foo was only partially converted (function got tired). Check this global variable to find out why."
@typeswitch Actual comment from a C library I'm working on.

I'm not a C programmer usually, but it seems I've fallen into the same trap.

EDIT: Alt-text
@Ollie @typeswitch I don't entirely get what you're saying but I hope it's not smelly in terms of strict aliasing.
@lizzy @typeswitch It was a toy project to analyze random floating point values for their binary data.

It was mostly to learn how the data type worked. I even had a bit of the code that could (albeitly naively) determine the largest value the fractional part of the number could still be accurately represented.
@lizzy @typeswitch so like if you had a number like 0.1 it would be able to tell you the largest floating point number that could still handle a precision in the tenths place.
@lizzy @typeswitch it is very terrible code, with separate function paths to handle 32-bit and 64-bit floats.

I know there has to be a better way to handle that without hard-coding separate functions for separate types in C but I couldn't be bothered for something that would never be used in production and as someone who knows very little C.
@lizzy @typeswitch

github.com/Ollie-Branch/float-shredder/tree/809681101ecbdf735ba69530cfade5ce1f82268e

Why am I trying to describe how bad it is when you could just read the code lol.
GitHub - Ollie-Branch/float-shredder at 809681101ecbdf735ba69530cfade5ce1f82268e

[WIP] A C/C++ header only library to eviscerate your floats in novel and grotesque ways - GitHub - Ollie-Branch/float-shredder at 809681101ecbdf735ba69530cfade5ce1f82268e

GitHub

@Ollie @typeswitch yea, this breaks static aliasing pretty badly - it’s undefined behavior. instead what you’re allowed to do is cast to char / unsigned char or simply memcpy it (recommended here):

uint32_t output; memcpy(&output, &input_float, sizeof(input_float)); return output;
@Ollie @typeswitch the thing about memcpy is that it conceptually accesses both source and destination as unsigned char array, which makes it legal