#45978 [Opn->Bgs]: round() errors.
| From: | mattwil@php.net | Date: | Wed, 03 Sep 2008 05:16:46 +0000 |
| Subject: | #45978 [Opn->Bgs]: round() errors. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-128630@lists.php.net to get a copy of this message | ||
ID: 45978
Updated by: mattwil@php.net
Reported By: firealwaysworks at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Math related
Operating System: Linux
PHP Version: 5.2.6
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.
The result of 21.33*1.015 is stored as slightly less than 21.64995
(21.649949999999997 on my Windows system), different than when you write
the literal static value. The result of 21.33*1.015 when printing it
(happens differently than round() function, so results may vary) or
rounding 5-14 decimal places, those extra internal 9s are "rolled over."
Previous Comments:
------------------------------------------------------------------------
[2008-09-03 02:09:39] firealwaysworks at gmail dot com
Description:
------------
In this example the multiplication of two floats yields a number with 5
significant figures. The round() function incorrectly handles this
number only when it is working with the answer to a mathematical
operation. It is important to note that the round() function is working
properly for static values.
I found this because I am writing financial software in PHP. I have
absolutely no doubt that this bug would cause our company to loose
money.
Reproduce code:
---------------
print 21.33*1.015."<br>";
print round(21.33*1.015,2)."<br>";
print round(21.33*1.015,4)."<br>";
print round(21.64995,4);
Expected result:
----------------
21.64995
21.65
21.65
21.65
Actual result:
--------------
21.64995
21.65
21.6499
21.65
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45978&edit=1