fixdiv(3alleg4)

fixdiv πŸ’–

okay so bestie, welcome to the Allegro manual β€” this little cutie lives in the Allegro game programming library, and today we're talking about fixed point division. yes. division. for fixed points. it's giving math but make it gamer ✨


✨ NAME ✨

fixdiv β€” Fixed point division. Allegro game programming library. πŸŽ€


🌸 SYNOPSIS 🌸

#include <allegro.h>

fixed fixdiv(fixed x, fixed y);

πŸ’… DESCRIPTION πŸ’…

okay so here's the tea πŸ«– β€” if you wanna divide a fixed point value by a regular integer, you can just use the normal / operator, no big deal, she's easy like that.

BUT. if you need to divide two fixed point values by each other? bestie you cannot just use /. you gotta call this function. this one. right here. fixdiv(). periodt. πŸ’…

and now the scary part 😭 β€” if a division by zero happens, errno gets set and the maximum possible value gets returned. but here's the sneaky lil detail that gets people every time: errno is not cleared if the operation is successful. so if you're planning to test for division by zero, you better set errno=0 before you call fixdiv(), or you're gonna be out here debugging a ghost πŸ‘» fr fr.

Example time, slay:

fixed result;
/* This will put 0.06060 `result'. */
result = fixdiv(itofix(2), itofix(33));
/* This will put 0 into `result'. */
result = fixdiv(0, itofix(-30));
/* Sets `errno' and puts -32768 into `result'. */
result = fixdiv(itofix(-100), itofix(0));
ASSERT(!errno); /* This will fail. */
πŸ’€ not the ASSERT failing... yeah bestie that last line will fail, that's the whole point of the example. iykyk.

πŸ‘‘ RETURN VALUE πŸ‘‘

she returns the result of dividing x by y. simple. iconic. 🌟

but if y is zero? 🚨 she returns the maximum possible fixed point value and sets errno to ERANGE. so like, she tried her best, but dividing by zero is not it. πŸ™ˆ


πŸ¦‹ SEE ALSO πŸ¦‹


and that's the whole vibe, bestie β€” this page came to you straight from Allegro, version 4.4.3, dated the same as always, living under the name fixdiv(3alleg4). go forth and divide responsibly πŸ’–βœ¨