fixsub(3alleg4)

πŸ’– fixsub πŸ’–

okay so bestie, welcome to the Allegro manual β€” yes, that Allegro, the game programming library, and today's main character is a lil function called fixsub. she's here to do subtraction, but safely. 🫢


✨ NAME ✨

fixsub β€” the safe function to subtract fixed point numbers, and she clamps underflow instead of letting everything fall apart. literally the responsible friend of the group chat. Allegro game programming library, obvs. πŸ’…


🌸 SYNOPSIS 🌸

#include <allegro.h>

fixed fixsub(fixed x, fixed y);

πŸ¦‹ DESCRIPTION πŸ¦‹

so like, yeah, fixed point numbers can be subtracted with the normal '-' integer operator. no cap. but here's the tea: that gives you zero protection against overflow. 😭

if overflow is a problem for you, you should use this function instead. it's slower than using integer operators, but if an overflow occurs it will set errno and clamp the result, rather than just letting it wrap. she catches you when you fall, periodt. πŸ‘‘

example time, look at this:

fixed result;
/* This will put 4965 into `result'. */
result = fixsub(itofix(5000), itofix(35));
/* Sets `errno' and puts -32768 into `result'. */
result = fixsub(itofix(-31000), itofix(3000));
ASSERT(!errno); /* This will fail. */

not me pointing out that last line 😭 the ASSERT(!errno) will fail, bestie, because errno is set. iykyk.


⭐ RETURN VALUE ⭐

returns the clamped result of subtracting y from x, and sets errno to ERANGE if there was an overflow. iconic behaviour honestly. πŸ’–


🌈 SEE ALSO 🌈

go read those too, they're her besties 🫢


okay that's the whole page!! this one came from Allegro, version 4.4.3 β€” so when you're out here clamping your fixed point numbers, just remember who wrote the manual for you. mwah πŸ’‹βœ¨πŸŽ€