[issue4796] Decimal to receive from_float method
Mark Dickinson
report at bugs.python.org
Fri Jan 2 12:31:38 CET 2009
Mark Dickinson <dickinsm at gmail.com> added the comment:
> Accordingly, I recommend Decimal.from_float(f) with no qualifiers or
> optional arguments.
I agree with this. Since lossless conversion is feasible, this seems
the obvious way to go.
It's lucky that the exponent range for (binary) floats is relatively
small, though: for IEEE 754 floats the worst case conversions (e.g.,
(2**53-1)/2**1074) produce Decimals with something like 767 significant
digits, which is still plenty small enough not to cause difficulties.
The recipe in the docs is not industrial strength: it doesn't handle
infinities, nans and negative zero. I think there's a place for a
from_float method that handles these special cases correctly---or at
least as correctly as reasonable: +-infinity should convert to +-
infinity, nans to (quiet) nans. I don't think it's worth worrying about
preserving the sign or payload of a binary nan: just convert all float
nans to Decimal('NaN'). I do however think it's worth trying to
preserve the sign of negative zero. Note: I'm not suggesting changing
the *documentation* at all---the recipe there is fine as it is, IMO, but
I think an official version should include these corner cases.
----------
nosy: +marketdickinson
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4796>
_______________________________________
More information about the Python-bugs-list
mailing list