Why is 2.3 = 2.2999999999999998 ?
Chad Netzer
cnetzer at mail.arc.nasa.gov
Thu Jan 23 20:12:08 EST 2003
On Thursday 23 January 2003 15:26, Arne Koewing wrote:
> i finally got it...
> that's just floats ;-)
>
> not rounding by default is quite irritating but you easily realize
> precision problems...
Note that the "rounding", is a result of printing. The value in memory
is the same regardless of how it is printed. T. Peters (I believe)
made the change in Python 2.<mumble> with regards to how repr() outputs
floats. str() still 'rounds' the output.
Hence:
$ python
Python 2.2.2 (#1, Jan 18 2003, 10:18:59)
[GCC 3.2.2 20030109 (Debian prerelease)] on linux2
>>> 2.2
2.2000000000000002
>>> print 2.2
2.2
>>> repr(2.2)
'2.2000000000000002'
>>> str(2.2)
'2.2'
>>> "%f" % 2.2
'2.200000'
>>> 2.2000000000000000
2.2000000000000002
>>> 2.2000000000000001
2.2000000000000002
Also,
>>> 2.2000000000000002 == 2.2
1
>>> 2.2000000000000001 == 2.2
1
>>> 2.2000000000000000 == 2.2
1
--
Bay Area Python Interest Group - http://www.baypiggies.net/
Chad Netzer
More information about the Python-list
mailing list