mirror of https://github.com/python/cpython
Issue #2478: Decimal(sqrt(0)) failed when the decimal context
was not explicitly supplied.
This commit is contained in:
parent
f8f1fbd53c
commit
3b24ccbe7e
|
@ -2453,6 +2453,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:
|
||||
|
@ -2466,9 +2469,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')
|
||||
|
||||
|
|
|
@ -1315,6 +1315,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):
|
||||
|
||||
|
|
Loading…
Reference in New Issue