[Pythonmac-SIG] Re: Number format in IDE

John W Baxter jwblist@olympus.net
Sun, 26 Aug 2001 11:22:31 -0700


At 12:49 -0500 8/26/2001, Jon Bradley wrote:
>Even when using float, the actual representation should be followed by a
>bunch of zeros - no matter what number represetation is being used.  so, 5.1
>+ 2 = 7.1000000000000000000000.  It should not equal anything else, that
>would be a direct result of incorrect calculation of the number on the
>languages part.

No...first...all those trailing zeros, if true, would be removed.

But more importantly, 5.1 is not exactly representable in binary floating
point.  (There are other representations in which it is exactly
representable:  decimal floating point, "rational" (a number containing a
numerator and denominator...1 and 10 in this case), etc).

The 5 is fine...the .1 is an endless beast.  The bit pattern used is the
one which comes closest to representing 5.1.

That's what I meant before by saying that older Python versions (like
1.5.2) lied about this...they rounded to 13(?) places, in that rounding,
5.1 indeed shows as 5.1).  This has nothing to do with the precision of the
floating point representation (C's long double can't represent 5.1 either,
using 128 bits instead of 64), although whether the representation of 5.1
is too high or too low will vary with precision.  Not does it have anything
to do with what chip is used (the famous Intel division bug was a different
issue).

It's inherent in trying to find a summation of selected elements of  1/2,
1/4, 1/8, ... which represents the value.  5.5 is easy...1/2 does the job.
5.25 is easy.  5.1 is impossible.

I think it's important to let students know about this annoyance early.
For instance, it is almost always an error to compare a floating point
result to 0.0...you should test whether the value is 'small enough'
instead...where 'small enough' varies with the problem at hand.

You may find these annoying as well:

>>> .1 + .1 + .1 == .3
0
The sum is almost equal to .3's closest representation, but not quite:
>>> .1 + .1 + .1 - .3
5.5511151231257827e-17

>>> 5.1 * 100
509.99999999999994


[These happen to have been run in Python 2.1 as installed by me into Mac OS
X from the Unix distribution.]

  --John
-- 
John Baxter   jwblist@olympus.net      Port Ludlow, WA, USA