[Edu-sig] Math weirdness?

Christopher A. Craig com-nospam@ccraig.org
03 Oct 2001 16:02:11 -0400


"Chris Meyers" <cmeyers@guardnet.com> writes:

> 10/03/2001 9:03:17 AM, Robert Rickenbrode II <rkr_ii@yahoo.com> wrote:
> 
> Interestingly, I get different results on Python 1.5.2 running on a
> Sparc.  For one thing the precision is lower. math.pi is
> 3.14159265359, and rounding it correctly gives 3.142. 10.0/3 gives
> 3.33333333333 again 12 digit accuracy. So I think there is a
> question whether later versions of Python have broken something in
> floating point calcuations.

Nope, the earlier ones had something broken in their floating point
representation.  1.6 and later return the full precision of the number
in repr(), older than that Python rounded to 12 decimal digits.

Your Python (1.5.2) still thinks 10./3 is 3.3333333333333335, it just
tells you it's 3.33333333333 because it thinks that will make you feel
better.  This scheme presented the interesting problem that
assert(float(repr(f))==f) would fail for some values of 'f' (for
example try 3.3333333333333339).  

When 1.6 came out this problem was addressed.  You can still get the
old behavior by using str() instead of repr() though.

-- 
Christopher A. Craig <com-nospam@ccraig.org>
"Unix-to-Unix Copy Program," said PDP-1. "You will never find a more
wretched hive of bugs and flamers. We must be cautious." - DEC Wars