[Pythonmac-SIG] possible bug with division operator and floating point numbers

Johann Hibschman johannh@uclink.berkeley.edu
Wed, 17 Jan 2001 00:03:14 -0800


On Tue, 16 Jan 2001, Richard Gordon wrote:
> > it basically worked out to 4.0/5.0 .
> >which came back with the answer, "0.80000000000000004." Now I have been
> >out of school for a bit, but Ilearned that should be 0.8.
> 
> Maybe this has been covered here & I missed it, but you are certainly 
> correct and it's certainly strange.

No, this is correct.  Python 2.0 has switched to using the exact printable
representation of numbers in interactive mode.  It now prints out what repr
printed before, the number to all decimal places.  I do suspect that a better
algorithm exists for this, though.

What you're seeing is the fact that floating-point numbers aren't exact.
That's just the way that floats are, on any platform.  Before, the interpreter
hid this from you.  Now, it displays it in all its glory.

You should never count on a float to be exact, and never test a float for
equality.  The last digit is effectively random.

-- 
Johann Hibschman
johannh@uclink.berkeley.edu