3 ms·
I've been using this style, in TFA as ((a % b) + b) % b It feels like there should be a nicer way (apart from the conditional one), but I can't think of it.
by hyperthesis 4y ago
I've been using this style, in TFA as
((a % b) + b) % b
It feels like there should be a nicer way (apart from the conditional one), but I can't think of it. Can you prove there isn't one?
- layer8 4y agoJust as a caveat, ((a % b) + b) can overflow. For example, with 32-bit signed integer arithmetic, mod(1000000000, 2000000000) using that formula will yield -1294967296 instead of 1000000000.
- klabb3 4y agoIt may also be expensive, since % has the cost of division, and there are two of them..