I found out recently that my code is generating negative zeroes. And this is my very first time encountering this scenario since 3 years ago.
The code was something like this:
$x = "300.20";
$y = ("300.10" + "0.10" + "0.00") - "0.00";
$z = $x - $y;
The $x
returns string, $y
and $z
return float. If you run this codes, $z
's value is -5.6843418860808E-14, using number_format
it will become -0.00
.
Also, $y
's value is 300.20
but it is not equal to 300.20
when comparing. Exactly same value but not equal.
My solution is to convert $y
to string: $z = $x - strval($y);
but the question is why is this happening?
Same output in PHP5 and PHP7