Consider decimal representations of the form d1.d2d3d4d5...dnExxx where xxx is an arbitrary exponent and both d1 and dn are nonzero.
Is the maximum n known such that there exists a decimal representation d1.d2d3d4d5...dnExxx such that the interval (d1.d2d3d4d5...dnExxx, d1.d2d3d4d5...((dn)+1)Exxx) contains an IEEE 754 double?
n should be at least 17. The question is how much above 17.
This number n has something to do with the number of digits that it is enough to consider in a decimal-to-double conversion such as strtod()
. I looked at the source code for David M. Gay's implementation hoping to find an answer there. There is an allusion to “40” but it is unclear whether this is a consequence of a sound mathematical result or just a statistically safe bound. Also the comment about “truncating” makes it sound like 0.5000000000000000000000000000000000000000000000000001 may be converted to 0.5 in round-upwards mode.
Musl's implementation seems to read approximately 125*9 digits, which is a lot. Then it switches to “sticky” mode:
if (c!='0') x[KMAX-4] |= 1;
Finally, how does the answer change when substituting “contains an IEEE 754 double” with “contains the midpoint of two consecutive IEEE 754 doubles”?
2^(-1074)
has 751 significant decimal digits, thus there is a decimal representationd1.d2...d750E-324
satisfying the condition (you can get longer ones, but not much). But you only need a handful of these digits to determine the closest IEEE754double
. – Daniel Fischer