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

David Lees debl2nonospammyWhammy at bellatlantic.net
Thu May 30 20:48:57 EDT 2002


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
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.  You are fooling yourself
if you think that perl and python 1.5.2 give the 'right' answer, but
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

In fact, you are likely to get in trouble when one of your 'correct'
answers is used in an arithmetic comparision and you get an unexpected
result because you thought you were comparing integers, but underneath a
floating point number is compared with an integer.


Your distinction between 'correct' and 'right' answers escapes me.  You
seem to be implying that something is wrong with C and the current
Python implementation, but this is not the case.  They likely all use
the same floating point libraries and hardware an have the same answers
inside.

David Lees

"David K. Trudgett" wrote:
> 
> On Thursday 2002-05-30 at 14:22:47 +0000, Michael Hudson wrote:
> 
> > Read this:
> >
> > http://www.python.org/doc/2.2.1/tut/node14.html
> >
> > (the fact that I can type this URL from memory should be some
> > indication that you could have answered your query yourself with a
> > little effort).
> 
> Obviously, something has changed since Python 1.5.2:
> 
> Python 1.5.2
> >>> 0.1
> 0.1
> >>> 3.1*2
> 6.2
> >>> sum = 0.0
> >>> for i in range(10):
> ...     sum = sum + 0.1
> ...
> >>> sum
> 1.0
> 
> Perl:
> 
> bash$ perl -e 'print 3.1*2 . "\n"'
> 6.2
> 
> bash$ perl -e '$sum = 0;foreach(1..10){$sum += 0.1}; print "$sum\n";'
> 1
> 
> Python 2.1.3
> 
> >>> 0.1
> 0.10000000000000001
> >>>
> 
> >>> 3.1*2
> 6.2000000000000002
> >>>
> 
> >>> sum = 0.0
> >>> for i in range(10):
> ...     sum = sum + 0.1
> ...
> >>> sum
> 0.99999999999999989
> >>>
> 
> Personally, I expect a scripting language to give me the right answer,
> not the "correct" answer. For "correctness" I can use C.
> 
> I suppose arguments could be made on both sides...
> 
> David Trudgett



More information about the Python-list mailing list