#40924 [Csd->Bgs]: round() precision problem
| From: | tony2001@php.net | Date: | Wed, 28 Mar 2007 13:43:12 +0000 |
| Subject: | #40924 [Csd->Bgs]: round() precision problem | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-110882@lists.php.net to get a copy of this message | ||
ID: 40924
Updated by: tony2001@php.net
Reported By: info at e-mhd dot com
-Status: Closed
+Status: Bogus
Bug Type: Performance problem
Operating System: Win & Linux
PHP Version: 5.2.1
Previous Comments:
------------------------------------------------------------------------
[2007-03-28 13:35:42] info at e-mhd dot com
Thank you for your reply.
------------------------------------------------------------------------
[2007-03-26 17:11:02] johannes@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.
.
------------------------------------------------------------------------
[2007-03-26 16:52:36] info at e-mhd dot com
Description:
------------
Problem with function round()
rounded value is incorrect
Reproduce code:
---------------
ini_set("precision", 15);
bcscale(5);
$a1 = (string)(3845349 * 34.065);
$a2 = (float)(3845349 * 34.065);
$a3 = bcmul(3845349, 34.065);
echo "A1 = ";
var_dump($a1);
echo("<br />");
echo "A2 = ";
var_dump($a2);
echo("<br />");
echo "A3 = ";
var_dump($a3);
echo "<br />Values after round() :";
echo "<br />A1 = ".round( $a1 ,2);
echo "<br />A2 = ".round( $a2 ,2);
echo "<br />A3 = ".round( $a3 ,2);
Expected result:
----------------
A1 = string(13) "130991813.685"
A2 = float(130991813.685)
A3 = string(13) "130991813.685"
Values after round() :
A1 = 130991813.69
A2 = 130991813.69
A3 = 130991813.69
Actual result:
--------------
A1 = string(13) "130991813.685"
A2 = float(130991813.685)
A3 = string(13) "130991813.685"
Values after round() :
A1 = 130991813.69
A2 = 130991813.68
A3 = 130991813.69
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40924&edit=1