[issue9009] Improve quality of Python/dtoa.c

Mark Dickinson report at bugs.python.org
Wed Jun 16 21:22:54 CEST 2010


Mark Dickinson <dickinsm at gmail.com> added the comment:

Here's the section of the 'bigcomp' code that I was referring to above:

	/* Now b/d = exactly half-way between the two floating-point values */
	/* on either side of the input string.  Compute first digit of b/d. */

	if (!(dig = quorem(b,d))) {
		b = multadd(b, 10, 0);	/* very unlikely */
		dig = quorem(b,d);
		}

You can see it in the original source at http://www.netlib.org/fp/dtoa.c

This code is part of the algorithm for strtod.  Here b and d are Bigints, and b / d is a fraction that gives an approximation to the value of the input to strtod;  the aim is to produce the digits of b / d one-by-one to compare them with the strtod input, and (eventually) use the result of that comparison work out whether to round up or down.

If the condition of the 'if' block above is ever satisfied, b is multiplied by 10 (that's the multadd(b, 10, 0) call), so the fraction b / d is multiplied by 10 (with no corresponding correction for the strtod input string), and the wrong comparison is made!

There are many other similar pieces of code in _Py_dg_strtod that can never get called (I've run coverage programs over the code to help verify this);  trying to establish the correctness of the current code isn't easy.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9009>
_______________________________________


More information about the Python-bugs-list mailing list