Steven D'Aprano <steve+pyt...@pearwood.info> added the comment:

If you change the starting point of the rounding away from zero, the bias flips 
back and forth, which is exactly what I would expect from Banker's Rounding:


    def check_bias(start):
        d = 0.001
        ne = no = 0
        for i in range(1000):
            digit = int(round(start + i * d, 1) * 10)
            if digit & 1:
                no += 1
            else:
                ne += 1
        return ne, no


    # Python 3.7
    >>> check_bias(0.0)
    (501, 499)
    >>> check_bias(0.1)
    (500, 500)
    >>> check_bias(0.2)
    (499, 501)
    >>> check_bias(0.3)
    (499, 501)
    >>> check_bias(0.4)
    (500, 500)
    >>> check_bias(0.5)
    (499, 501)
    >>> check_bias(0.6)
    (501, 499)


I ran the same check_bias in Python 2.7, which doesn't use bankers rounding, 
and the bias is consistently in one direction:

    # Python 2.7
    >>> check_bias(0.0)
    (500, 500)
    >>> check_bias(0.1)
    (499, 501)
    >>> check_bias(0.2)
    (498, 502)
    >>> check_bias(0.3)
    (498, 502)
    >>> check_bias(0.4)
    (499, 501)
    >>> check_bias(0.5)
    (498, 502)
    >>> check_bias(0.6)
    (500, 500)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41198>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to