[ python-Bugs-1534769 ] Identical floats print inconsistently

SourceForge.net noreply at sourceforge.net
Fri Aug 4 23:24:13 CEST 2006


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

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Marc W. Abel (gihon)
Assigned to: Nobody/Anonymous (nobody)
Summary: Identical floats print inconsistently

Initial Comment:
Hi, and thank you.

Many bugs relating to this have been submitted by
others over a period of years, and these have generally
been closed with "not a bug" responses.  I'll do my
best to explain the problem clearly enough.

The following session prints a single variable three
ways, with two different results:

-----------

[me at localhost current]$ python
Python 2.4.1 (#1, May 16 2005, 15:19:29)
[GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> a = round(1./7, 3)
>>> print a
0.143
>>> print (a,)
(0.14299999999999999,)
>>> print (a,)[0]
0.143
>>>

-----------

I'm fully informed about IEEE floating point
representations in binary, but the limitations of data
type are not causing the difference in output.  The
interpreter is using different rules to print this
float, depending on whether it's a straight float or
part of some other structure (in this example, a tuple).

Once the interpreter recurses to a depth where it's
clearly going to print a float, whatever rule is
selected needs to be consistently applied.  This means
calling the same string formatting code with the same
inputs.

Marc


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

>Comment By: Tim Peters (tim_one)
Date: 2006-08-04 17:24

Message:
Logged In: YES 
user_id=31435

gbrandl is correct that the differences here are entirely
due to the difference between float's __repr__ and __str__
methods.  The reasons they /are/ different for floats are
explained in the Tutorial's appendix on floating-point issues.

It may be considered unfortunate that tuple.__repr__ is the
same as tuple.__str__, and that both "pass repr down" to
containees, but the reason for that has to do with the
difference between str.__repr__ and str.__str__.  If
str(tuple) "passed str down" to containees, then, e.g.,

    print ("a, bc", "de f,", "gh), i")

would display the incomprehensibly confused:

    (a, bc, de f,, gh), i)

IOW, it's actually the difference in what string types do
that /drives/ the general decision that c.__str__ is the
same as c.__repr__ for all objects `c` of container types,
and that both "pass repr down" to containees.

Alas, that's not always what people using strings want
either :-)

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

Comment By: Georg Brandl (gbrandl)
Date: 2006-08-04 16:14

Message:
Logged In: YES 
user_id=849994

Sorry, but this is another "not a bug".

"print tuple" invokes tuple.__repr__() (because there is no
separate __str__) which invokes repr(item) on each tuple
item, while "print item" invokes str(item). For floats, this
distinction results in a different rounding precision.

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

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


More information about the Python-bugs-list mailing list