[issue36151] Incorrect answer when calculating 11/3

2019-02-28 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

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.6665


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.

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36151] Incorrect answer when calculating 11/3

2019-02-28 Thread Terry J. Reedy


Terry J. Reedy  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.6665 

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.666518636930049979127943515777587890625')

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

This expression is true for all 18 digit literals from
3.66630 to
3.66674

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36151] Incorrect answer when calculating 11/3

2019-02-28 Thread Zachary Ware


Zachary Ware  added the comment:

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.

--
assignee: terry.reedy -> 
components:  -IDLE
nosy: +zach.ware
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed
title: Incorrect answer when calculating 11//3 -> Incorrect answer when 
calculating 11/3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36151] Incorrect answer when calculating 11//3

2019-02-28 Thread Aiden Zhou


Change by Aiden Zhou :


Removed file: https://bugs.python.org/file48180/Screenshot (102)_LI.jpg

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36151] Incorrect answer when calculating 11//3

2019-02-28 Thread Aiden Zhou


Change by Aiden Zhou :


Added file: https://bugs.python.org/file48180/Screenshot (102)_LI.jpg

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36151] Incorrect answer when calculating 11//3

2019-02-28 Thread Aiden Zhou


New submission from Aiden Zhou :

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)

--
assignee: terry.reedy
components: IDLE
files: Screenshot (102)_LI.jpg
messages: 336868
nosy: azihdoeun, terry.reedy
priority: normal
severity: normal
status: open
title: Incorrect answer when calculating 11//3
type: behavior
versions: Python 3.7
Added file: https://bugs.python.org/file48179/Screenshot (102)_LI.jpg

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com