php.net |  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jille at hexon dot cx
New email:
PHP Version: OS:

 

 [2011-06-03 16:10 UTC] 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)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-10 20:01 UTC] cataphract@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: cataphract
 [2011-06-10 20:10 UTC] cataphract@php.net
Seems to have been introduced by the fix to bug #52550 (r301991).
 [2011-06-11 04:00 UTC] cataphract@php.net
-Status: Assigned +Status: Bogus
 [2011-06-11 04:00 UTC] 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-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] 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 16:48 UTC] rasmus@php.net
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-2021 The PHP Group
All rights reserved.
Last updated: Tue Jan 19 22:01:23 2021 UTC