I was just re-reading What’s New In Python 3.0 and it states:
The
round()
function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example,round(2.5)
now returns 2 rather than 3.)
and
the documentation for round()
:
For the built-in types supporting
round()
, values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, rounding is done toward the even choice
So, in Python 2 (for example, v2.7.3) I get the expected:
round(2.5)
3.0
round(3.5)
4.0
However, now under Python 3 (for example v3.2.3):
round(2.5)
2
round(3.5)
4
This seems counter-intuitive and contrary to what I understand about rounding (and bound to trip up people). English isn't my native language but until I read this I thought I knew what rounding meant :-/ I am sure at the time Python 3 was introduced there must have been some discussion of this, but I was unable to find a good reason in my search.
- Does anyone have insight into why this was changed to this?
- Are there any other mainstream programming languages (e.g., C, C++, Java, Perl, ..) that do this sort of (to me inconsistent) rounding?
What am I missing here?
UPDATE: @Li-aungYip's comment re "Banker's rounding" gave me the right search term/keywords to search for and I found this SO question: Why does .NET use banker's rounding as default?, so I will be reading that carefully.
round()
could just accept a separate argument to change the rounding behavior. – Elainaelaine340.33
does not exist in floating point arithmetic. When you see340.33
you're actually getting340.33499999999997953636921010911464691162109375
which of course does round to340.33
even under bankers rounding. Python puts in extra effort to display340.33
even though it knows it isn't because it thinks that's what you want to see: docs.python.org/3/tutorial/floatingpoint.html To find out what number you really have when you enter a float you canfrom decimal import Decimal
and thenDecimal(340.33)
. – Obsceneround
isn't mentioned in the docs. Normally the Python version where a change was made is recorded. – Obscene