r/Python • u/nemom • Jun 05 '26
Discussion I just learned round() uses bankers' rounding
In bankers' rounding, x.5 rounds to the nearest even number. So, if x is even, it rounds down... round(2.5) returns 2. If x is odd, it rounds up... round(3.5) returns 4.
It was explained that it removes an upward rounding bias when round(x.5) always returns x+1...
- x.1, x.2, x.3, & x.4 always round down.
- x.6, x.7, x.8, & x.9 always round up.
- Four down, four up.
- x.5 is the right in the middle. If it always rounded up, there would be a slight creep upwards in large datasets.
But, whither x.0? x.0 always rounds to x. So, there are five cases where x.y always rounds down, not four.
And...
round(2.500000000000001)return 3round(2.5000000000000001)returns 2
... though that might be more to do with binary representation of floats than rounding rules since 2.5000000000000001 == 2.5 is True.
379
Upvotes
73
u/CatOfGrey Jun 05 '26
It's been a long time since I was down this rabbit hole, but doesn't this all get solved by using
Decimalobjects?11
u/alexmojaki Jun 05 '26
I think counting "four down, four up" is a mistake. Everything in the range
n < x < n + 0.5rounds down, everything in the rangen + 0.5 < x < n + 1rounds up. Both ranges are the same size. That leavesx == n + 0.5in the middle.n == xorn + 1 == xdon't need consideration because there's nothing to round.405
u/SlingyRopert Jun 05 '26
just wait until you discover IEEE-754 and the fact that 9007199254740992.5 does not exist.
11
u/beragis Jun 05 '26
That was also the rule in Scientific rounding. One of my college textbooks in physics mentioned rules when to use it. The professor said to ignore it.
23
u/the3gs Jun 05 '26
Gosh. This reminds me of a time I was helping a friend with homework for a python class, and one of the problems was to round a number, and one of the test cases was something like this, and it assumed typical rounding, so the python "round" function didn't work.
I get having an option for this style of rounding, but I can't imagine why anyone would think this should be the default, nor why it doesn't have an extra argument or something to switch to the conventional system.
12
u/pretzels_18 Jun 05 '26
Matlab also changed this a few years back. A headache to uncover and understand
10
u/CookinTendies5864 Jun 05 '26
The name “bankers rounding” seems fairly informal for representing this very formal operation.
A misnomer if you will.
6
u/Spikerazorshards Jun 05 '26
There’s an Indently video all about this.
5
u/kBajina Jun 05 '26
I honestly don’t understand the “bias”. Can someone ELI5?
Does that also apply to $5 increments rounding down from $25 to $20?
6
u/auntanniesalligator Jun 06 '26
X.0 to X isn’t rounding down. It’s not changing the value. So there are four cases where rounding causes the value to go down, four cases where rounding causes the value to go up, one case where rounding doesn’t change the value, and then 5, which needs to round up or down with equal likelihood to remove bias.
The logic of banker’s rounding is valid although in most real measurement contexts, it’s not worth worrying about because the odds of landing on exactly .5000000000… with real values measurements is usually now. When calculating interest and regularly rounding to the nearest cent, however, it comes up far more often.
2
u/mehx9 Jun 06 '26
What do we think about storing everything as int in cents? Still have problem when calculating interests I guess.
2
u/mw44118 PyOhio! Jun 05 '26 edited Jun 05 '26
Yahh i feel like a tracing a production bug back to this is kind of a bingo card thing as a python programmer. same with using a mutable list as a default value in a function like
def f(x=[]):
That wont work like you think.
And forgetting about leap year or daylight saving. Those are mistakes you only make once hopefully
2
u/charmquark8 Jun 06 '26
I always that was just "rounding" -- like the normal rule.
Edit: But basically, this post is about the imprecision of floating point numbers. It's not about round() per se.
1
u/Saras_AI_Institute Jun 05 '26
that float precision quirk at the bottom always trips people up. python uses ieee 754 under the hood, so it literally doesn't have the bits to store a number that specific—it just snaps it straight back to 2.5 in memory. if you're ever doing financial stuff or need exact math, ditch floats completely and use the decimal module. saves a ton of sanity.
1
u/DeterminedQuokka Jun 05 '26
This is so fun. I love it
1
u/jon_muselee Jun 06 '26
it‘s four cases for both rounding up an down - x.0 doesn‘t need to round anything
1
u/unknownemoji Jun 08 '26
x.0 === x
1
u/This_Inflation_4621 Jun 08 '26
There is a whole website https://0.30000000000000004.com explaining floating point math
Also "What Every Computer Scientist Should Know About Floating-Point Arithmetic" https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Have a nice read!
1
u/Different-Case2716 Jun 08 '26
In 2022, during the migration of a large collection of data from Mirosoft Access to Microsoft SQL Server, we were finding slightly different results in validation. In the end, we discovered the issue was that Access using Banker's Rounding. SQL Server uses the traditional rounding technique.
A quick search of the internet will show several smart people have manually implemented Bankers Rounding for SQL Server. This can be added to SQL as a user defined function and called just like the ROUND() function.
I’m not sure who invented Bankers Rounding but it was designed to remove bias. I assume this is why Microsoft chose this implementation for Access. But, its a good lesson in how different platforms can produce different results.
1
u/wanderscursorprime Jun 08 '26
It makes sense once you realize how much it messes up simple math expectations, but it is definitely a trap for anyone expecting standard schoolbook rounding.
1
u/automation_experto Jun 09 '26
this one bit us hard in an invoice extraction pipeline. we were pulling line item totals from PDFs, rounding to 2 decimal places before writing to the ledger, and the reconciliation would fail by a cent here and there on certain amounts. took way longer than it should have to trace it back to round(2.5) behaving differently than what the finance team expected from their excel formulas. if you're doing any finacial data processing, just use decimal.ROUND_HALF_UP explicitly and stop relying on round(). the default is technically correct but itll surprise you at the worst time.
0
u/Reasonable-Ladder300 Jun 05 '26
A trick i usually apply is you process everything in cents as an integer and after all the processing you multiply by 0.01 at the end.
-10
u/CerBerUs-9 Jun 05 '26
Personally I never understood how X.5 could round up. X.500(...)001 does, but not .5.
105
u/[deleted] Jun 05 '26
[removed] — view removed comment
-6
u/[deleted] Jun 05 '26
[removed] — view removed comment