[Python-bugs-list] [ python-Bugs-426711 ] Floating Point Curiosity in Python 2.1

noreply@sourceforge.net noreply@sourceforge.net
Wed, 23 May 2001 13:18:51 -0700


Bugs item #426711, was updated on 2001-05-23 11:27
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=426711&group_id=5470

Category: None
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Daniel Graham (danielgraham)
Assigned to: Nobody/Anonymous (nobody)
Summary: Floating Point Curiosity in Python 2.1

Initial Comment:
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on
linux 2

In python 2.1	in python 1.5.2
>>> .10*103
10.300000000000001	10.3	
>>>round(.10*103,2)
10.300000000000001	10.3
>>>round(.10*103.123,1)
10.300000000000001	10.3
>>>round(.10*103.123,2)
10.31			10.31
>>>round(.0*103.123,3)
10.311999999999999	10.312


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

>Comment By: Daniel Graham (danielgraham)
Date: 2001-05-23 13:18

Message:
Logged In: YES 
user_id=20306

OK, I finally get the point.  Thanks

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

Comment By: Tim Peters (tim_one)
Date: 2001-05-23 12:17

Message:
Logged In: YES 
user_id=31435

There is no difference in the behavior of round().  Read 
the canned response again <wink>:  the only difference is 
the number of decimal digits in Python's default *display* 
of round()'s result.  Here in a recent Python:

>>> round(.10 * 103.123, 3)
10.311999999999999
>>> print round(.10 * 103.123, 3)
10.312
>>>

Same result, different display rounding, and that's all.  
1.5.2 also computed 10.311999999999999 for the result; but 
it didn't show you the truth by default.

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

Comment By: Daniel Graham (danielgraham)
Date: 2001-05-23 11:55

Message:
Logged In: YES 
user_id=20306

I wasn't surprised by the "representation error" but by the
behavior of the function "round".

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

Comment By: Tim Peters (tim_one)
Date: 2001-05-23 11:33

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