3 ms·
Just as a caveat, ((a % b) + b) can overflow. For example, with 32-bit signed integer arithmetic, mod(1000000000, 2000000000) using that formula will yield -129
by layer8 4y ago
Just 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..