|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54985 round doesn't work with non-14 precision
Submitted: 2011-06-03 16:10 UTC Modified: 2011-06-11 17:01 UTC
From: jille at hexon dot cx Assigned: cataphract (profile)
Status: Not a bug Package: *Math Functions
PHP Version: 5.3.6 OS: n/a
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
34 - 9 = ?
Subscribe to this entry?

 [2011-06-03 16:10 UTC] jille at hexon dot cx
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:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-10 20:01 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: cataphract
 [2011-06-10 20:10 UTC]
Seems to have been introduced by the fix to bug #52550 (r301991).
 [2011-06-11 04:00 UTC]
-Status: Assigned +Status: Bogus
 [2011-06-11 04:00 UTC]
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-11 15:20 UTC] 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 16:09 UTC]
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 16:48 UTC]
You can write your own little string rounding wrapper. If you are rounding to 3 
decimal places, set your precision accordingly.
 [2011-06-11 17:01 UTC] jille at hexon dot cx
Or I could just use number_format(). Stupid I didn't think of that earlier.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 24 06:01:30 2024 UTC