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: wrong subtraction result
Type: Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Constantino.Antunes, r.david.murray
Priority: normal Keywords:

Created on 2014-09-02 10:03 by Constantino.Antunes, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg226270 - (view) Author: Constantino Antunes (Constantino.Antunes) Date: 2014-09-02 10:03
I was using python as my calculator when I got a result which had to be rounded to be the value I expected. So I made some tests with a simple case.
Given that 0.38 - 0.20 = 0.18, then 1000.38 - 1000.20 should be also 0.18.

Here is this calculation done on three different systems:

Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.38 - 0.20
0.18
>>> test = lambda x: (x+0.38) - (x+0.20)
>>> test(0)
0.18
>>> test(1000)
0.17999999999994998
>>> test(1000000)
0.18000000005122274
>>> test(1000000000)
0.1799999475479126
>>> test(1000000000000)
0.1800537109375
>>> test(1000000000000000)
0.125

---

Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.38 - 0.20
0.18
>>> test = lambda x: (x+0.38) - (x+0.20)
>>> test(0)
0.18
>>> test(1000)
0.17999999999994998
>>> test(1000000000000000)
0.125

---

Python 2.4.3 (#1, Oct 23 2012, 22:02:41) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.38 - 0.20
0.17999999999999999
>>> test = lambda x: (x+0.38) - (x+0.20)
>>> test(0)
0.17999999999999999
>>> test(1000)
0.17999999999994998
>>> test(1000000000000000)
0.125
msg226272 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-02 12:36
https://docs.python.org/2/tutorial/floatingpoint.html
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66521
2014-09-02 12:36:32r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg226272

resolution: not a bug
stage: resolved
2014-09-02 10:03:47Constantino.Antunescreate