Python

Python 3 Rounding Surprise

I discovered this subtle change when one of my unit tests mysteriously failed once I ported it from Python 2 to Python 3. A computed price was consistently one microdollar higher in the Python 3 version.

I eventually tracked it down to a change in rounding behaviour between Python 2 and 3. According to the docs (emphasis mine):

if two multiples are equally close, rounding is done toward the even choice

I proved it to myself thusly:

$ python2 -c 'print( round(8.5) )'
9.0
$ python3 -c 'print( round(8.5) )'
8

This method of rounding is called “banker’s rounding.” The good news is that banker’s rounding better suits our need for a fair rounding method. That is: our goal in rounding is to have rounding errors even out in aggregate, and banker’s rounding achieves that better than always rounding up (i.e., away from zero).

Revert to Python 2 Behaviour

Should you find the need to use Python 2’s rounding algorithm, it’s available as part of the Decimal module as decimal.ROUND_HALF_UP (example).

References

Ron

https://www.ronrothman.com/public/about+me.shtml

Share
Published by
Ron
Tags: python

Recent Posts

Python 3 Exception Chaining

Exception chaining solves two problems in Python 2. 1. Swallowed Exceptions If, while handling exception…

4 years ago

Safe Password Storage – How Websites Get It Wrong

Here's the recording of my talk "15 minutes w/ Beeswax: Safe Password Storage - How…

4 years ago

Python at Scale: Concurrency at Beeswax

My presentation from the NYC Python Meetup: Python at Scale; Concurrency at Beeswax. Concurrent Python…

4 years ago

Python Dependencies The Right Way

My BazelCon 2019 lightning talk, Python Dependencies The Right Way*, has been posted. Please excuse…

4 years ago

Python 3 f-strings

One of my favorite features of Python 3 is f-strings (Formatted String Literals). Each of…

4 years ago

mtwsgi: A Multithreaded Python WSGI Implementation

I wanted to combine the simplicity of Python's built-in WSGI server with the benefits of…

11 years ago

This website uses cookies.