Jump to content

Number addition


juhasamy

Recommended Posts

$x = -0.2;
$x += 0.1; print $x . '<br>';
$x += 0.1; print $x . '<br>';
$x += 0.1; print $x . '<br>';

=>

-0.1
0
0.1

 

 

$x = -0.3;
$x += 0.1; print $x . '<br>';
$x += 0.1; print $x . '<br>';
$x += 0.1; print $x . '<br>';
$x += 0.1; print $x . '<br>';

=>

-0.2
-0.1
2.77555756156E-17
0.1

 

???

Link to comment
https://forums.phpfreaks.com/topic/77481-number-addition/
Share on other sites

You're dealing with the classic problem of converting a floating point number from the internal representation in binary to decimal. The value "2.77555756156E-17" is really zero for all pratical purposes.

 

It's not a bug in PHP, it's just how computers work...

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/77481-number-addition/#findComment-392245
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.