Python 3.x rounding behavior

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, under v2.7.3 :

In [85]: round(2.5)
Out[85]: 3.0

In [86]: round(3.5)
Out[86]: 4.0

as I'd have expected. However, now under v3.2.3 :

In [32]: round(2.5)
Out[32]: 2

In [33]: round(3.5)
Out[33]: 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 v3 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 (eg, 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.


    Python 3.0's way 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 actually does offer 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. :-)


    您可以使用Decimal模块控制您在Py3000中获得的舍入:

    >>> 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)
    
    链接地址: http://www.djcxy.com/p/8504.html

    上一篇: 为什么.NET使用银行家的舍入作为默认值?

    下一篇: Python 3.x舍入行为