[Python-Dev] Backport new float repr to Python 2.7?

Tim Peters tim.peters at gmail.com
Mon Oct 12 21:23:33 CEST 2009


[Mark Dickinson]
> It occurs to me that any doctests that depend on the precise form of
> repr(x) are, in a sense, already broken, since 2.x makes no guarantees
> about repr(x) being consistent across platforms.

The doctest documentation has warned about this forever (look near the
end of the "Warnings" section,

    http://docs.python.org/library/doctest.html#warnings

).


> It's just an accident that repr(x) in 2.x pretty much *is* consistent across
> major platforms, so long as you steer clear of IEEE 754 oddities like subnormals,
> nans and infinities.

If you don't consider Windows to be a major platform ;-)  Besides that
there's just no guessing what the Microsoft double->string routines
will produce for the 17th digit, the MS routines always produce 3
digits for the exponent in scientific notation, while AFAIK all other
platforms produce 2 digits when 3 aren't necessary.  For example, on
Windows under Python 2.x:

>>> repr(1e47)
'1e+047'
>>> repr(1e-47)
'9.9999999999999997e-048'

and "everywhere else":

>>> repr(1e47)
'1e+47'
>>> repr(1e-47)
'9.9999999999999997e-48'

The leading 0 in the Window's exponents is enough to break a naive
doctest too -- and repr(x) does produce scientific notation for
"almost all" finite doubles.  Why people are obsessed with the
relative handful of doubles between 1 and 1000 is beyond me ;-)

As doctest's original author, I have no sympathy for users who burn
themselves with float output.  Of course it's open to question whether
I have sympathy for anyone, ever, but that's not the issue here ;-)


More information about the Python-Dev mailing list