[issue36028] Integer Division discrepancy with decimal

Tim Peters report at bugs.python.org
Mon Feb 18 17:46:41 EST 2019


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

"Multiple roundings" in the float code is a red herring - that's just implementation details trying to cheaply get the same effect as computing with infinite precision.  Here with actual unbounded precision:

>>> from fractions import Fraction
>>> y = Fraction(0.4)
>>> y
Fraction(3602879701896397, 9007199254740992)
>>> q = 4 / y
>>> q
Fraction(36028797018963968, 3602879701896397)
>>> int(q)
9
>>> 4 - 9 * y
Fraction(3602879701896395, 9007199254740992)
>>> float(_)
0.3999999999999998
>>> 

So exactly the same results as divmod(4, 0.4) returned.

The underlying problem here is that the infinitely precise result of 4.0 / 0.4 is NOT an integer, in turn stemming from that the float 0.4 is not four tenths.

So I recommend to close this as not-a-bug, but I'm not doing that now because I want clarification on what the OP meant by saying the results differ between Pythons 2 and 3.  I see no differences here between Pythons 2.7.11 and 3.7.2 on 64-bit Windows (not in 4.0 vs 0.4, or in any of the other cases the OP mentioned).

----------

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


More information about the Python-bugs-list mailing list