[issue4796] Decimal to receive from_float method

Mark Dickinson report at bugs.python.org
Sat Jan 3 11:40:43 CET 2009


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

Instead of the repeated divisions and Inexact tests, how about a direct 
approach:  n/2**k = (n*5**k)/10**k, so something like:

    sign = 0 if copysign(1.0, self) == 1.0 else 1
    n, d = abs(self).as_integer_ratio()
    k = d.bit_length() - 1
    return _dec_from_triple(sign, str(n*5**k), -k)

should work, and would likely be faster too.

I also think the sign of 0 should be preserved:  i.e.,

>>> Decimal.from_float(-0.0)
Decimal('-0')

Am still reviewing---more comments to come.

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


More information about the Python-bugs-list mailing list