Bug #54985 [Bgs]: round doesn't work with non-14 precision

From: Date: Sat, 11 Jun 2011 15:01:31 +0000
Subject: Bug #54985 [Bgs]: round doesn't work with non-14 precision
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-161312@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54985&edit=1

 ID:                 54985
 User updated by:    jille at hexon dot cx
 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:

Or I could just use number_format(). Stupid I didn't think of that earlier.


Previous Comments:
------------------------------------------------------------------------
[2011-06-11 16:48:37] 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 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).

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    http://bugs.php.net/bug.php?id=54985


-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54985&edit=1


Thread (8 messages)

« previous php.bugs (#161312) next »