Bug #54479 [Bgs]: round(1e15+0.1) returns 1e15+0.1 instead of 1e15
| From: | cataphract@php.net | Date: | Thu, 07 Apr 2011 12:19:07 +0000 |
| Subject: | Bug #54479 [Bgs]: round(1e15+0.1) returns 1e15+0.1 instead of 1e15 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-159992@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54479&edit=1
ID: 54479
Updated by: cataphract@php.net
Reported by: for-bugs at hnw dot jp
Summary: round(1e15+0.1) returns 1e15+0.1 instead of 1e15
Status: Bogus
Type: Bug
Package: Math related
Operating System: any
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
If anything, the cutoff value is too high:
The accuracy (effective number of digits to the right of the decimal
point) of a double is given by 15.9546-log10(|x|) and:
In[22]:= Solve[$MachinePrecision-Log[10, Abs[x]]==1.,x]
14
Out[22]= {{x -> 9.0072 10 }}
Previous Comments:
------------------------------------------------------------------------
[2011-04-07 01:52:10] for-bugs at hnw dot jp
My test script seems to be wrong. How about next one?
<?php
ini_set("precision",19);
var_dump(1000000000000000.125);
// float(1000000000000000.125)
var_dump(round(1000000000000000.125));
// PHP 5.3.6 returns float(1000000000000000.125)
// PHP 5.2.17 returns float(1000000000000000)
IEEE 754 double precision have 53 bit fractions, so 1000000000000000 and
1000000000000000.125 is exact number.
This behavior is caused by ext/standard/math.c:160-163
/* This value is beyond our precision, so rounding it is pointless
*/
if (fabs(tmp_value) >= 1e15) {
return value;
}
------------------------------------------------------------------------
[2011-04-06 19:39:39] derick@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://www.floating-point-gui.de/
Thank you for your interest in PHP.
.
------------------------------------------------------------------------
[2011-04-06 19:22:45] for-bugs at hnw dot jp
Description:
------------
When round() is called with 1 argument which value is between 1e15 and
2^53, round() returns non-rounded value even if the value has fractional
part.
For instance, though 1e15 is exact number as IEEE 754 double precision,
round(1e15+0.1) returns 1e15+0.1. I think 1e15 is better result.
Test script:
---------------
<?php
'ini_set("precision",18);
var_dump(round(1000000000000000.1));
Expected result:
----------------
float(1000000000000000)
Actual result:
--------------
float(1000000000000000.12)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54479&edit=1