|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62959 Wrong math
Submitted: 2012-08-28 17:50 UTC Modified: 2012-08-31 04:41 UTC
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: lauris dot kuznecovs at laurisnet dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.4.6 OS: Windows 7, Centos 5.8
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.
Block user comment
Status: Assign to:
Bug Type:
From: lauris dot kuznecovs at laurisnet dot com
New email:
PHP Version: OS:


 [2012-08-28 17:50 UTC] lauris dot kuznecovs at laurisnet dot com
<? echo (443.292 - 193.126 - 250.166); ?>
Return: -2.8421709430404e-14

<? echo round(443.292 - 193.126 - 250.166, 2); ?>
Return: -0

But these calculations must return 0.
BTW -0 does not exist in MATH. Maybe some error in rounding function.

Test script:
<? echo (443.292 - 193.126 - 250.166); ?>
<? echo round(443.292 - 193.126 - 250.166, 2); ?>

Expected result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-28 17:57 UTC]
-0 may not exist in traditional math, but it very much exists in computer 
representations. Computers can't represent floating point values with complete 
accuracy, so if you manipulate floating point you are going to get rounding 
errors. -0 means a value rounded to 0 from the negative direction. You can read for more details.
 [2012-08-28 17:57 UTC]
-Status: Open +Status: Not a bug
 [2012-08-28 18:11 UTC] lauris dot kuznecovs at laurisnet dot com
Ok, lets say that the round() function works as expected, but still this 
calculation (443.292 - 193.126 - 250.166) return: -2.8421709430404 E-14
And that is not correct!
 [2012-08-28 18:19 UTC]
Depends on your definition of correct. Like I said, computers cannot represent 
floating point with perfect accuracy. -2.8421709430404 E-14 is an extremely 
small number very close to 0, so it is as close to "correct" as you are going to 
You should probably read through this to get up to 
speed on how computers treat floating point values. This isn't a PHP-specific 
thing. For example:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (443.292 - 193.126 - 250.166)
 [2012-08-28 19:00 UTC]
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:
 [2012-08-28 19:09 UTC]
Oh, sorry, I missed rasmus second reply ;)
 [2012-08-31 04:41 UTC] lauris dot kuznecovs at laurisnet dot com
I think this can be closed. No more questions.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Sep 22 23:01:24 2023 UTC