[issue36151] Incorrect answer when calculating 11/3

Terry J. Reedy report at bugs.python.org
Thu Feb 28 17:08:47 EST 2019


Terry J. Reedy <tjreedy at udel.edu> added the comment:

To add to Zach's answer:

Please do not mark issues for IDLE just because you use IDLE to send your code to Python and display the returned string answer. If in any doubt, rerun the same code directly with Python either interactively or from the command line.  (Note: Windows requires "", not '', on command line.)

C:\Users\Terry>python -c "print(11/3)"
3.6666666666666665 

As for the result: 11/3 cannot be exactly represented as a binary (or decimal) finite-precision floating point number.  In this case, the nearest binary float is less that 11/3.  Python prints floats with up to 17 decimal digits.    We can get more with the decimal module.

>>> decimal.Decimal(11/3)
Decimal('3.666666666666666518636930049979127943515777587890625')

We can also check with decimal float literals
>>> 11/3 == 3.6666666666666665)
True

This expression is true for all 18 digit literals from
3.66666666666666630 to
3.66666666666666674

It would have been better to ask about 11/3 on python-list (for instance), where other beginners could see the answer, rather than here.  I will repost my answer there.

----------

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


More information about the Python-bugs-list mailing list