[Python-bugs-list] [ python-Bugs-790885 ] float precision when printing a tuple directly

SourceForge.net noreply at sourceforge.net
Mon Aug 18 17:31:43 EDT 2003


Bugs item #790885, was opened at 2003-08-18 19:21
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=790885&group_id=5470

Category: Python Interpreter Core
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: André Thieme (mitt)
Assigned to: Nobody/Anonymous (nobody)
Summary: float precision when printing a tuple directly

Initial Comment:
When printing a from a function returned tuple that
contains a float produces a wrong output:

def foo1():
	return 2.9

print foo1()
#prints "2.9" as expected

def foo2():
	return (2.9, "Hello world")

print foo2()
#prints "(2.8999999999999999, 'Hello world')"
#where "(2.9, 'Hello world')" was expected



But:
x, y = foo2()
print x, y
#or
print "(" + str(x) + ", " + `y` + ")"
#work



André Thieme,
Germany

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

>Comment By: Tim Peters (tim_one)
Date: 2003-08-18 19:31

Message:
Logged In: YES 
user_id=31435

That was a canned response.  The link it contains is still 
relevant, though:  read about the difference between str
(a_float) (which lies) and repr(a_float) (which lies less).

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

Comment By: Tim Peters (tim_one)
Date: 2003-08-18 19:29

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://www.python.org/doc/2.2.1/tut/node14.html

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

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



More information about the Python-bugs-list mailing list