Message372890
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) |
|
Date |
User |
Action |
Args |
2020-07-02 22:11:37 | steven.daprano | set | recipients:
+ steven.daprano, lemburg, tim.peters, rhettinger, mark.dickinson, stutzbach, Carlos Neves |
2020-07-02 22:11:37 | steven.daprano | set | messageid: <1593727897.38.0.0390748812536.issue41198@roundup.psfhosted.org> |
2020-07-02 22:11:37 | steven.daprano | link | issue41198 messages |
2020-07-02 22:11:37 | steven.daprano | create | |
|