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.

classification
Title: Incorrect answer when calculating 11/3
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: azihdoeun, steven.daprano, terry.reedy, zach.ware
Priority: normal Keywords:

Created on 2019-02-28 21:07 by azihdoeun, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Screenshot (102)_LI.jpg azihdoeun, 2019-02-28 21:07 Screenshot taken at the issue
Messages (4)
msg336868 - (view) Author: Aiden Zhou (azihdoeun) * Date: 2019-02-28 21:07
When calculating 11//3, the last digit of the decimals is wrongly rounded to 5 as obviously it needs to be rounded to 7. IDLE (Python 3.7 32-bit)
msg336869 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2019-02-28 21:20
This has nothing to do with IDLE and everything to do with how floating point numbers work; have a read through https://docs.python.org/3/tutorial/floatingpoint.html for an introduction to floating point.  If you need exact decimal math, use the `decimal` module instead.
msg336873 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-02-28 22:08
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.
msg336874 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-02-28 22:20
Aiden: in the future, please do not post unnecessary screenshots of text. They are hostile to the blind and slight-impaired, they make it impossible to copy your code and run it ourselves, and they cannot be searched for.

Instead, copy and paste the text demonstrating the code and the results:

py> 11/3
3.6666666666666665


Thank you.

Zachary: while the rest of your comments were excellent, please don't tell people that Decimal is "exact". Decimal is a floating point format just like floats are, and so suffers the same kinds of rounding errors as binary floats. In fact, Decimal often suffers from larger rounding errors than binary, which is why serious numeric data scientists still use binary float.

The advantages of Decimal over binary float is that you can configure how much precision is used, and that any number you can write out exactly in base 10 can be represented exactly as a Decimal. But it still has to round off values which cannot be represented exactly as Decimals.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80332
2019-02-28 22:20:35steven.dapranosetnosy: + steven.daprano
messages: + msg336874
2019-02-28 22:08:47terry.reedysetmessages: + msg336873
2019-02-28 21:20:37zach.waresetstatus: open -> closed

assignee: terry.reedy ->
components: - IDLE
title: Incorrect answer when calculating 11//3 -> Incorrect answer when calculating 11/3
nosy: + zach.ware

messages: + msg336869
resolution: not a bug
stage: resolved
2019-02-28 21:09:58azihdoeunsetfiles: - Screenshot (102)_LI.jpg
2019-02-28 21:08:47azihdoeunsetfiles: + Screenshot (102)_LI.jpg
2019-02-28 21:07:43azihdoeuncreate