Author mark.dickinson
Recipients alexandre.vassalotti, amaury.forgeotdarc, christian.heimes, gvanrossum, mark.dickinson, nascheme, noam, rhettinger, skip.montanaro, tim.peters
Date 2008-07-08.10:08:02
SpamBayes Score 0.00942489
Marked as misclassified No
Message-id <1215511685.36.0.664412307999.issue1580@psf.upfronthosting.co.za>
In-reply-to
Content
Here's the 'proof' that 15 digits should be enough:

Suppose that x is a positive (for simplicity) real number that's exactly 
representable as a decimal with <= 15 digits.  We'd like to know that
'%.15g' % (nearest_float_to_x) recovers x.

There are integers k and m such that 2**(k-1) <= x <= 2**k, and 10**(m-1) 
< x <= 10**m.  (e.g. k = ceiling(log_2(x)), m = ceiling(log_10(x)))

Let y be the closest floating-point number to x.  Then |y-x| <= 2**(k-54).  Hence |y-x| <= x*2**-53 < x/2 * 10**-15 <= 10**(m-15)/2.  It follows that 
x is the closest 15-digit decimal to y, hence that applying '%.15g' to y 
should recover x exactly.

The key step here is in the middle inequality, which uses the fact that
2**-52 < 10**-15.  The above doesn't work for subnormals, but I'm guessing 
that people don't care too much about these.  The argument above also 
depends on correct rounding, but there's actually sufficient leeway here 
(that is, 2**-52 is quite substantially smaller than 10**-15) that the 
whole thing would probably work even in the absence of correct rounding.
History
Date User Action Args
2008-07-08 10:08:05mark.dickinsonsetspambayes_score: 0.00942489 -> 0.00942489
recipients: + mark.dickinson, gvanrossum, tim.peters, skip.montanaro, nascheme, rhettinger, amaury.forgeotdarc, christian.heimes, alexandre.vassalotti, noam
2008-07-08 10:08:05mark.dickinsonsetspambayes_score: 0.00942489 -> 0.00942489
messageid: <1215511685.36.0.664412307999.issue1580@psf.upfronthosting.co.za>
2008-07-08 10:08:04mark.dickinsonlinkissue1580 messages
2008-07-08 10:08:02mark.dickinsoncreate