fixsub(3)

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

✨ fixsub(3) ✨

okay bestie, gather round, because this is a page from the Allegro manual and it's about a little function called fixsub πŸ’– it's the safe way to subtract fixed point numbers, and it clamps underflow instead of letting everything go feral. it's giving "responsible subtraction" energy and honestly? we love that for us 🫢


🌸 NAME 🌸

fixsub β€” the safe function to subtract fixed point numbers, clamping underflow. part of the Allegro game programming library πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <allegro.h>

fixed fixsub(fixed x, fixed y);

πŸ’– DESCRIPTION πŸ’–

so here's the tea, bestie β˜• fixed point numbers can be subtracted with the normal - integer operator, but the way that gives you zero protection against overflow? not iconic. not even a little bit. 😭

if overflow is a problem for you β€” and babes, it usually is β€” you should use this function instead. periodt.

it's slower than using integer operators, ngl, that's the tradeoff. but if an overflow happens, it will set errno and clamp the result, rather than just letting it wrap and ruining your whole day. no cap. ✨

example:

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 that last line failing on you. it literally tells you it will fail. the audacity. the honesty. i'm obsessed.

πŸ‘‘ RETURN VALUE πŸ‘‘

returns the clamped result of subtracting y from x, and it sets errno to ERANGE if there was an overflow πŸ¦‹


🌈 SEE ALSO 🌈


and that's the whole page, bestie πŸŽ€ shoutout to Allegro, version 4.4.3 β€” this little fixsub(3) moment came straight from that source, so go be safe with your subtractions and don't let anything wrap on you πŸ’œπŸ§šβ€β™€οΈ

πŸ“¦ where this page comes from