[Python-bugs-list] [ python-Bugs-473456 ] float round up on decimal numbers

noreply@sourceforge.net noreply@sourceforge.net
Sun, 21 Oct 2001 13:01:48 -0700


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

Category: Python Interpreter Core
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Frederic Giacometti (giacometti)
Assigned to: Tim Peters (tim_one)
Summary: float round up on decimal numbers

Initial Comment:

We used to get this right on python 1.5.2; and this breaks since Python 2.0:

>>> for i in range( 10): x = 0.1 * i; print x, str( x), repr( x)
...
0.0 0.0 0.0
0.1 0.1 0.10000000000000001
0.2 0.2 0.20000000000000001
0.3 0.3 0.30000000000000004
0.4 0.4 0.40000000000000002
0.5 0.5 0.5
0.6 0.6 0.60000000000000009
0.7 0.7 0.70000000000000007
0.8 0.8 0.80000000000000004
0.9 0.9 0.90000000000000002
>>> print 0.6 == 0.60000000000000009
0
>>>

>>> 0.3
0.29999999999999999
>>> 0.1
0.10000000000000001
>>> 1.0
1.0
>>> 1.01
1.01
>>> 0.8
0.80000000000000004
>>> 0.5
0.5
>>> 1.3
1.3
>>>

We get the same problem on Unix and Windows; and python1.5.2 gives the exact decimal values 
everywhere.


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

>Comment By: Tim Peters (tim_one)
Date: 2001-10-21 13:01

Message:
Logged In: YES 
user_id=31435

BTW, that was the canned "float precision" response (this 
is a FAQ).  Your last example illustrates why Python made 
this change:  the best possible 754 double approximation to 
0.6 is indeed not equal to the best possible 754 double 
approximation to the product of 6 with the best possible 
754 double approximation to 0.1.  So if you expected 0.6 to 
equal 0.1 * 6 exactly, you probably shouldn't be using 
floating-point at all, in Python or any other language 
<wink>.

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

Comment By: Tim Peters (tim_one)
Date: 2001-10-21 12:50

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 more information:

http://python.sourceforge.net/devel-docs/tut/node14.html

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

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