PHP 4.0 Bug #8164 Updated: weird subtraction result
| From: | lyric@php.net | Date: | Sat, 09 Dec 2000 07:15:32 +0000 |
| Subject: | PHP 4.0 Bug #8164 Updated: weird subtraction result | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-40639@lists.php.net to get a copy of this message | ||
ID: 8164
Updated by: lyric
Reported By: c.becker@provi.de
Old-Status: Open
Status: Closed
Bug Type: Math related
Assigned To:
Comments:
This is not a PHP bug, it's the age old issue of the accuracy of a floating point number. Since
a computer cannot store a number *precisely*, it stores as close as it can get. When you do math
operations on such a number, the difference (aka rounding error) between what you entered and what
the computer stored is exagerated. See http://www.dcs.ed.ac.uk/home/mhe/plume/node9.html
for a bit of an explanation.
Try using printf( "%.1f", $result );
Previous Comments:
---------------------------------------------------------------------------
[2000-12-08 11:49:31] c.becker@provi.de
this won't work, either.
$one = 555.6;
$two = 555.5;
$result = $one - $two;
echo $result;
---------------------------------------------------------------------------
[2000-12-08 06:23:13] c.becker@provi.de
I don't know, if this is a Math related problem,
it is a simple subtraction problem.
If I type this
$one = "555.6";
$two = "555.5";
$result = $one - $two;
echo $result;
the result in my browser is
0.10000000000002
This occurs with more numbers than above.
So, what is my problem?
---------------------------------------------------------------------------
Full Bug description available at: http://bugs.php.net/?id=8164