array_sum PHP错误的结果[重复]

This question already has an answer here:

the result of this should be zero!

echo array_sum([-61.50,50.00,10.50,1.00,0.00,50.00,-16.73,-20.00,-55.75,42.48]);

Why is giving -7.105427357601E-15?

</div>

Just try round(), you will get the same result.

echo round(array_sum([-61.50,50.00,10.50,1.00,0.00,50.00,-16.73,-20.00,-55.75,42.48]));

Because floating point values (which you have here when you use decimals) are not exact. They're approximations.

The error in that approximation comes out to -7.105427357601E-15 when summing these values.

It's because of floats. If you want to calculate something with precision 2 (for this example) you should use something like this:

$el = [-61.50,50.00,10.50,1.00,0.00,50.00,-16.73,-20.00,-55.75,42.48];
$sum = 0;
foreach ($el as $e) {
    $sum += $e * 100;
}

echo $sum / 100;

You should never trust to float values. Another example from Javascript (Google developer Console):

enter image description here