Re: Bug #984 Updated: round() works bad
| From: | Tom May | Date: | Thu, 10 Dec 1998 17:31:35 +0000 |
| Subject: | Re: Bug #984 Updated: round() works bad | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-2749@lists.php.net to get a copy of this message | ||
Bug Database <php-dev@lists.php.net> writes:
> ID: 984
> Updated by: zeev
> Reported By: segi@active.ch
> Status: Closed
> Bug Type: Misbehaving function
> Assigned To:
> Comments:
>
> This has to do with the fact that floating point numbers
> have limited accuracy.
>
> 0.5 is never represented as 0.5000000... with zeros all the way
> to infinity, but usually as something like
> 0.49999999999999999998
>
> 1.5 may be represented as 1.50000000000002, etc.
>
> Simply, you cannot rely on 0.5 being accurate, and thus
> the behavir of round() can be considered undefined.
>
> Full Bug description available at:
> http://ca.php.net/bugs.php3?id=984
Power-of-two fractions such as 0.5 *are* represented exactly in
floating point as long as there is enough precision, and there is
certainly enough to represent say 1.5 which only requires 2 bits.
At the risk of spreading further disinformation, I think round() is
more likely using a convention where 0.5 will be always be rounded to
an even whole number. Since both rounding 0.5 up and rounding it down
are equally valid, this rule meanes 50% of the time it is rounded up
and 50% of the time it is rounded down so any nasty numeric effects
might balance each other out somewhat instead of accumulating.
Tom.
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net