I was trying to validate if the first floating number could be fully divided by the second floating number and I got something like these:
$first = 10.20; // or 5.10, 7.14, 9.18
$second = 1.02;
var_dump(fmod($first, $second));
// output is showing
float(1.02)
But if I try to divide below these numbers by 1.02 fmod()
works nicely.I don't understand whyfmod()
behaving like this? Any answer to this question will help me to understand the fact.
$first = 4.08; // or 2.04, 3.06, 6.12, 8.16
$second = 1.02;
var_dump(fmod($first, $second));
// output is showing
float(0)
It is because the fmod()
function only returns the floating point modulos of the division made.
As in your first case the remainder is a floating point value it is returned by the function.
But in the below examples if we make divisions as
$first = 4.08; // or 2.04, 3.06, 6.12, 8.16
$second = 1.02;
We get the remainder as of type integer which is 0.041616.The problem here is the values shown by the function are limited only till the first decimal(in this case it is 0.0
).But if we had to find the modulos of two numbers as
<?php
$x = 5.7;
$y = 1.3;
$r = fmod($x, $y);
?>
We will get the values as float(0.5).
Because the here the first digit after decimal point is not 0.
And hence in your case it is shown float(0)
by the function.
For more details you can visit Click Here