]> granicus.if.org Git - python/commitdiff
Decimal.sqrt(0) failed when the context was not
authorMark Dickinson <dickinsm@gmail.com>
Tue, 25 Mar 2008 14:35:25 +0000 (14:35 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Tue, 25 Mar 2008 14:35:25 +0000 (14:35 +0000)
explicitly supplied.

Lib/decimal.py
Lib/test/test_decimal.py
Misc/NEWS

index ebeb6d72081c5aef34f331e77c8c9712acdd8c0d..8b3e2526eef9c97dcdfaa64d699cb07c2b6cba56 100644 (file)
@@ -2316,6 +2316,9 @@ class Decimal(object):
 
     def sqrt(self, context=None):
         """Return the square root of self."""
+        if context is None:
+            context = getcontext()
+
         if self._is_special:
             ans = self._check_nans(context=context)
             if ans:
@@ -2329,9 +2332,6 @@ class Decimal(object):
             ans = _dec_from_triple(self._sign, '0', self._exp // 2)
             return ans._fix(context)
 
-        if context is None:
-            context = getcontext()
-
         if self._sign == 1:
             return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0')
 
index 34fc34ab9447feedb5d5dbbc389cf438588cee4f..efaa174301a8ae39d0b680828bd342c9d18f72a4 100644 (file)
@@ -1192,6 +1192,12 @@ class DecimalUsabilityTest(unittest.TestCase):
         d = d1.max(d2)
         self.assertTrue(type(d) is Decimal)
 
+    def test_implicit_context(self):
+        # Check results when context given implicitly.  (Issue 2478)
+        c = getcontext()
+        self.assertEqual(str(Decimal(0).sqrt()),
+                         str(c.sqrt(Decimal(0))))
+
 
 class DecimalPythonAPItests(unittest.TestCase):
 
index 3e95a44da9962d6f18b0a0c4c804816529bc7009..39b32cf0e70d8c30e06a670e3009dcf273ec57e6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,8 @@ Core and builtins
 Library
 -------
 
+- Issue #2478: fix failure of decimal.Decimal(0).sqrt()
+
 - Issue #2432: give DictReader the dialect and line_num attributes
   advertised in the docs.