1

Can someone explain me, why in my code 65259.33026979 + (-65259.33026979) returns me -7.2759576141834E-12 instead zero?

$nx3 = 616;
$nx2 = 65349;
$dzN1 = 6746;
$dzN = 982;

$Qn = $nx3 / $dzN1;
$w2n2 = ($Qn * $dzN)*(-1);

$w3n2 = $nx2 + $w2n2;
$Qn2 = $w3n2 / $dzN1;

$w4n2 = ($Qn2 * $dzN1)*(-1);

$w5n2 = $w3n2 + $w4n2;

echo '$w3n2 = '.$w3n2.'<br />';
echo '$w4n2 = '.$w4n2.'<br />';
echo $w3n2.' + ('.$w4n2.') = '.$w5n2;

When i trying normally add this two values i getting correct answer.

  • 3
    This is due to a finite precision when doing floating point arithmetics. This is not limited to PHP, it affects many others languages using floating point datatypes. See [this website](http://floating-point-gui.de/) for more info. Also there is a [SO thread](http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples) for this. – Jirka Hrazdil Feb 15 '17 at 19:15
  • I solved the problem just by rounding the number to 10 decimal. I just wanted two numbers amounted to zero. Thank you for help :) Do i need to delete this post? – Patryk Banaś Feb 15 '17 at 20:47

0 Answers0