ftofix(3)

πŸ“¦ version 4.4.3.1, in fedora 43, fedora 44

πŸ’– ftofix(3) πŸ’–

okay bestie, gather round, because today we're talking about ftofix from the Allegro game programming library β€” she converts a floating point value to fixed point and she does it safely, which is honestly so much more than some other functions can say. πŸŽ€


✨ SYNOPSIS ✨

#include <allegro.h>

fixed ftofix(double x);

like, that's it. one function, one double, one fixed coming back at you. iconic. πŸ’…


🌸 DESCRIPTION 🌸

So ftofix() takes a floating point value and converts it to fixed point. simple. slay. ✨

But here's the part where she diverges from her cousin itofix(): unlike itofix(), this function clamps values that could overflow the type conversion. so instead of just letting chaos happen, she catches it, and while she's doing that she sets errno to ERANGE. no cap, that's the whole bit. 🫢

Here's the example from the page, verbatim, because it's giving very much "this will fail and that's the point":

fixed number;
number = itofix(-40000);
ASSERT(fixfloor(number) == -32768);
number = itofix(64000);
ASSERT(fixfloor(number) == 32767);
ASSERT(!errno); /* This will fail. */

yes bestie, that last assert is supposed to fail. it's a demonstration, not a bug. read it again. 😭


πŸ¦‹ RETURN VALUE πŸ¦‹

She returns the floating point value converted to fixed point, clamping any overflows as she goes β€” and yes, she's setting errno while she's at it. so check it. or don't, but then don't come crying to me. πŸ™ˆ

⚠️ lowkey important: if you don't check errno after a call that overflowed, you will simply not know. the value got clamped and errno got set to ERANGE and that info is just... sitting there. waiting. πŸ‘‘

πŸ’œ SEE ALSO πŸ’œ

go read those too, i'm literally begging. they're all in their own little manpage era and they deserve the attention. 🧚✨


Allegro, version 4.4.3 β€” mwah, that's the whole page, bestie. thanks for coming to my ted talk, now go clamp some floats responsibly. πŸ’–πŸŒˆβ­

πŸ“¦ where this page comes from