I read here that:
Modulus with non integer numbers will give unpredictable results.
However, I tried to play a bit with it, and it seems to give pretty predictable results:
function mod($a, $b) {
echo "$a % $b = " . ($a % $b) . '<br>';
}
mod(10.3, 4);
mod(10.3, 2);
mod(-5.1, 3);
// OUTPUT:
// 10.3 % 4 = 2
// 10.3 % 2 = 0
// -5.1 % 3 = -2
In other words, the double seems to be converted to integer first.
Is there any definition of how % works when the first operand is double?
use:
fmod(10.3, 4)
I too had to do the same thing but I noticed doubles are converted to int and using fmod returns a double.
If this helps
From http://php.net/manual/en/language.operators.arithmetic.php
The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With