Message78781
> 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. |
|
Date |
User |
Action |
Args |
2009-01-02 11:31:39 | mark.dickinson | set | recipients:
+ mark.dickinson, rhettinger, steven.daprano |
2009-01-02 11:31:38 | mark.dickinson | set | messageid: <1230895898.96.0.199448932616.issue4796@psf.upfronthosting.co.za> |
2009-01-02 11:31:38 | mark.dickinson | link | issue4796 messages |
2009-01-02 11:31:37 | mark.dickinson | create | |
|