[Python-bugs-list] [Bug #127483] repr and str return different results for a some floats

noreply@sourceforge.net noreply@sourceforge.net
Wed, 03 Jan 2001 22:25:53 -0800


Bug #127483, was updated on 2001-Jan-03 22:00
Here is a current snapshot of the bug.

Project: Python
Category: Python Interpreter Core
Status: Closed
Resolution: Invalid
Bug Group: Feature Request
Priority: 5
Submitted by: nobody
Assigned to : nobody
Summary: repr and str return different results for a some floats

Details: I was getting some strange result when converting between strings
and floats for doing calculations and discovered that it was happening when
executing the following code.

def test_float( a_float ) :
	print a_float, str( a_float ), repr( a_float )
	
if __name__ == '__main__' :
	test_float( 0.01 )
	test_float( 0.02 )
	test_float( 0.03 )
	test_float( 0.04 )
	test_float( 0.05 )
	test_float( 0.06 )
	test_float( 0.07 )

RESULTS
0.01 0.01 0.01
0.02 0.02 0.02
0.03 0.03 0.029999999999999999
0.04 0.04 0.040000000000000001
0.05 0.05 0.050000000000000003
0.06 0.06 0.059999999999999998
0.07 0.07 0.070000000000000007


Follow-Ups:

Date: 2001-Jan-03 22:25
By: tim_one

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