This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients Carlos Neves, lemburg, mark.dickinson, rhettinger, steven.daprano, stutzbach, tim.peters
Date 2020-07-02.22:11:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1593727897.38.0.0390748812536.issue41198@roundup.psfhosted.org>
In-reply-to
Content
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)
History
Date User Action Args
2020-07-02 22:11:37steven.dapranosetrecipients: + steven.daprano, lemburg, tim.peters, rhettinger, mark.dickinson, stutzbach, Carlos Neves
2020-07-02 22:11:37steven.dapranosetmessageid: <1593727897.38.0.0390748812536.issue41198@roundup.psfhosted.org>
2020-07-02 22:11:37steven.dapranolinkissue41198 messages
2020-07-02 22:11:37steven.dapranocreate