!!! 2.1, 2.2, 2.2.1, PYTHON BUGS ????

David K. Trudgett dkt at registriesltd.com.au
Thu May 30 21:56:53 EDT 2002


On Friday 2002-05-31 at 00:48:57 +0000, David Lees wrote:

> David,
> 
> The link that was referenced gives a pretty good explanation of what is
> going on.  I would guess that the output routine for printing floating

Yes, someone else explained about that. I did refer to that link, but
I probably didn't read all of it.


> point was changed between Python 1.5.2 and Python 2.1.  If you are
> bothered by the representational rounding for 0.1, you can always
> truncate the answer to fewer decimal places.  

Yes, which is what one would normally do, probably, depending on how
quick and dirty the program is.


> You are fooling yourself
> if you think that perl and python 1.5.2 give the 'right' answer, but

I was slightly tongue in cheek with "right" and "correct".


> python 2.1 somehow does not.  You confuse truncated output with internal
> representation.  For example, if you subtract one from your perl
> example, you do NOT get zero:
> 
>  perl -e '$sum = 0;foreach(1..10){$sum += 0.1};$sum -= 1.0; print
> "$sum\n";'
> -1.11022302462516e-16

Bitten! :-) Didn't I say so? ;-) Actually, I never do stuff like that,
having done most of my past programming in Pascal (mostly Turbo Pascal
and Object Pascal). Just like in C, and probably just about every
other language, one needs to be on the ball with floating point
representation when doing numerical work.

For the benefit of those who may not know, both Python and Perl have
support for large integer/floating point arithmetic (arbitrary
precision). It's slow, but for precision, it can't be beat! :-)  

I believe, in Python 2.2 or higher, one needs to do something like

from mx.Number import *
i = Float(3.1, precision=128)
i = i * 2
print i.format(10)

I don't have Python 2.2, so perhaps someone else can elaborate or
correct if the above isn't quite right.

In older Pythons, I believe there is an "mpz" module that does
something similar.


David Trudgett






More information about the Python-list mailing list