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).
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).
Exception chaining solves two problems in Python 2. 1. Swallowed Exceptions If, while handling exception…
Here's the recording of my talk "15 minutes w/ Beeswax: Safe Password Storage - How…
My presentation from the NYC Python Meetup: Python at Scale; Concurrency at Beeswax. Concurrent Python…
My BazelCon 2019 lightning talk, Python Dependencies The Right Way*, has been posted. Please excuse…
One of my favorite features of Python 3 is f-strings (Formatted String Literals). Each of…
I wanted to combine the simplicity of Python's built-in WSGI server with the benefits of…
This website uses cookies.