Wednesday, 22 June 2016

rounding - Loss of significance php round




I have the following code




$suma += round(1*43067.8000,2);

$suma += round(-1*18875.1800,2);

$suma += round(-1*15293.9700,2);

$suma += round(-1*8898.6500,2);

print $suma;



The output is 3.6379788070917E-12



Why the answer is not 0 if the number is rounded before the addition?


Answer



Round still returns a float. Because it is a float there might always be very small errors. In this case, this causes your answer to differ from 0.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...