[Python-bugs-list] [Bug #132399] Problem with floats in dictionary values

noreply@sourceforge.net noreply@sourceforge.net
Wed, 14 Feb 2001 13:33:22 -0800


Bug #132399, was updated on 2001-Feb-14 13:29
Here is a current snapshot of the bug.

Project: Python
Category: Python Interpreter Core
Status: Closed
Resolution: Invalid
Bug Group: Not a Bug
Priority: 5
Submitted by: nobody
Assigned to : nobody
Summary: Problem with floats in dictionary values

Details: Python 2.0 seems to have a bug when using floats as the value in
a
dictionary.  Is there a fix for this that I need?

This is the Python 2.0 version of what I'm doing:

>>> x = {'a':0.4, 'b':0.4, 'c':0.4}
>>> print x
{'b': 0.40000000000000002, 'c': 0.40000000000000002, 'a':
0.40000000000000002}

But, if you just are doing this with normal scalar variables it works
fine.

>>> x = 0.4
>>> print x
0.4

And, notice the difference from the following Python 1.5.2 version below,
which treats the dictionary values correctly.

>>> x = {'a':0.4, 'b':0.4, 'c':0.4}
>>> print x
{'b': 0.4, 'c': 0.4, 'a': 0.4}



Follow-Ups:

Date: 2001-Feb-14 13:33
By: gvanrossum

Comment:
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

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

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=132399&group_id=5470