classification
Title: 8/3 is calculated incorrectly
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jonas Wegelius, benjamin.peterson, eryksun
Priority: normal Keywords:

Created on 2016-10-03 06:20 by Jonas Wegelius, last changed 2016-10-03 08:59 by eryksun. This issue is now closed.

Messages (3)
msg277931 - (view) Author: Jonas Wegelius (Jonas Wegelius) Date: 2016-10-03 06:20
When you type 8/3, the interpreter return incorrect value:
2.6666666666666665

it should be 
2.6666666666666667
msg277932 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2016-10-03 06:23
Please see https://docs.python.org/3/tutorial/floatingpoint.html

Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2.6666666666666667 == 2.6666666666666665
True
msg277939 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-10-03 08:59
A CPython float uses the platform's double-precision floating point. The significand of a double has 53 bits of precision, which is 15 decimal digits of precision. However, uniquely representing a double in decimal requires 17 digits, which is why the str and repr format is extended with the otherwise insignificant "65" digits. For output, you can format the number with sys.float_info.dig decimal digits. For example:

    >>> sys.float_info.dig
    15
    >>> '%0.*g' % (sys.float_info.dig, 8/3)
    '2.66666666666667'
History
Date User Action Args
2016-10-03 08:59:25eryksunsetnosy: + eryksun
messages: + msg277939
2016-10-03 06:41:02SilentGhostsetstage: resolved
2016-10-03 06:23:30benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg277932

resolution: not a bug
2016-10-03 06:20:30Jonas Wegeliuscreate