Bug #54985 [Bgs]: round doesn't work with non-14 precision
Edit report at http://bugs.php.net/bug.php?id=54985&edit=1
ID: 54985
Updated by: rasmus@php.net
Reported by: jille at hexon dot cx
Summary: round doesn't work with non-14 precision
Status: Bogus
Type: Bug
Package: *Math Functions
Operating System: n/a
PHP Version: 5.3.6
Assigned To: cataphract
Block user comment: N
Private report: N
New Comment:
You can write your own little string rounding wrapper. If you are rounding to 3
decimal places, set your precision accordingly.
Previous Comments:
------------------------------------------------------------------------
[2011-06-11 16:09:41] cataphract@php.net
It does give the correct result; it gives the nearest double number to a multiple of 0.01. The
problem is you are showing digits beyond the number of digits guaranteed to be correct (i.e. not
affected by rounding error). For doubles, with an effective precision of 53 bits, the (possibly
non-integer) number of digits to the right of the decimal point that is guaranteed to be correct is
given by 53*log10(2) - log10(abs(x)); for x=0.055, this is around 17.21. So for this number you must
not set 'precision' beyond 17, otherwise you might see garbage.
------------------------------------------------------------------------
[2011-06-11 15:20:23] jille at hexon dot cx
I disagree with that this behaviour is correct. I think there should be a
function which can round() in a reliable way. If it isn't possible with floats I
think there should be a function which returns a string.
------------------------------------------------------------------------
[2011-06-11 04:00:55] cataphract@php.net
This is a bogus report (.055 is not exactly representable, the closest is 7926335344172073*2^-57);
r301991 did introduce a bug, but it's completely unrelated.
------------------------------------------------------------------------
[2011-06-10 20:10:39] cataphract@php.net
Seems to have been introduced by the fix to bug #52550 (r301991).
------------------------------------------------------------------------
[2011-06-03 16:10:14] jille at hexon dot cx
Description:
------------
Round() doesn't work right when the precision is set to e.g. 16.
The last comment in #51701 also makes notice of this. (However it seems unrelated to that
bugreport.)
Bug #5500 states that you shouldn't set the precision higher than the data-type can hold. If
that's still true I think we should add a warning when setting the precision too high instead
of just accepting it and trying to work it out.
_php_math_round in ext/standard/math.c cites:
if ((precision_places = php_intlog10abs(value)) > 0) {
precision_places = 14 - php_intlog10abs(value);
} else {
precision_places = 14;
}
and I guess 14 shouldn't be hardcoded there.
Test script:
---------------
php > ini_set('precision', 30);
php > var_dump(round((5.5/100), 3));
Expected result:
----------------
float(0.055)
Actual result:
--------------
float(0.0550000000000000002775557561563)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54985&edit=1
Thread (8 messages)