]> granicus.if.org Git - python/commitdiff
Fixed #29534 - _decimal difference with _pydecimal (#65)
authorAndrew Nester <andrew.nester.dev@gmail.com>
Tue, 14 Feb 2017 18:22:55 +0000 (21:22 +0300)
committerMark Dickinson <mdickinson@enthought.com>
Tue, 14 Feb 2017 18:22:55 +0000 (18:22 +0000)
Lib/_pydecimal.py
Lib/test/test_decimal.py
Misc/NEWS

index 0b40928ff8d109dd49d3347da06a4bca91e8d6f3..0fa152c2a6f8ef05f476dac2c00048a5251a8133 100644 (file)
@@ -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:
index 59a17af278c05277303ac9cacea050f96d165b8a..48360d1baedb641b444fbbbbb1deffba31c0d3cd 100644 (file)
@@ -1185,6 +1185,16 @@ class FormatTest(unittest.TestCase):
         self.assertEqual(format(Decimal('100000000.123'), 'n'),
                          '100\u066c000\u066c000\u066b123')
 
+    def test_decimal_from_float_argument_type(self):
+        class A(self.decimal.Decimal):
+            def __init__(self, a):
+                self.a_type = type(a)
+        a = A.from_float(42.5)
+        self.assertEqual(self.decimal.Decimal, a.a_type)
+
+        a = A.from_float(42)
+        self.assertEqual(self.decimal.Decimal, a.a_type)
+
 class CFormatTest(FormatTest):
     decimal = C
 class PyFormatTest(FormatTest):
index 8786f19e47b56c2a956294331f72e897f513f676..e6df0144b1061e51c582737dbaa05ddc2396c01b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 1?
 Core and Builtins
 -----------------
 
+- bpo-29534: Fixed different behaviour of Decimal.from_float() for _decimal and _pydecimal.
+
 - bpo-29438: Fixed use-after-free problem in key sharing dict.
 
 - Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].