Derived from this question : (Java) How does java do modulus calculations with negative numbers?
Anywhere to force PHP to return positive 51?
update
Looking for a configuration setting to fix, instead hard-guessing
Or other math function like bcmath?
updated
Not entire convinced by that java answer, as it does not take account of negative modulus
-13+(-64) =?
If GMP is available, you can use gmp_mod
Calculates n modulo d. The result is always non-negative, the sign of d is ignored.
Example:
echo gmp_strval(gmp_mod('-13', '64')); // 51
Note that n
and d
have to be GMP number resources or numeric strings. Anything else won't work¹
echo gmp_strval(gmp_mod(-13, 64));
echo gmp_mod(-13, 64);
will both return -51 instead (which is a bug).
¹ running the above in this codepad, will produce 51 in all three cases. It won't do that on my development machine.