Re: is the bankers' round() algorithm implemented well?
| From: | Sara Golemon | Date: | Mon, 07 Feb 2005 20:46:28 +0000 |
| Subject: | Re: is the bankers' round() algorithm implemented well? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-14700@lists.php.net to get a copy of this message | ||
> I understood that round() is using the so called Bankers' round algorithm.
> But I notice it's inconsistent. The banker's algorithm is magically
> activated only for numbers beyond 2047.0. Before 2047, the "classic"
> 5-is-rounded-up method is used.
>
Floating point numbers are........ imprecise.
What you see as 2047.005, the computer might see as
2047.0049999999999999123869128 or perhaps as 2047.005000000000000012396291
When I only care about a certain accuracy (such as with monetary amounts) I
'solve' this quirk of the CPU by adding a miniscule fudge factor:
round($someval + 0.000000001, 2)
PHP doesn't do this automatically because sometimes a plus-fudge is
appropriate, sometimes a negative-fudge is better.
-Sara