Python 3.x rounding behavior

前端 未结 11 1682
别那么骄傲
别那么骄傲 2020-11-22 00:41

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 cas

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 01:05

    Sample Reproduction:

    ['{} => {}'.format(x+0.5, round(x+0.5)) for x in range(10)]
    
    ['0.5 => 0', '1.5 => 2', '2.5 => 2', '3.5 => 4', '4.5 => 4', '5.5 => 6', '6.5 => 6', '7.5 => 8', '8.5 => 8', '9.5 => 10']
    

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

    States:

    Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

    For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number.

    For a general Python object number, round delegates to number.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.

    Given this insight you can use some math to resolve it

    import math
    def my_round(i):
      f = math.floor(i)
      return f if i - f < 0.5 else f+1
    

    now you can run the same test with my_round instead of round.

    ['{} => {}'.format(x + 0.5, my_round(x+0.5)) for x in range(10)]
    ['0.5 => 1', '1.5 => 2', '2.5 => 3', '3.5 => 4', '4.5 => 5', '5.5 => 6', '6.5 => 7', '7.5 => 8', '8.5 => 9', '9.5 => 10']
    

提交回复
热议问题