How Bankers Rounding Legacy should Work?

Here’s a side-by-side diff of the two algorithms for “Banker’s Rounding (legacy)” and “Banker’s Rounding” in version-15 of the Frappe Framework:

Banker’s Rounding (legacy) Banker’s Rounding
Slightly favors rounding up; invoices and returns can be off by a cent. More balanced and consistent; invoices and returns cancel better, but some totals may be a cent lower than before in rare “.5” cases.
Rounds “.5” up for positive values, and toward zero for negative values. That means invoices tend to round a little higher, and returns tend to round a little less negative. As a result, an invoice and its return may not perfectly cancel out in cents. Rounds “.5” to the nearest even last digit, for both positive and negative values. This is more symmetric, so a return is much more likely to be the exact negative of the invoice when the raw numbers match.
10.005 rounded to two decimals: 10.01 10.005 rounded to two decimals: 10.00
-10.005 rounded to two decimals: -10.00 -10.005 rounded to two decimals: -10.00

The epsilon check makes the new rounding treat values that are very close to .5 as if they were exactly .5, which is important because binary floating‑point numbers often can’t represent .5 boundaries precisely after calculations. Without epsilon, some values that should be treated as half might be rounded the “wrong” way.

A more technical explanation from Ankushs PR:

Epsilon is small correctional value added to correctly round numbers which can’t be represented in IEEE 754 representation.

In simplified terms, the representation optimizes for absolute errors in representation so if a number is not representable it might be represented by a value ever so slighly smaller than the value itself. This becomes a problem when breaking ties for numbers ending with 5 when it’s represented by a smaller number. By adding a very small value close to what’s “least count” or smallest representable difference in the scale we force the number to be bigger than actual value, this increases representation error but removes rounding error.

References:

1 Like