Index: Python/dtoa.c =================================================================== --- Python/dtoa.c (revision 77511) +++ Python/dtoa.c (working copy) @@ -1477,6 +1477,31 @@ if (!nd0) nd0 = nd; + + /* Summary of parsing results: + * + * - nd >= nd0 >= 1 + * + * - the nd significant digits are in s0[0:nd0] and s0[nd0+1:nd+1] + * (using the usual Python half-open slice notation) + * + * - the absolute value of the number represented by the original input + * string is n * 10**e, where n is the integer represented by the + * concatenation of s0[0:nd0] and s0[nd0+1:nd+1] + * + * - the first significant digit is nonzero + * + * - the last significant digit may or may not be nonzero; (some code + * currently assumes that it's nonzero; this is a bug) + * + * - y contains the value represented by the first min(9, nd) + * significant digits + * + * - if nd > 9, z contains the value represented by significant digits + * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z + * gives the value represented by the first min(16, nd) sig. digits. + */ + k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; dval(&rv) = y; if (k > 9) { @@ -1593,15 +1618,18 @@ /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */ /* minimum number of decimal digits to distinguish double values */ /* in IEEE arithmetic. */ - i = j = 18; - if (i > nd0) - j += dplen; - for(;;) { - if (--j <= dp1 && j >= dp0) - j = dp0 - 1; - if (s0[j] != '0') + + /* Truncate input to 18 significant digits, then discard any trailing + zeros on the result by updating nd, nd0, e and y suitably. (There's + no need to update z; it's not reused beyond this point.) */ + for (i = 18; i > 0; ) { + /* scan back until we hit a nonzero digit. significant digit 'i' + is s0[i] if i < nd0, s0[i+1] if i >= nd0. */ + --i; + if ((i < nd0 ? s0[i] : s0[i+1]) != '0') { + ++i; break; - --i; + } } e += nd - i; nd = i; @@ -1611,8 +1639,8 @@ y = 0; for(i = 0; i < nd0; ++i) y = 10*y + s0[i] - '0'; - for(j = dp1; i < nd; ++i) - y = 10*y + s0[j++] - '0'; + for(; i < nd; ++i) + y = 10*y + s0[i+1] - '0'; } } bd0 = s2b(s0, nd0, nd, y); Index: Lib/test/test_strtod.py =================================================================== --- Lib/test/test_strtod.py (revision 77511) +++ Lib/test/test_strtod.py (working copy) @@ -258,6 +258,8 @@ # issue 7632 bug 5: the following 2 strings convert differently '1000000000000000000000000000000000000000e-16', #'10000000000000000000000000000000000000000e-17', + # issue 7632 bug 8: the following produced 10.0 + '10.900000000000000012345678912345678912345', ] for s in test_strings: self.check_strtod(s)