summaryrefslogtreecommitdiffstats
path: root/Lib/decimal.py
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2009-01-04 21:17:43 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2009-01-04 21:17:43 (GMT)
commitba298e4942168f08762d9ddbef32d6b08890d3c5 (patch)
tree53f0af69495685efbfcd6219d55a60a760b0d2fb /Lib/decimal.py
parent9d625c26c0c9610f9ddeaa69b09501da9bed1c3b (diff)
downloadcpython-ba298e4942168f08762d9ddbef32d6b08890d3c5.zip
cpython-ba298e4942168f08762d9ddbef32d6b08890d3c5.tar.gz
cpython-ba298e4942168f08762d9ddbef32d6b08890d3c5.tar.bz2
Merged revisions 68314 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r68314 | mark.dickinson | 2009-01-04 21:10:56 +0000 (Sun, 04 Jan 2009) | 5 lines 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. ........
Diffstat (limited to 'Lib/decimal.py')
-rw-r--r--Lib/decimal.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index 197269c..baff38b 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -655,7 +655,8 @@ class Decimal(_numbers.Real):
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):
"""Converts a float to a decimal number, exactly.
@@ -681,11 +682,18 @@ class Decimal(_numbers.Real):
return cls(f)
if _math.isinf(f) or _math.isnan(f): # raises TypeError if not a float
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()
k = d.bit_length() - 1
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):
"""Returns whether the number is not actually one.