31

In Python 2.7, repr of a float returns the nearest decimal number up to 17 digits long; this is precise enough to uniquely identify each possible IEEE floating point value. str of a float worked similarly, except that it limited the result to 12 digits; for most purposes this is a more reasonable result, and insulates you from the slight differences between binary and decimal representation.

Python 2 demo: http://ideone.com/OKJtxv

print str(1.4*1.5)
2.1
print repr(1.4*1.5)
2.0999999999999996

In Python 3.2 it appears str and repr return the same thing.

Python 3 demo: http://ideone.com/oAKRsb

print(str(1.4*1.5))
2.0999999999999996
print(repr(1.4*1.5))
2.0999999999999996

Is there a PEP that describes the change, or some other statement from someone responsible?

6
  • 2
    Looks like issue #1580 could be relevant here. There's also a note on this change in What’s New In Python 3.1: Other Language Changes (last bullet point).
    – Lukas Graf
    Sep 17, 2014 at 19:28
  • 6
    I guess this is this one: bugs.python.org/issue9337
    – georg
    Sep 17, 2014 at 19:34
  • Python 3.1 added a new feature to hide irrelevant rounding errors by rounding to the shortest decimal fraction within one epsilon of the float, instead of the closest, so the 12-digit hack is no longer necessary.
    – abarnert
    Sep 17, 2014 at 19:34
  • @abarnert I think my short example shows the fallacy of that thinking. The link you provide seems only concerned with what tests are going to break, rather than whether it's a good idea in the first place; perhaps the mailing list discussion has more rationale. Sep 17, 2014 at 19:46
  • 2
    @MarkRansom: Well, in that particular case, the 12-digit hack was wrong, as you can see by 2.0999999999999996 == 2.1. Sure, sometimes you may want that imprecision even though it's not irrelevant, but then sometimes you want only 2 digits too, and of course Python provides ways to do that explicitly and easily, it just doesn't do it implicitly. At any rate, if you want to argue over the rationale or try to change Python, this isn't the place to do it. Whether the arguments in the issues that Lukas and georg (not me) posted are fallacious or not, they were the arguments.
    – abarnert
    Sep 17, 2014 at 20:03

1 Answer 1

44

No, there's no PEP. There's an issue in the bug tracker, and an associated discussion on the Python developers mailing list. While I was responsible for proposing and implementing the change, I can't claim it was my idea: it had arisen during conversations with Guido at EuroPython 2010.

Some more details: as already mentioned in comments, Python 3.1 introduced a new algorithm for the string repr of a float, (later backported to the Python 2 series, so that it also appears in Python 2.7). As a result of this new algorithm, a "short" decimal number typed in at the prompt has a correspondingly short representation. This eliminated one of the existing reasons for the difference between str and repr, and made it possible to use the same algorithm for both str and repr. So for Python 3.2, following the discussion linked to above, str and repr were made identical. As to why: it makes the language a little bit smaller and cleaner, and it removes the rather arbitrary choice of 12 digits when outputting the string. (The choice of 17 digits used for the repr in Python versions prior to 2.7 is far from arbitrary, by the way: two distinct IEEE 754 binary64 floats will have distinct representations when converted to decimal with 17 significant digits, and 17 is the smallest integer with this property.)

Apart from simplicity, there are some less obvious benefits. One aspect of the repr versus str distinction that's been confusing for users in the past is the fact that repr automatically gets used in containers. So for example in Python 2.7:

>>> x = 1.4 * 1.5
>>> print x
2.1
>>> print [x]
[2.0999999999999996]

I'm sure there's at least one StackOverflow question asking about this phenomenon somewhere: here is one such, and another more recent one. With the simplification introduced in Python 3.2, we get this instead:

>>> x = 1.4 * 1.5
>>> print(x)
2.0999999999999996
>>> print([x])
[2.0999999999999996]

which is at least more consistent.

If you do want to be able to hide imprecisions, the right way to do it remains the same: use string formatting for precise control of the output format.

>>> print("{:.12g}".format(x))
2.1

I hope that explains some of the reasoning behind the change. I'm not going to argue that it's universally beneficial: as you point out, the old str had the convenient side-effect of hiding imprecisions. But in my opinion (of course, I'm biased), it does help eliminate a few surprises from the language.

3
  • Thanks for the info, this is exactly what I was looking for. I just wish there was an equivalent to the g format spec that didn't arbitrarily switch to scientific notation, I avoid it for that reason. Sep 17, 2014 at 20:36
  • Agreed; I've often wished for string formatting to be a little bit more configurable. One day, maybe. Sep 17, 2014 at 20:39
  • P.S. I'm not sure I buy the argument that the output of an object needs to be the same as the output of a list containing the object, that's still not the case for strings containing unprintable characters: ideone.com/zNauZB Sep 17, 2014 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.