#42333 [Opn->Bgs]: double precision error
ID: 42333
Updated by: jani@php.net
Reported By: boydell at orbsix dot com
-Status: Open
+Status: Bogus
Bug Type: Math related
Operating System: WinXP, Linux
PHP Version: 5.2.3
New Comment:
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
Previous Comments:
------------------------------------------------------------------------
[2007-08-18 00:14:20] boydell at orbsix dot com
Description:
------------
The math on the following should be zero.
echo (65 + 50 - 50 + 15 - 15 + 100 - 100 + 50 + 16.50 - 36.50 + 88.21 -
38.21 - 145);
but we get: -2.84217094304E-14
Reproduce code:
---------------
$list = array(65, 50, -50, 15, -15, 100, -100, 50, 16.50, -36.50,
88.21, -38.21, -145);
$current = 0;
foreach($list as $number) {
echo $current . "+" . $number . "=";
$current += $number;
echo $current . "\n<br>";
}
Expected result:
----------------
0+65=65
65+50=115
115+-50=65
65+15=80
80+-15=65
65+100=165
165+-100=65
65+50=115
115+16.5=131.5
131.5+-36.5=95
95+88.21=183.21
183.21+-38.21=145
145+-145=0
Actual result:
--------------
0+65=65
65+50=115
115+-50=65
65+15=80
80+-15=65
65+100=165
165+-100=65
65+50=115
115+16.5=131.5
131.5+-36.5=95
95+88.21=183.21
183.21+-38.21=145
145+-145=-2.8421709430404E-14
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42333&edit=1
Thread (2 messages)