Author noam
Recipients christian.heimes, gvanrossum, nascheme, noam, rhettinger, tim.peters
Date 2007-12-17.21:53:19
SpamBayes Score 0.0177636
Marked as misclassified No
Message-id <1197928400.58.0.0155950146692.issue1580@psf.upfronthosting.co.za>
In-reply-to
Content
Ok, I think I have a solution!

We don't really need always the shortest decimal representation. We just
want that for most floats which have a nice decimal representation, that
representation will be used. 

Why not do something like that:

def newrepr(f):
    r = str(f)
    if eval(r) == f:
        return r
    else:
        return repr(f)

Or, in more words:

1. Calculate the decimal representation of f with 17 precision digits,
s1, using the system's routines.
2. Create a new string, s2, by rounding the resulting string to 12
precision digits.
3. Convert the resulting rounded string to a new double, g, using the
system's routines.
4. If f==g, return s2. Otherwise, return s1.

It will take some more time than the current repr(), because of the
additional decimal to binary conversion, but we already said that if
speed is extremely important one can use "'%f.17' % f". It will
obviously preserve the eval(repr(f)) == f property. And it will return a
short representation for almost any float that has a short representation.

This algorithm I will be glad to implement.

What do you think?
History
Date User Action Args
2007-12-17 21:53:20noamsetspambayes_score: 0.0177636 -> 0.0177636
recipients: + noam, gvanrossum, tim.peters, nascheme, rhettinger, christian.heimes
2007-12-17 21:53:20noamsetspambayes_score: 0.0177636 -> 0.0177636
messageid: <1197928400.58.0.0155950146692.issue1580@psf.upfronthosting.co.za>
2007-12-17 21:53:20noamlinkissue1580 messages
2007-12-17 21:53:19noamcreate