# is the bankers' round() algorithm implemented well?

- Thread: https://externals.io/message/14698
- Original archive: https://news-web.php.net/php.internals/14698
- Started by Ron Korving on 2005-02-07T20:39:20+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #14698 — Ron Korving — 2005-02-07T20:39:20+00:00

URL: https://externals.io/message/14698#14698

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.

5 is rounded up for:

round(9.005, 2) gives 9.01
round(9.015, 2) gives 9.02
round(9.025, 2) gives 9.03
round(9.035, 2) gives 9.04
round(2047.005, 2) gives 2047.01
round(2047.015, 2) gives 2047.02
round(2047.025, 2) gives 2047.03
round(2047.035, 2) gives 2047.04

bankers' rounding:

round(2048.005, 2) gives 2048.01
round(2048.015, 2) gives 2048.01
round(30000.005, 2) gives 30000.01
round(30000.015, 2) gives 30000.01

This can't be intentional, can it? Someone tell me if this is a bug or a
feature please.

Ron

---

## #14700 — Sara Golemon — 2005-02-07T20:46:28+00:00

URL: https://externals.io/message/14698#14700 · In reply to: #14698

> 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

---

## #14702 — Ron Korving — 2005-02-07T21:04:46+00:00

URL: https://externals.io/message/14698#14702 · In reply to: #14700

I just did a long test from 1 - 10000 and I noticed that every time the
number was increased with one bit (starting with 2048, then 4096, then 8192)
behaviour changes. You were right, my bad.

Makes me wonder though, what's the point of a bankers' algorithm if .005 can
never be reached, only approached? :) This unpredictable rounding behaviour
is a real b*tch, but what can you do :)

Ron

"Sara Golemon" <pollita@php.net> wrote in message
news:20050207204629.63824.qmail@lists.php.net...
> > […]
algorithm.
> > […]
>
> 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

---

## #14704 — messju mohr — 2005-02-07T21:20:06+00:00

URL: https://externals.io/message/14698#14704 · In reply to: #14702

> I just did a long test from 1 - 10000 and I noticed that every time the
> number was increased with one bit (starting with 2048, then 4096, then 8192)
> behaviour changes. You were right, my bad.
> 
> Makes me wonder though, what's the point of a bankers' algorithm if .005 can
> never be reached, only approached? :) This unpredictable rounding behaviour
> is a real b*tch, but what can you do :)

you want to use integers or bcmath (http://php.net/bc) or gmp
(http://php.net/gmp) to get your own control of rounding and
accuracy. this makes this thread more appropiate for php-general than
for internals.

greetings
messju

> Ron
