classification
Title: Math calculation problem (1.6-1.0)>0.6, python said TRUE
Type: behavior Stage:
Components: Windows Versions: Python 3.1
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: DhaReaL, mark.dickinson, tim.peters
Priority: normal Keywords:

Created on 2010-01-14 20:44 by DhaReaL, last changed 2010-01-14 22:37 by tim.peters. This issue is now closed.

Files
File name Uploaded Description Edit
bug.py DhaReaL, 2010-01-14 20:44 example code of the bug
unnamed DhaReaL, 2010-01-14 22:04
Messages (4)
msg97785 - (view) Author: pedro flores (DhaReaL) Date: 2010-01-14 20:44
this simple comparison (1.6-1.0)>0.6 , python answer TRUE, and that isnt true.
msg97786 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-14 20:54
This is not a bug:  Python, like many other computer languages, stores floats in binary.  The values 1.6 and 0.6 aren't exactly representable in the internal format used, so the stored versions of 1.6 and 0.6 are actually just very close approximations to those values.  It just so happens that the approximation for 1.6 is a tiny amount larger than 1.6 (the exact value stored is 1.600000000000000088817841970012523233890533447265625), while the approximation for 0.6 is a tiny amount smaller than 0.6 (the exact value is 0.59999999999999997779553950749686919152736663818359375).

I recommend looking at the appendix to the Python tutorial for more information about floating point:

http://docs.python.org/tutorial/floatingpoint.html
msg97789 - (view) Author: pedro flores (DhaReaL) Date: 2010-01-14 22:04
kk, then i cannot use this comparison?, and this not happen
with....8.6-8>0.6 this is false, according to python.

2010/1/14 Mark Dickinson <report@bugs.python.org>

>
> Mark Dickinson <dickinsm@gmail.com> added the comment:
>
> This is not a bug:  Python, like many other computer languages, stores
> floats in binary.  The values 1.6 and 0.6 aren't exactly representable in
> the internal format used, so the stored versions of 1.6 and 0.6 are actually
> just very close approximations to those values.  It just so happens that the
> approximation for 1.6 is a tiny amount larger than 1.6 (the exact value
> stored is 1.600000000000000088817841970012523233890533447265625), while the
> approximation for 0.6 is a tiny amount smaller than 0.6 (the exact value is
> 0.59999999999999997779553950749686919152736663818359375).
>
> I recommend looking at the appendix to the Python tutorial for more
> information about floating point:
>
> http://docs.python.org/tutorial/floatingpoint.html
>
> ----------
> nosy: +mark.dickinson
> resolution:  -> invalid
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue7704>
> _______________________________________
>
msg97793 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2010-01-14 22:37
You can use the comparison, provided you understand what it does, and that it does NOT do what you hoped it would do.  Here:

>>> 1.6 - 1.0
0.60000000000000009

That shows quite clearly that subtracting 1 from the binary approximation to 1.6 does not leave exactly 0.6.  If you need that to happen, use the decimal module (which, in return for being slower, emulates decimal floating-point arithmetic).

Your other case (8.6-8.0) does NOT equal (decimal) 0.6 exactly either, but fools you into thinking it's working the way you hope it works because the result is a little /less/ than 0.6:

>>> 8.6 - 8.0
0.59999999999999964

Do read the tutorial appendix Mark invited you to read.  If you don't, you're going to remain hopelessly confused ;-)
History
Date User Action Args
2010-01-14 22:37:41tim.peterssetnosy: + tim.peters
messages: + msg97793
2010-01-14 22:04:45DhaReaLsetfiles: + unnamed

messages: + msg97789
2010-01-14 20:54:25mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg97786

resolution: not a bug
2010-01-14 20:44:26DhaReaLcreate