Python 3.x rounding behavior Python 3.x rounding behavior python python

Python 3.x rounding behavior


Python 3's way (called "round half to even" or "banker's rounding") is considered the standard rounding method these days, though some language implementations aren't on the bus yet.

The simple "always round 0.5 up" technique results in a slight bias toward the higher number. With large numbers of calculations, this can be significant. The Python 3.0 approach eliminates this issue.

There is more than one method of rounding in common use. IEEE 754, the international standard for floating-point math, defines five different rounding methods (the one used by Python 3.0 is the default). And there are others.

This behavior is not as widely known as it ought to be. AppleScript was, if I remember correctly, an early adopter of this rounding method. The round command in AppleScript offers several options, but round-toward-even is the default as it is in IEEE 754. Apparently the engineer who implemented the round command got so fed up with all the requests to "make it work like I learned in school" that he implemented just that: round 2.5 rounding as taught in school is a valid AppleScript command. :-)


You can control the rounding you get in Py3000 using the Decimal module:

>>> decimal.Decimal('3.5').quantize(decimal.Decimal('1'),     rounding=decimal.ROUND_HALF_UP)>>> Decimal('4')>>> decimal.Decimal('2.5').quantize(decimal.Decimal('1'),        rounding=decimal.ROUND_HALF_EVEN)>>> Decimal('2')>>> decimal.Decimal('3.5').quantize(decimal.Decimal('1'),     rounding=decimal.ROUND_HALF_DOWN)>>> Decimal('3')


Just to add here an important note from documentation:

https://docs.python.org/dev/library/functions.html#round

Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

So don't be surprised to get following results in Python 3.2:

>>> round(0.25,1), round(0.35,1), round(0.45,1), round(0.55,1)(0.2, 0.3, 0.5, 0.6)>>> round(0.025,2), round(0.035,2), round(0.045,2), round(0.055,2)(0.03, 0.04, 0.04, 0.06)