mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Fix Decimal.from_float to use valid Python 2.3 syntax, as per
comments at top of decimal.py. (But note that the from_float method itself with still not be usable before Python 2.7.) See issue 4796 for discussion.
This commit is contained in:
parent
1de3327015
commit
6a961637a8
1 changed files with 11 additions and 3 deletions
|
@ -654,7 +654,8 @@ class Decimal(object):
|
||||||
|
|
||||||
raise TypeError("Cannot convert %r to Decimal" % value)
|
raise TypeError("Cannot convert %r to Decimal" % value)
|
||||||
|
|
||||||
@classmethod
|
# @classmethod, but @decorator is not valid Python 2.3 syntax, so
|
||||||
|
# don't use it (see notes on Py2.3 compatibility at top of file)
|
||||||
def from_float(cls, f):
|
def from_float(cls, f):
|
||||||
"""Converts a float to a decimal number, exactly.
|
"""Converts a float to a decimal number, exactly.
|
||||||
|
|
||||||
|
@ -680,11 +681,18 @@ class Decimal(object):
|
||||||
return cls(f)
|
return cls(f)
|
||||||
if _math.isinf(f) or _math.isnan(f): # raises TypeError if not a float
|
if _math.isinf(f) or _math.isnan(f): # raises TypeError if not a float
|
||||||
return cls(repr(f))
|
return cls(repr(f))
|
||||||
sign = 0 if _math.copysign(1.0, f) == 1.0 else 1
|
if _math.copysign(1.0, f) == 1.0:
|
||||||
|
sign = 0
|
||||||
|
else:
|
||||||
|
sign = 1
|
||||||
n, d = abs(f).as_integer_ratio()
|
n, d = abs(f).as_integer_ratio()
|
||||||
k = d.bit_length() - 1
|
k = d.bit_length() - 1
|
||||||
result = _dec_from_triple(sign, str(n*5**k), -k)
|
result = _dec_from_triple(sign, str(n*5**k), -k)
|
||||||
return result if cls is Decimal else cls(result)
|
if cls is Decimal:
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
return cls(result)
|
||||||
|
from_float = classmethod(from_float)
|
||||||
|
|
||||||
def _isnan(self):
|
def _isnan(self):
|
||||||
"""Returns whether the number is not actually one.
|
"""Returns whether the number is not actually one.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue