php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42333 double precision error
Submitted: 2007-08-18 00:14 UTC Modified: 2007-08-18 00:32 UTC
From: boydell at orbsix dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.3 OS: WinXP, Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: boydell at orbsix dot com
New email:
PHP Version: OS:

 

 [2007-08-18 00:14 UTC] 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 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-18 00:32 UTC] jani@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 02:01:30 2024 UTC