[Python-bugs-list] [ python-Bugs-421384 ] bad floating point precision

noreply@sourceforge.net noreply@sourceforge.net
Mon, 06 May 2002 10:05:13 -0700


Bugs item #421384, was opened at 2001-05-04 10:08
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=421384&group_id=5470

Category: None
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: bad floating point precision

Initial Comment:
Platform: P3-700, Win2k (SP0), Python 2.1, IDLE 0.8.
Floating point calculations seem to have a very bad 
precision. Below is a log of a session. Things are 
getting worse when comparing two floating point 
values - in this example, 2.1*2/2 != 2.1!

#--[LOG START]--
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit 
(Intel)] on win32
Type "copyright", "credits" or "license" for more 
information.
IDLE 0.8 -- press F1 for help
>>> 2.1*2
4.2000000000000002
>>> 2.1*2/2
2.1000000000000001
>>> 
#--[LOG END]--


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

Comment By: Michael Donegan (invader8)
Date: 2002-05-06 12:05

Message:
Logged In: YES 
user_id=537045

This bug still exists in Python 2.2 (#1, Dec 31 2001, 15:21:18)
Should it be closed?

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

Comment By: Tim Peters (tim_one)
Date: 2001-05-04 14:04

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=421384&group_id=5470