summaryrefslogtreecommitdiffstats
path: root/Lib/_pydecimal.py
diff options
context:
space:
mode:
authorAndrew Nester <andrew.nester.dev@gmail.com>2017-02-14 18:22:55 (GMT)
committerMark Dickinson <mdickinson@enthought.com>2017-02-14 18:22:55 (GMT)
commit6d1dece06d13a7d40637e07b2c79f34aab368766 (patch)
tree3ca2a345299134cb312b3f6fbd9962e8e8dcdbb7 /Lib/_pydecimal.py
parentc33ee85b6fbed7f9c68e9fd39cd0582af9237ef1 (diff)
downloadcpython-6d1dece06d13a7d40637e07b2c79f34aab368766.zip
cpython-6d1dece06d13a7d40637e07b2c79f34aab368766.tar.gz
cpython-6d1dece06d13a7d40637e07b2c79f34aab368766.tar.bz2
Fixed #29534 - _decimal difference with _pydecimal (#65)
Diffstat (limited to 'Lib/_pydecimal.py')
-rw-r--r--Lib/_pydecimal.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py
index 0b40928..0fa152c 100644
--- a/Lib/_pydecimal.py
+++ b/Lib/_pydecimal.py
@@ -734,18 +734,23 @@ class Decimal(object):
"""
if isinstance(f, int): # handle integer inputs
- return cls(f)
- if not isinstance(f, float):
- raise TypeError("argument must be int or float.")
- if _math.isinf(f) or _math.isnan(f):
- return cls(repr(f))
- if _math.copysign(1.0, f) == 1.0:
- sign = 0
+ sign = 0 if f >= 0 else 1
+ k = 0
+ coeff = str(abs(f))
+ elif isinstance(f, float):
+ if _math.isinf(f) or _math.isnan(f):
+ return cls(repr(f))
+ 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
+ coeff = str(n*5**k)
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)
+ raise TypeError("argument must be int or float.")
+
+ result = _dec_from_triple(sign, coeff, -k)
if cls is Decimal:
return result
else: