floating point in 2.0

Tim Peters tim.one at home.com
Wed Jun 6 13:33:46 EDT 2001


[Michael P. Soulier]
>     In Python 1.5.2, I could do this:
>
> good = (0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
>
> I could then get back exactly this when I output the value
> of "good".  However, with Python 2.0, I get this:
> >
> > >>> good
> > (0.0, 0.10000000000000001, 0.20000000000000001, 0.29999999999999999,
> > 0.40000000000000002, 0.5, 0.59999999999999998, 0.69999999999999996,
> > 0.80000000000000004, 0.90000000000000002, 1.0)

[Kurt B. Kaiser]
> from sys import displayhook
> def nogeekydisplay(x):
>     print x
>
> sys.displayhook = nogeekydisplay
>
> Put that in your .pythonrc config file so it will load at startup.

That particular code doesn't work, and if it worked as intended wouldn't
address Michael's complaint.

First, code that works as intended:

import sys
def nogeekydisplay(x):
    print x

sys.displayhook = nogeekydisplay

Then for why it doesn't address Michael's complaint:

>>> .1
0.1
>>> [.1]
[0.10000000000000001]
>>>

That is, while it does effectively replace repr() with str(), the builtin
container types (list, tuple, dict) pass repr() down to the things they
contain, regardless of whether str() or repr() was applied to *them*.
Afraid it takes more work than "it should" to worm around this.

BTW, people who want to play with the display hook should start with a
better simulation of the system display hook regardless, like:

import sys, __builtin__

def default_display_hook(x):
    if x is None:
        return
    print repr(x)
    __builtin__._ = x

sys.displayhook = default_display_hook

Then replacing "print repr(x)" with "pprint.pprint(x)" is a variation I
often use.





More information about the Python-list mailing list