[Python-bugs-list] [ python-Bugs-430200 ] corrupt floats in lists & tuples

noreply@sourceforge.net noreply@sourceforge.net
Mon, 04 Jun 2001 22:33:22 -0700


Bugs item #430200, was updated on 2001-06-04 22:10
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=430200&group_id=5470

Category: Python Interpreter Core
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: corrupt floats in lists & tuples

Initial Comment:
C Python 2.1 on GNU/Linux Mandrake-7.2.  Intel PIII.
Compiled from sources.

Assigning a tuple or list of floating-point numbers
returns corrupted numbers.  In the interpreter with
nothing imported: 
>>> t = (0.5, 0.6, 0.76, 0.1)
>>> t
(0.5, 0.59999999999999998, 0.76000000000000001,
0.10000000000000001)
>>> t = (1.5, 2.26, 3.76, 4.1)
>>> t
(1.5, 2.2599999999999998, 3.7599999999999998,
4.0999999999999996)
>>> l = [1.5, 2.26, 3.76, 4.1]
>>> l
[1.5, 2.2599999999999998, 3.7599999999999998,
4.0999999999999996]

At least one other identical case so far exists in my
Python Authors Group.

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2001-06-04 22:33

Message:
Logged In: YES 
user_id=31435

I closed this with a boilerplate reply set up for it.  Note 
that this (well, things "like this") has been a very active 
discussion topic on comp.lang.python over the past week.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-06-04 22:31

Message:
Logged In: YES 
user_id=31435

This is not a bug.

Binary floating point cannot represent decimal fractions exactly,
so some rounding always occurs (even in Python 1.5.2).

What changed is that Python 2.0 shows more precision than before
in certain circumstances (repr() and the interactive prompt). 

You can use str() or print to get the old, rounded output: 

>>> print 0.1+0.1
0.2
>>>

Follow the link for a detailed example:

http://www.python.org/cgi-bin/moinmoin/RepresentationError


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=430200&group_id=5470