[issue3166] Make conversions from long to float correctly rounded.

Mark Dickinson report at bugs.python.org
Tue Dec 9 20:18:31 CET 2008


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

By the way, the algorithm here is essentially the same as the algorithm that I 
implemented for the float.fromhex method, except that the float.fromhex method is more 
complicated in that it may have to deal with signed zeros or subnormals.

So any mathematical defects that you find in this patch probably indicate a defect in 
float.fromhex too.

In fact, the code *does* do integer arithmetic, except that one of the integers happens 
to be stored as a double.  If you look at the code you'll find that at every stage, the 
floating-point variable "x" has an exact nonnegative integer value between 0 and 
2**DBL_MANT_DIG.  All such values are exactly representable as a double, and all the 
arithmetic operations involved are exact.  This holds right up to the ldexp call at the 
end.  So the arithmetic with x is essentially integer arithmetic.

I accept the code needs extra documentation;  I was planning to put the equivalent 
Python code into the comments to make things clearer.

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


More information about the Python-bugs-list mailing list