floating point glitch

Tim Roberts timr at probo.com
Sun Sep 26 08:10:56 CEST 2004


David O'Farrell <David.OFarrell at ericsson.com> wrote:
>
>Is this only on solaris ?

Every IEEE754 processor, every language.

>Python 2.3.3 (#1, Mar 19 2004, 16:18:33)
>[GCC 2.95.2 19991024 (release)] on sunos5
>Type "help", "copyright", "credits" or "license" for more information.
> >>> a=[66.6, 333, 333, 1, 1234.5]
> >>> print a.count(333), a.count(66.6), a.count('x')
>2 1 0
> >>> a.append(333)
> >>> print a
>[66.599999999999994, 333, 333, 1, 1234.5, 333]

This is a FAQ.  The short answer is that 66.6 cannot be represented exactly
in binary.  It is an infinitely repeating fraction.  (1234.5 is not, which
is why the same thing didn't happen to it.)

When you use print, it calls repr() to get the string representation.  repr
tells you the exact value, as close as possible.  That value is as close as
you can get to 66.6 using a 64-bit IEEE754 float.

str() lies to you to make you happy:

>>> str(a[0])
66.6
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list