[issue41198] Round built-in function not shows zeros acording significant figures and calculates different numbers of odd and even

Tim Peters report at bugs.python.org
Thu Jul 2 17:56:36 EDT 2020


Tim Peters <tim at python.org> added the comment:

I assumed Mark would tell us what's up with the arange() oddity, so let's see whether he does.  There is no truly good way to generate "evenly spaced" binary floats using a non-representable conceptual decimal delta.  The dumbass ;-) way doesn't show a discrepancy in pure Python:

>>> num = ne = no = 0
>>> d = 0.001
>>> while num < 1.0:
...     digit = int(round(num, 1) * 10)
...     if digit & 1:
...         no += 1
...     else:
...         ne += 1
...     num += d
>>> ne, no
(500, 500)

However, a somewhat less naive way does show a discrepancy, but less so than what arange() apparently does:

>>> ne = no = 0
>>> for i in range(1000):
...     digit = int(round(i * d, 1) * 10)
...     if digit & 1:
...         no += 1
...     else:
...         ne += 1
>>> ne, no
(501, 499)

I assume that's because of the specific nearest/even behavior I already showed for multipliers i=250 and i=750.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41198>
_______________________________________


More information about the Python-bugs-list mailing list