ftofix(3)
π 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 checkerrnoafter a call that overflowed, you will simply not know. the value got clamped anderrnogot set toERANGEand 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
- fedora 43: allegro-devel 4.4.3.1-18.fc43 Β· Giftware
- fedora 43: allegro-devel 4.4.3.1-18.fc43 Β· Giftware
- fedora 44: allegro-devel 4.4.3.1-20.fc44 Β· Giftware
- fedora 44: allegro-devel 4.4.3.1-20.fc44 Β· Giftware
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms