R. David Murray wrote:
Gary Herron <gher...@islandtraining.com> wrote:
MRAB wrote:
Grant Edwards wrote:
On 2009-05-21, Christian Heimes <li...@cheimes.de> wrote:
seanm...@gmail.com schrieb:
The explaination in my introductory Python book is not very
satisfying, and I am hoping someone can explain the following to me:

4 / 5.0
0.80000000000000004

4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
It bothers me.
Welcome to IEEE 754 floating point land! :)
Floating point is sort of like quantum physics: the closer you
look, the messier it gets.
+1 as QOTW

And just to add one bit of clarity: This problem has nothing to do with the OP's division of 4 by 5.0, but rather that the value of 0.8 itself cannot be represented exactly in IEEE 754. Just try

 >>> print repr(0.8)  # No division needed
'0.80000000000000004'

Python 3.1b1+ (py3k:72432, May 7 2009, 13:51:24) [GCC 4.1.2 (Gentoo 4.1.2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
4 / 5.0
0.8
print(repr(0.8))
0.8

In py3k Eric Smith and Mark Dickinson have implemented Gay's floating
point algorithm for Python so that the shortest repr that will round
trip correctly is what is used as the floating point repr....

--David

Which won't change the fact that 0.8 and lots of other favorite floats are still not representable exactly, but it will hide this fact from most newbies. One of the nicer results of this will be that these (almost) weekly questions and discussions will be come a thing of the past.
With a sigh of relief,
Gary Herron





--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to