Lines Matching full:decimal
11 These are the test cases for the Decimal module.
14 the Decimal arithmetic using the tests provided by Mike Cowlishaw. The latter
19 http://speleotrove.com/decimal/dectest.zip
44 C = import_fresh_module('decimal', fresh=['_decimal'])
45 P = import_fresh_module('decimal', blocked=['_decimal'])
46 orig_sys_decimal = sys.modules['decimal']
48 # fractions module must import the correct decimal module.
50 sys.modules['decimal'] = P
52 sys.modules['decimal'] = C
54 sys.modules['decimal'] = orig_sys_decimal
128 """Class which tests the Decimal class against the IBM test cases."""
131 self.context = self.decimal.Context()
132 self.readcontext = self.decimal.Context()
147 # Decimal behaves differently from decNumber for these testcases so these
173 if self.decimal == C:
193 # Name adapter to be able to change the Decimal and Context
244 self.ErrorNames = {'clamped' : self.decimal.Clamped,
245 'conversion_syntax' : self.decimal.InvalidOperation,
246 'division_by_zero' : self.decimal.DivisionByZero,
247 'division_impossible' : self.decimal.InvalidOperation,
248 'division_undefined' : self.decimal.InvalidOperation,
249 'inexact' : self.decimal.Inexact,
250 'invalid_context' : self.decimal.InvalidOperation,
251 'invalid_operation' : self.decimal.InvalidOperation,
252 'overflow' : self.decimal.Overflow,
253 'rounded' : self.decimal.Rounded,
254 'subnormal' : self.decimal.Subnormal,
255 'underflow' : self.decimal.Underflow}
258 # Decimal instance.
277 if self.decimal == C and self.decimal.MAX_EMAX == 425000000:
283 return self.decimal.Decimal(v, context)
295 except self.decimal.DecimalException as exception:
350 raise self.decimal.InvalidOperation
369 for exception in Signals[self.decimal]:
391 except Signals[self.decimal] as e:
411 except Signals[self.decimal] as e:
419 ordered_errors = [e for e in OrderedSignals[self.decimal] if e in theirexceptions]
426 except Signals[self.decimal] as e:
442 except Signals[self.decimal] as error:
460 return [e for e in Signals[self.decimal] if self.context.flags[e]]
463 if self.decimal == C and self.decimal.MAX_PREC == 425000000:
470 if self.decimal == C and self.decimal.MAX_PREC == 425000000:
475 if self.decimal == C and self.decimal.MAX_PREC == 425000000:
483 decimal = C variable in CIBMTestCases
485 decimal = P variable in PyIBMTestCases
487 # The following classes test the behaviour of Decimal according to PEP 327
490 '''Unit tests for Explicit Construction cases of Decimal.'''
493 Decimal = self.decimal.Decimal
494 self.assertEqual(Decimal(), Decimal("0"))
497 Decimal = self.decimal.Decimal
498 self.assertRaises(TypeError, Decimal, None)
501 Decimal = self.decimal.Decimal
504 d = Decimal(45)
508 d = Decimal(500000123)
512 d = Decimal(-45)
516 d = Decimal(0)
524 d = Decimal(i)
528 Decimal = self.decimal.Decimal
529 InvalidOperation = self.decimal.InvalidOperation
530 localcontext = self.decimal.localcontext
533 self.assertEqual(str(Decimal('')), 'NaN')
536 self.assertEqual(str(Decimal('45')), '45')
539 self.assertEqual(str(Decimal('45.34')), '45.34')
542 self.assertEqual(str(Decimal('45e2')), '4.5E+3')
545 self.assertEqual(str(Decimal('ugly')), 'NaN')
548 self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4')
549 self.assertEqual(str(Decimal(' -7.89')), '-7.89')
550 self.assertEqual(str(Decimal(" 3.45679 ")), '3.45679')
553 self.assertEqual(str(Decimal('1_3.3e4_0')), '1.33E+41')
554 self.assertEqual(str(Decimal('1_0_0_0')), '1000')
559 self.assertEqual(str(Decimal(lead + '9.311E+28' + trail)),
565 self.assertRaises(InvalidOperation, Decimal, "xyz")
567 self.assertRaises(TypeError, Decimal, "1234", "x", "y")
570 self.assertRaises(InvalidOperation, Decimal, "1\u00a02\u00a03")
571 self.assertRaises(InvalidOperation, Decimal, "\u00a01\u00a02\u00a0")
574 self.assertRaises(InvalidOperation, Decimal, "\u00a0")
575 self.assertRaises(InvalidOperation, Decimal, "\u00a0\u00a0")
578 self.assertRaises(InvalidOperation, Decimal, "12\u00003")
581 self.assertRaises(InvalidOperation, Decimal, "1_2_\u00003")
586 Decimal = self.decimal.Decimal
587 context = self.decimal.Context()
590 self.assertEqual(str(Decimal(s)), '9.999999')
594 Decimal = self.decimal.Decimal
597 d = Decimal( (0, (0,), 0) )
601 d = Decimal( (1, (4, 5), 0) )
605 d = Decimal( (0, (4, 5, 3, 4), -2) )
609 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
613 d = Decimal( (0, (), "F") )
617 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1)) )
620 self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) )
621 self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) )
622 self.assertRaises(ValueError, Decimal, (Decimal(1), (4, 3, 4, 9, 1), 2))
625 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') )
626 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) )
627 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), '1') )
630 self.assertRaises(ValueError, Decimal, (1, "xyz", 2) )
631 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, None, 1), 2) )
632 self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) )
633 self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) )
634 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) )
637 Decimal = self.decimal.Decimal
639 d = Decimal([0, [0], 0])
642 d = Decimal([1, [4, 3, 4, 9, 1, 3, 5, 3, 4], -25])
645 d = Decimal([1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25])
648 d = Decimal((1, [4, 3, 4, 9, 1, 3, 5, 3, 4], -25))
652 Decimal = self.decimal.Decimal
654 self.assertIs(bool(Decimal(0)), False)
655 self.assertIs(bool(Decimal(1)), True)
656 self.assertEqual(Decimal(False), Decimal(0))
657 self.assertEqual(Decimal(True), Decimal(1))
660 Decimal = self.decimal.Decimal
663 d = Decimal(45)
664 e = Decimal(d)
668 d = Decimal(500000123)
669 e = Decimal(d)
673 d = Decimal(-45)
674 e = Decimal(d)
678 d = Decimal(0)
679 e = Decimal(d)
685 Decimal = self.decimal.Decimal
687 r = Decimal(0.1)
688 self.assertEqual(type(r), Decimal)
691 self.assertTrue(Decimal(float('nan')).is_qnan())
692 self.assertTrue(Decimal(float('inf')).is_infinite())
693 self.assertTrue(Decimal(float('-inf')).is_infinite())
694 self.assertEqual(str(Decimal(float('nan'))),
695 str(Decimal('NaN')))
696 self.assertEqual(str(Decimal(float('inf'))),
697 str(Decimal('Infinity')))
698 self.assertEqual(str(Decimal(float('-inf'))),
699 str(Decimal('-Infinity')))
700 self.assertEqual(str(Decimal(float('-0.0'))),
701 str(Decimal('-0')))
704 self.assertEqual(x, float(Decimal(x))) # roundtrip
707 Decimal = self.decimal.Decimal
708 InvalidOperation = self.decimal.InvalidOperation
709 Rounded = self.decimal.Rounded
711 nc = copy.copy(self.decimal.getcontext())
715 d = Decimal()
725 self.assertIsInstance(d, Decimal)
730 d = Decimal('456789')
740 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
745 # from Decimal
746 prevdec = Decimal(500000123)
747 d = Decimal(prevdec)
759 self.assertTrue(isinstance(d, Decimal))
785 Decimal('NaN12345'))
792 self.assertEqual(str(nc.create_decimal(Decimal('NaN12345'))), 'NaN')
797 Decimal = self.decimal.Decimal
799 nc = self.decimal.Context()
801 self.assertEqual(type(r), Decimal)
820 Decimal = self.decimal.Decimal
828 self.assertEqual(str(Decimal(input)), expected)
831 decimal = C variable in CExplicitConstructionTest
833 decimal = P variable in PyExplicitConstructionTest
836 '''Unit tests for Implicit Construction cases of Decimal.'''
839 Decimal = self.decimal.Decimal
840 self.assertRaises(TypeError, eval, 'Decimal(5) + None', locals())
843 Decimal = self.decimal.Decimal
846 self.assertEqual(str(Decimal(5) + 45), '50')
848 self.assertEqual(Decimal(5) + 123456789000, Decimal(123456789000))
851 Decimal = self.decimal.Decimal
852 self.assertRaises(TypeError, eval, 'Decimal(5) + "3"', locals())
855 Decimal = self.decimal.Decimal
856 self.assertRaises(TypeError, eval, 'Decimal(5) + 2.2', locals())
859 Decimal = self.decimal.Decimal
860 self.assertEqual(Decimal(5) + Decimal(45), Decimal(50))
863 Decimal = self.decimal.Decimal
884 self.assertEqual(divmod(E(), Decimal(10)), 'divmod 10')
885 self.assertEqual(divmod(Decimal(10), E()), '10 rdivmod')
886 self.assertEqual(eval('Decimal(10) < E()'), 'gt 10')
887 self.assertEqual(eval('Decimal(10) > E()'), 'lt 10')
888 self.assertEqual(eval('Decimal(10) <= E()'), 'ge 10')
889 self.assertEqual(eval('Decimal(10) >= E()'), 'le 10')
890 self.assertEqual(eval('Decimal(10) == E()'), 'eq 10')
891 self.assertEqual(eval('Decimal(10) != E()'), 'ne 10')
907 self.assertEqual(eval('E()' + sym + 'Decimal(10)'),
909 self.assertEqual(eval('Decimal(10)' + sym + 'E()'),
913 decimal = C variable in CImplicitConstructionTest
915 decimal = P variable in PyImplicitConstructionTest
920 Decimal = self.decimal.Decimal
922 # triples giving a format, a Decimal, and the expected result
958 ('.0f', '0', '0'), # no decimal point
1073 self.assertEqual(format(Decimal(d), fmt), result)
1076 self.assertRaises(TypeError, Decimal(1).__format__, b'-020')
1079 Decimal = self.decimal.Decimal
1087 return ''.join([chr(x) for x in lst]) if self.decimal == C else lst
1090 if self.decimal == C:
1091 return Decimal(x).__format__(fmt, override)
1093 return Decimal(x).__format__(fmt, _localeconv=override)
1126 self.assertEqual(get_fmt(Decimal('12.7'), en_US), '12.7')
1127 self.assertEqual(get_fmt(Decimal('12.7'), fr_FR), '12,7')
1128 self.assertEqual(get_fmt(Decimal('12.7'), ru_RU), '12,7')
1129 self.assertEqual(get_fmt(Decimal('12.7'), crazy), '1-2&7')
1163 # wide char separator and decimal point
1164 self.assertEqual(get_fmt(Decimal('-1.5'), dotsep_wide, '020n'),
1169 # locale with wide char separator and decimal point
1170 Decimal = self.decimal.Decimal
1175 self.skipTest('inappropriate decimal point separator '
1181 self.assertEqual(format(Decimal('100000000.123'), 'n'),
1185 class A(self.decimal.Decimal):
1189 self.assertEqual(self.decimal.Decimal, a.a_type)
1192 self.assertEqual(self.decimal.Decimal, a.a_type)
1195 decimal = C variable in CFormatTest
1197 decimal = P variable in PyFormatTest
1203 Decimal = self.decimal.Decimal
1205 d1 = Decimal('-11.1')
1206 d2 = Decimal('22.2')
1209 self.assertEqual(d1+d2, Decimal('11.1'))
1210 self.assertEqual(d2+d1, Decimal('11.1'))
1214 self.assertEqual(c, Decimal('-6.1'))
1219 self.assertEqual(c, Decimal('-6.1'))
1222 #inline with decimal
1224 self.assertEqual(d1, Decimal('11.1'))
1228 self.assertEqual(d1, Decimal('16.1'))
1231 Decimal = self.decimal.Decimal
1233 d1 = Decimal('-11.1')
1234 d2 = Decimal('22.2')
1237 self.assertEqual(d1-d2, Decimal('-33.3'))
1238 self.assertEqual(d2-d1, Decimal('33.3'))
1242 self.assertEqual(c, Decimal('-16.1'))
1247 self.assertEqual(c, Decimal('16.1'))
1250 #inline with decimal
1252 self.assertEqual(d1, Decimal('-33.3'))
1256 self.assertEqual(d1, Decimal('-38.3'))
1259 Decimal = self.decimal.Decimal
1261 d1 = Decimal('-5')
1262 d2 = Decimal('3')
1265 self.assertEqual(d1*d2, Decimal('-15'))
1266 self.assertEqual(d2*d1, Decimal('-15'))
1270 self.assertEqual(c, Decimal('-25'))
1275 self.assertEqual(c, Decimal('-25'))
1278 #inline with decimal
1280 self.assertEqual(d1, Decimal('-15'))
1284 self.assertEqual(d1, Decimal('-75'))
1287 Decimal = self.decimal.Decimal
1289 d1 = Decimal('-5')
1290 d2 = Decimal('2')
1293 self.assertEqual(d1/d2, Decimal('-2.5'))
1294 self.assertEqual(d2/d1, Decimal('-0.4'))
1298 self.assertEqual(c, Decimal('-1.25'))
1303 self.assertEqual(c, Decimal('-0.8'))
1306 #inline with decimal
1308 self.assertEqual(d1, Decimal('-2.5'))
1312 self.assertEqual(d1, Decimal('-0.625'))
1315 Decimal = self.decimal.Decimal
1317 d1 = Decimal('5')
1318 d2 = Decimal('2')
1321 self.assertEqual(d1//d2, Decimal('2'))
1322 self.assertEqual(d2//d1, Decimal('0'))
1326 self.assertEqual(c, Decimal('1'))
1331 self.assertEqual(c, Decimal('1'))
1334 #inline with decimal
1336 self.assertEqual(d1, Decimal('2'))
1340 self.assertEqual(d1, Decimal('1'))
1343 Decimal = self.decimal.Decimal
1345 d1 = Decimal('5')
1346 d2 = Decimal('2')
1349 self.assertEqual(d1**d2, Decimal('25'))
1350 self.assertEqual(d2**d1, Decimal('32'))
1354 self.assertEqual(c, Decimal('625'))
1359 self.assertEqual(c, Decimal('16807'))
1362 #inline with decimal
1364 self.assertEqual(d1, Decimal('25'))
1368 self.assertEqual(d1, Decimal('390625'))
1371 Decimal = self.decimal.Decimal
1373 d1 = Decimal('5')
1374 d2 = Decimal('2')
1377 self.assertEqual(d1%d2, Decimal('1'))
1378 self.assertEqual(d2%d1, Decimal('2'))
1382 self.assertEqual(c, Decimal('1'))
1387 self.assertEqual(c, Decimal('2'))
1390 #inline with decimal
1392 self.assertEqual(d1, Decimal('1'))
1396 self.assertEqual(d1, Decimal('1'))
1399 Decimal = self.decimal.Decimal
1401 d1 = Decimal('5')
1402 d2 = Decimal('2')
1406 self.assertEqual(p, Decimal('2'))
1407 self.assertEqual(q, Decimal('1'))
1413 self.assertEqual(p, Decimal('1'))
1414 self.assertEqual(q, Decimal('1'))
1420 self.assertEqual(p, Decimal('1'))
1421 self.assertEqual(q, Decimal('2'))
1426 Decimal = self.decimal.Decimal
1428 self.assertEqual(+Decimal(45), Decimal(+45)) # +
1429 self.assertEqual(-Decimal(45), Decimal(-45)) # -
1430 self.assertEqual(abs(Decimal(45)), abs(Decimal(-45))) # abs
1440 Decimal = self.decimal.Decimal
1441 InvalidOperation = self.decimal.InvalidOperation
1442 localcontext = self.decimal.localcontext
1444 n = Decimal('NaN')
1445 s = Decimal('sNaN')
1446 i = Decimal('Inf')
1447 f = Decimal('2')
1488 Decimal = self.decimal.Decimal
1490 d = Decimal(1).copy_sign(Decimal(-2))
1491 self.assertEqual(Decimal(1).copy_sign(-2), d)
1492 self.assertRaises(TypeError, Decimal(1).copy_sign, '-2')
1495 decimal = C variable in CArithmeticOperatorsTest
1497 decimal = P variable in PyArithmeticOperatorsTest
1502 Decimal = cls.decimal.Decimal
1503 InvalidOperation = cls.decimal.InvalidOperation
1504 DivisionByZero = cls.decimal.DivisionByZero
1505 Overflow = cls.decimal.Overflow
1506 Underflow = cls.decimal.Underflow
1507 Inexact = cls.decimal.Inexact
1508 getcontext = cls.decimal.getcontext
1509 localcontext = cls.decimal.localcontext
1511 d1 = Decimal(1)
1512 d3 = Decimal(3)
1526 cls.assertRaises(InvalidOperation, c3.compare, d1, Decimal('sNaN'))
1532 cls.assertEqual(test1, Decimal('0.333333333333333333333333'))
1533 cls.assertEqual(test2, Decimal('0.333333333333333333333333'))
1541 Decimal = cls.decimal.Decimal
1542 InvalidOperation = cls.decimal.InvalidOperation
1543 DivisionByZero = cls.decimal.DivisionByZero
1544 Overflow = cls.decimal.Overflow
1545 Underflow = cls.decimal.Underflow
1546 Inexact = cls.decimal.Inexact
1547 getcontext = cls.decimal.getcontext
1548 localcontext = cls.decimal.localcontext
1550 d1 = Decimal(1)
1551 d3 = Decimal(3)
1560 cls.assertRaises(Overflow, c2.multiply, Decimal('1e425000000'), 999)
1566 cls.assertRaises(Underflow, c3.divide, Decimal('1e-425000000'), 999)
1576 cls.assertEqual(test1, Decimal('0.333333333333333333333333'))
1577 cls.assertEqual(test2, Decimal('0.333333333333333333'))
1585 '''Unit tests for thread local contexts in Decimal.'''
1591 DefaultContext = self.decimal.DefaultContext
1593 if self.decimal == C and not self.decimal.HAVE_THREADS:
1618 for sig in Signals[self.decimal]:
1630 decimal = C variable in CThreadingTest
1633 decimal = P variable in PyThreadingTest
1636 '''Unit tests for Usability cases of Decimal.'''
1640 Decimal = self.decimal.Decimal
1642 da = Decimal('23.42')
1643 db = Decimal('23.42')
1644 dc = Decimal('45')
1656 #a Decimal and an int
1661 #a Decimal and uncomparable
1668 a = list(map(Decimal, range(100)))
1675 Decimal = self.decimal.Decimal
1677 da = Decimal('0.25')
1678 db = Decimal('3.0')
1691 self.assertNotEqual(0.1, Decimal('0.1'))
1694 Decimal = self.decimal.Decimal
1696 da = Decimal('0.25')
1697 db = Decimal('3.0')
1714 D = self.decimal.Decimal
1715 F = fractions[self.decimal].Fraction
1716 Context = self.decimal.Context
1717 localcontext = self.decimal.localcontext
1718 InvalidOperation = self.decimal.InvalidOperation
1756 Decimal = self.decimal.Decimal
1758 d = Decimal('43.24')
1766 Decimal = self.decimal.Decimal
1767 localcontext = self.decimal.localcontext
1776 hashit(Decimal(23))
1777 hashit(Decimal('Infinity'))
1778 hashit(Decimal('-Infinity'))
1779 hashit(Decimal('nan123'))
1780 hashit(Decimal('-NaN'))
1782 test_values = [Decimal(sign*(2**m + n))
1788 Decimal("-1"), # ==> -2
1789 Decimal("-0"), # zeros
1790 Decimal("0.00"),
1791 Decimal("-0.000"),
1792 Decimal("0E10"),
1793 Decimal("-0E12"),
1794 Decimal("10.0"), # negative exponent
1795 Decimal("-23.00000"),
1796 Decimal("1230E100"), # positive exponent
1797 Decimal("-4.5678E50"),
1800 Decimal(2**64 + 2**32 - 1),
1803 Decimal("1.634E100"),
1804 Decimal("90.697E100"),
1805 Decimal("188.83E100"),
1806 Decimal("1652.9E100"),
1807 Decimal("56531E100"),
1815 self.assertEqual(hashit(Decimal(23)), hashit(23))
1816 self.assertRaises(TypeError, hash, Decimal('sNaN'))
1817 self.assertTrue(hashit(Decimal('Inf')))
1818 self.assertTrue(hashit(Decimal('-Inf')))
1820 # check that the hashes of a Decimal float match when they
1826 d = Decimal(s)
1832 x = Decimal("123456789.1")
1846 self.assertEqual(hashit(Decimal(x)), hashit(x))
1849 Decimal = self.decimal.Decimal
1851 d1 = Decimal('15.32')
1852 d2 = Decimal('28.5')
1862 #between Decimal and int
1869 Decimal = self.decimal.Decimal
1872 self.assertFalse(Decimal(0))
1874 self.assertTrue(Decimal('0.372'))
1878 Decimal = self.decimal.Decimal
1880 d = Decimal('15.32')
1882 self.assertEqual(repr(d), "Decimal('15.32')") # repr
1886 Decimal = self.decimal.Decimal
1888 d1 = Decimal('66')
1889 d2 = Decimal('15.32')
1912 self.assertEqual(math.floor(Decimal(d)), i)
1913 self.assertRaises(ValueError, math.floor, Decimal('-NaN'))
1914 self.assertRaises(ValueError, math.floor, Decimal('sNaN'))
1915 self.assertRaises(ValueError, math.floor, Decimal('NaN123'))
1916 self.assertRaises(OverflowError, math.floor, Decimal('Inf'))
1917 self.assertRaises(OverflowError, math.floor, Decimal('-Inf'))
1932 self.assertEqual(math.ceil(Decimal(d)), i)
1933 self.assertRaises(ValueError, math.ceil, Decimal('-NaN'))
1934 self.assertRaises(ValueError, math.ceil, Decimal('sNaN'))
1935 self.assertRaises(ValueError, math.ceil, Decimal('NaN123'))
1936 self.assertRaises(OverflowError, math.ceil, Decimal('Inf'))
1937 self.assertRaises(OverflowError, math.ceil, Decimal('-Inf'))
1959 self.assertEqual(round(Decimal(d)), i)
1960 self.assertRaises(ValueError, round, Decimal('-NaN'))
1961 self.assertRaises(ValueError, round, Decimal('sNaN'))
1962 self.assertRaises(ValueError, round, Decimal('NaN123'))
1963 self.assertRaises(OverflowError, round, Decimal('Inf'))
1964 self.assertRaises(OverflowError, round, Decimal('-Inf'))
1985 self.assertEqual(str(round(Decimal(d), n)), r)
1988 # Test conversions of decimal NANs to float.
1990 Decimal = self.decimal.Decimal
1992 f = float(Decimal(s))
1998 Decimal = self.decimal.Decimal
2000 d = Decimal(s)
2004 Decimal = self.decimal.Decimal
2007 d = Decimal( (0, (0,), 0) )
2011 d = Decimal( (1, (4, 5), 0) )
2015 d = Decimal( (0, (4, 5, 3, 4), -2) )
2019 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
2023 Decimal = self.decimal.Decimal
2026 d = Decimal(0)
2030 d = Decimal(-45)
2034 d = Decimal("-4.34913534E-17")
2037 # The '0' coefficient is implementation specific to decimal.py.
2039 d = Decimal("Infinity")
2043 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) )
2045 d = Decimal( (1, (0, 0, 0), 37) )
2047 d = Decimal( (1, (), 37) )
2051 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') )
2053 d = Decimal( (1, (0, 0, 0), 'N') )
2055 d = Decimal( (1, (), 'n') )
2058 # For infinities, decimal.py has always silently accepted any
2060 d = Decimal( (0, (0,), 'F') )
2062 d = Decimal( (0, (4, 5, 3, 4), 'F') )
2064 d = Decimal( (1, (0, 2, 7, 1), 'F') )
2068 Decimal = self.decimal.Decimal
2072 Decimal.as_integer_ratio, Decimal('inf'))
2074 Decimal.as_integer_ratio, Decimal('-inf'))
2076 Decimal.as_integer_ratio, Decimal('-nan'))
2078 Decimal.as_integer_ratio, Decimal('snan123'))
2083 d = Decimal('%s%dE%d' % (sign, coeff, exp))
2098 self.assertEqual(Decimal(p) / Decimal(q), d)
2101 # Different behaviours when subclassing Decimal
2102 Decimal = self.decimal.Decimal
2104 class MyDecimal(Decimal):
2110 self.assertIs(type(d), Decimal)
2113 self.assertIs(type(d), Decimal)
2123 # Decimal(Decimal)
2124 d = Decimal('1.0')
2125 x = Decimal(d)
2126 self.assertIs(type(x), Decimal)
2129 # MyDecimal(Decimal)
2135 # Decimal(MyDecimal)
2136 x = Decimal(m)
2137 self.assertIs(type(x), Decimal)
2148 Decimal = self.decimal.Decimal
2149 getcontext = self.decimal.getcontext
2153 self.assertEqual(str(Decimal(0).sqrt()),
2154 str(c.sqrt(Decimal(0))))
2157 Decimal = self.decimal.Decimal
2158 Context = self.decimal.Context
2159 localcontext = self.decimal.localcontext
2160 InvalidOperation = self.decimal.InvalidOperation
2161 DivisionByZero = self.decimal.DivisionByZero
2162 Overflow = self.decimal.Overflow
2163 Underflow = self.decimal.Underflow
2164 Subnormal = self.decimal.Subnormal
2165 Inexact = self.decimal.Inexact
2166 Rounded = self.decimal.Rounded
2167 Clamped = self.decimal.Clamped
2174 x = Decimal("111")
2175 y = Decimal("1e9999")
2176 z = Decimal("1e-9999")
2195 self.assertRaises(InvalidOperation, Decimal(-1).ln, context=None)
2203 self.assertRaises(InvalidOperation, Decimal(-1).log10, context=None)
2208 self.assertRaises(DivisionByZero, Decimal(0).logb, context=None)
2218 self.assertRaises(InvalidOperation, Decimal('sNaN').next_minus, context=None)
2223 self.assertRaises(InvalidOperation, Decimal('sNaN').next_plus, context=None)
2251 ans = str(x.compare(Decimal('Nan891287828'), context=None))
2253 self.assertRaises(InvalidOperation, x.compare, Decimal('sNaN'), context=None)
2259 self.assertRaises(InvalidOperation, x.compare_signal, Decimal('NaN'), context=None)
2283 self.assertRaises(InvalidOperation, x.max, Decimal('sNaN'), context=None)
2289 self.assertRaises(InvalidOperation, x.max_mag, Decimal('sNaN'), context=None)
2295 self.assertRaises(InvalidOperation, x.min, Decimal('sNaN'), context=None)
2301 self.assertRaises(InvalidOperation, x.min_mag, Decimal('sNaN'), context=None)
2333 self.assertRaises(Overflow, x.fma, Decimal('1e9999'), 3, context=None)
2339 ans = str(Decimal('1.5').to_integral(rounding=None, context=None))
2342 ans = str(Decimal('1.5').to_integral(rounding=None, context=None))
2344 ans = str(Decimal('1.5').to_integral(rounding=ROUND_UP, context=None))
2347 self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral, context=None)
2351 ans = str(Decimal('1.5').to_integral_value(rounding=None, context=None))
2354 ans = str(Decimal('1.5').to_integral_value(rounding=None, context=None))
2356 ans = str(Decimal('1.5').to_integral_value(rounding=ROUND_UP, context=None))
2359 self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral_value, context=None)
2363 ans = str(Decimal('1.5').to_integral_exact(rounding=None, context=None))
2366 ans = str(Decimal('1.5').to_integral_exact(rounding=None, context=None))
2368 ans = str(Decimal('1.5').to_integral_exact(rounding=ROUND_UP, context=None))
2371 self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral_exact, context=None)
2375 ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=None, context=None))
2378 ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=None, context=None))
2380 … ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=ROUND_UP, context=None))
2383 … self.assertRaises(InvalidOperation, y.quantize, Decimal('1e-10'), rounding=ROUND_UP, context=None)
2396 # Check that methods taking a second Decimal argument will
2397 # always accept an integer in place of a Decimal.
2398 Decimal = self.decimal.Decimal
2400 self.assertEqual(Decimal(4).compare(3),
2401 Decimal(4).compare(Decimal(3)))
2402 self.assertEqual(Decimal(4).compare_signal(3),
2403 Decimal(4).compare_signal(Decimal(3)))
2404 self.assertEqual(Decimal(4).compare_total(3),
2405 Decimal(4).compare_total(Decimal(3)))
2406 self.assertEqual(Decimal(4).compare_total_mag(3),
2407 Decimal(4).compare_total_mag(Decimal(3)))
2408 self.assertEqual(Decimal(10101).logical_and(1001),
2409 Decimal(10101).logical_and(Decimal(1001)))
2410 self.assertEqual(Decimal(10101).logical_or(1001),
2411 Decimal(10101).logical_or(Decimal(1001)))
2412 self.assertEqual(Decimal(10101).logical_xor(1001),
2413 Decimal(10101).logical_xor(Decimal(1001)))
2414 self.assertEqual(Decimal(567).max(123),
2415 Decimal(567).max(Decimal(123)))
2416 self.assertEqual(Decimal(567).max_mag(123),
2417 Decimal(567).max_mag(Decimal(123)))
2418 self.assertEqual(Decimal(567).min(123),
2419 Decimal(567).min(Decimal(123)))
2420 self.assertEqual(Decimal(567).min_mag(123),
2421 Decimal(567).min_mag(Decimal(123)))
2422 self.assertEqual(Decimal(567).next_toward(123),
2423 Decimal(567).next_toward(Decimal(123)))
2424 self.assertEqual(Decimal(1234).quantize(100),
2425 Decimal(1234).quantize(Decimal(100)))
2426 self.assertEqual(Decimal(768).remainder_near(1234),
2427 Decimal(768).remainder_near(Decimal(1234)))
2428 self.assertEqual(Decimal(123).rotate(1),
2429 Decimal(123).rotate(Decimal(1)))
2430 self.assertEqual(Decimal(1234).same_quantum(1000),
2431 Decimal(1234).same_quantum(Decimal(1000)))
2432 self.assertEqual(Decimal('9.123').scaleb(-100),
2433 Decimal('9.123').scaleb(Decimal(-100)))
2434 self.assertEqual(Decimal(456).shift(-1),
2435 Decimal(456).shift(Decimal(-1)))
2437 self.assertEqual(Decimal(-12).fma(Decimal(45), 67),
2438 Decimal(-12).fma(Decimal(45), Decimal(67)))
2439 self.assertEqual(Decimal(-12).fma(45, 67),
2440 Decimal(-12).fma(Decimal(45), Decimal(67)))
2441 self.assertEqual(Decimal(-12).fma(45, Decimal(67)),
2442 Decimal(-12).fma(Decimal(45), Decimal(67)))
2445 decimal = C variable in CUsabilityTest
2447 decimal = P variable in PyUsabilityTest
2452 Decimal = self.decimal.Decimal
2454 self.assertTrue(issubclass(Decimal, numbers.Number))
2455 self.assertFalse(issubclass(Decimal, numbers.Real))
2456 self.assertIsInstance(Decimal(0), numbers.Number)
2457 self.assertNotIsInstance(Decimal(0), numbers.Real)
2461 Decimal = self.decimal.Decimal
2463 savedecimal = sys.modules['decimal']
2466 sys.modules['decimal'] = self.decimal
2467 d = Decimal('-3.141590000')
2474 x = C.Decimal('-3.123e81723')
2475 y = P.Decimal('-3.123e81723')
2477 sys.modules['decimal'] = C
2479 sys.modules['decimal'] = P
2481 self.assertIsInstance(r, P.Decimal)
2484 sys.modules['decimal'] = P
2486 sys.modules['decimal'] = C
2488 self.assertIsInstance(r, C.Decimal)
2491 x = C.Decimal('-3.123e81723').as_tuple()
2492 y = P.Decimal('-3.123e81723').as_tuple()
2494 sys.modules['decimal'] = C
2496 sys.modules['decimal'] = P
2501 sys.modules['decimal'] = P
2503 sys.modules['decimal'] = C
2508 sys.modules['decimal'] = savedecimal
2511 Decimal = self.decimal.Decimal
2516 self.assertEqual(int(Decimal(s)), int(float(s)))
2518 d = Decimal(s)
2520 self.assertEqual(Decimal(int(d)), r)
2522 self.assertRaises(ValueError, int, Decimal('-nan'))
2523 self.assertRaises(ValueError, int, Decimal('snan'))
2524 self.assertRaises(OverflowError, int, Decimal('inf'))
2525 self.assertRaises(OverflowError, int, Decimal('-inf'))
2528 Decimal = self.decimal.Decimal
2533 self.assertEqual(int(Decimal(s)), int(float(s)))
2535 d = Decimal(s)
2537 self.assertEqual(Decimal(math.trunc(d)), r)
2541 Decimal = self.decimal.Decimal
2543 class MyDecimal(Decimal):
2547 self.assertTrue(issubclass(MyDecimal, Decimal))
2561 str(Decimal('NaN')))
2563 str(Decimal('Infinity')))
2565 str(Decimal('-Infinity')))
2572 Decimal = self.decimal.Decimal
2573 Context = self.decimal.Context
2574 Inexact = self.decimal.Inexact
2579 Decimal('3.1415')
2584 Decimal('3.1416')
2593 "Decimal('-0')")
2595 "Decimal('1')")
2597 "Decimal('10')")
2600 Decimal = self.decimal.Decimal
2601 Context = self.decimal.Context
2602 InvalidOperation = self.decimal.InvalidOperation
2606 Decimal('7.335').quantize(Decimal('.01')),
2607 Decimal('7.34')
2610 Decimal('7.335').quantize(Decimal('.01'), rounding=ROUND_DOWN),
2611 Decimal('7.33')
2615 Decimal("10e99999").quantize, Decimal('1e100000'), context=c
2619 d = Decimal("0.871831e800")
2620 x = d.quantize(context=c, exp=Decimal("1e797"), rounding=ROUND_DOWN)
2621 self.assertEqual(x, Decimal('8.71E+799'))
2624 Decimal = self.decimal.Decimal
2626 x = Decimal("9.8182731e181273")
2631 x = Decimal("1")
2640 D = self.decimal.Decimal
2641 Context = self.decimal.Context
2642 localcontext = self.decimal.localcontext
2643 InvalidOperation = self.decimal.InvalidOperation
2644 Overflow = self.decimal.Overflow
2753 decimal = self.decimal
2754 DecimalException = decimal.DecimalException
2755 InvalidOperation = decimal.InvalidOperation
2756 FloatOperation = decimal.FloatOperation
2757 DivisionByZero = decimal.DivisionByZero
2758 Overflow = decimal.Overflow
2759 Underflow = decimal.Underflow
2760 Subnormal = decimal.Subnormal
2761 Inexact = decimal.Inexact
2762 Rounded = decimal.Rounded
2763 Clamped = decimal.Clamped
2785 self.assertTrue(issubclass(decimal.ConversionSyntax, InvalidOperation))
2786 self.assertTrue(issubclass(decimal.DivisionImpossible, InvalidOperation))
2787 self.assertTrue(issubclass(decimal.DivisionUndefined, InvalidOperation))
2788 self.assertTrue(issubclass(decimal.DivisionUndefined, ZeroDivisionError))
2789 self.assertTrue(issubclass(decimal.InvalidContext, InvalidOperation))
2792 decimal = C variable in CPythonAPItests
2794 decimal = P variable in PyPythonAPItests
2799 Context = self.decimal.Context
2800 InvalidOperation = self.decimal.InvalidOperation
2801 DivisionByZero = self.decimal.DivisionByZero
2802 Overflow = self.decimal.Overflow
2821 c = self.decimal.Context()
2836 Context = self.decimal.Context
2838 savedecimal = sys.modules['decimal']
2841 sys.modules['decimal'] = self.decimal
2868 sys.modules['decimal'] = dumper
2879 sys.modules['decimal'] = loader
2892 sys.modules['decimal'] = savedecimal
2895 Decimal = self.decimal.Decimal
2897 self.assertIn(Decimal(10), ['a', 1.0, Decimal(10), (1,2), {}])
2898 self.assertNotIn(Decimal(10), ['a', 1.0, (1,2), {}])
2902 Decimal = self.decimal.Decimal
2903 Context = self.decimal.Context
2922 Context = self.decimal.Context
2927 Decimal = self.decimal.Decimal
2928 Context = self.decimal.Context
2931 d = c.abs(Decimal(-1))
2936 Decimal = self.decimal.Decimal
2937 Context = self.decimal.Context
2940 d = c.add(Decimal(1), Decimal(1))
2942 self.assertEqual(c.add(Decimal(1), 1), d)
2943 self.assertEqual(c.add(1, Decimal(1)), d)
2948 Decimal = self.decimal.Decimal
2949 Context = self.decimal.Context
2952 d = c.compare(Decimal(1), Decimal(1))
2954 self.assertEqual(c.compare(Decimal(1), 1), d)
2955 self.assertEqual(c.compare(1, Decimal(1)), d)
2960 Decimal = self.decimal.Decimal
2961 Context = self.decimal.Context
2964 d = c.compare_signal(Decimal(1), Decimal(1))
2966 self.assertEqual(c.compare_signal(Decimal(1), 1), d)
2967 self.assertEqual(c.compare_signal(1, Decimal(1)), d)
2972 Decimal = self.decimal.Decimal
2973 Context = self.decimal.Context
2976 d = c.compare_total(Decimal(1), Decimal(1))
2978 self.assertEqual(c.compare_total(Decimal(1), 1), d)
2979 self.assertEqual(c.compare_total(1, Decimal(1)), d)
2984 Decimal = self.decimal.Decimal
2985 Context = self.decimal.Context
2988 d = c.compare_total_mag(Decimal(1), Decimal(1))
2990 self.assertEqual(c.compare_total_mag(Decimal(1), 1), d)
2991 self.assertEqual(c.compare_total_mag(1, Decimal(1)), d)
2996 Decimal = self.decimal.Decimal
2997 Context = self.decimal.Context
3000 d = c.copy_abs(Decimal(-1))
3005 Decimal = self.decimal.Decimal
3006 Context = self.decimal.Context
3009 d = c.copy_decimal(Decimal(-1))
3014 Decimal = self.decimal.Decimal
3015 Context = self.decimal.Context
3018 d = c.copy_negate(Decimal(-1))
3023 Decimal = self.decimal.Decimal
3024 Context = self.decimal.Context
3027 d = c.copy_sign(Decimal(1), Decimal(-2))
3029 self.assertEqual(c.copy_sign(Decimal(1), -2), d)
3030 self.assertEqual(c.copy_sign(1, Decimal(-2)), d)
3035 Decimal = self.decimal.Decimal
3036 Context = self.decimal.Context
3039 d = c.divide(Decimal(1), Decimal(2))
3041 self.assertEqual(c.divide(Decimal(1), 2), d)
3042 self.assertEqual(c.divide(1, Decimal(2)), d)
3047 Decimal = self.decimal.Decimal
3048 Context = self.decimal.Context
3051 d = c.divide_int(Decimal(1), Decimal(2))
3053 self.assertEqual(c.divide_int(Decimal(1), 2), d)
3054 self.assertEqual(c.divide_int(1, Decimal(2)), d)
3059 Decimal = self.decimal.Decimal
3060 Context = self.decimal.Context
3063 d = c.divmod(Decimal(1), Decimal(2))
3065 self.assertEqual(c.divmod(Decimal(1), 2), d)
3066 self.assertEqual(c.divmod(1, Decimal(2)), d)
3071 Decimal = self.decimal.Decimal
3072 Context = self.decimal.Context
3075 d = c.exp(Decimal(10))
3080 Decimal = self.decimal.Decimal
3081 Context = self.decimal.Context
3084 d = c.fma(Decimal(2), Decimal(3), Decimal(4))
3086 self.assertEqual(c.fma(Decimal(2), 3, 4), d)
3087 self.assertEqual(c.fma(2, Decimal(3), 4), d)
3088 self.assertEqual(c.fma(2, 3, Decimal(4)), d)
3089 self.assertEqual(c.fma(Decimal(2), Decimal(3), 4), d)
3096 Decimal('Infinity'), Decimal(0), "not a decimal")
3098 Decimal(1), Decimal('snan'), 1.222)
3099 # ... and for Decimal.fma.
3100 self.assertRaises(TypeError, Decimal('Infinity').fma,
3101 Decimal(0), "not a decimal")
3102 self.assertRaises(TypeError, Decimal(1).fma,
3103 Decimal('snan'), 1.222)
3106 Decimal = self.decimal.Decimal
3107 Context = self.decimal.Context
3110 d = c.is_finite(Decimal(10))
3115 Decimal = self.decimal.Decimal
3116 Context = self.decimal.Context
3119 d = c.is_infinite(Decimal(10))
3124 Decimal = self.decimal.Decimal
3125 Context = self.decimal.Context
3128 d = c.is_nan(Decimal(10))
3133 Decimal = self.decimal.Decimal
3134 Context = self.decimal.Context
3137 d = c.is_normal(Decimal(10))
3142 Decimal = self.decimal.Decimal
3143 Context = self.decimal.Context
3146 d = c.is_qnan(Decimal(10))
3151 Decimal = self.decimal.Decimal
3152 Context = self.decimal.Context
3155 d = c.is_signed(Decimal(10))
3160 Decimal = self.decimal.Decimal
3161 Context = self.decimal.Context
3164 d = c.is_snan(Decimal(10))
3169 Decimal = self.decimal.Decimal
3170 Context = self.decimal.Context
3173 d = c.is_subnormal(Decimal(10))
3178 Decimal = self.decimal.Decimal
3179 Context = self.decimal.Context
3182 d = c.is_zero(Decimal(10))
3187 Decimal = self.decimal.Decimal
3188 Context = self.decimal.Context
3191 d = c.ln(Decimal(10))
3196 Decimal = self.decimal.Decimal
3197 Context = self.decimal.Context
3200 d = c.log10(Decimal(10))
3205 Decimal = self.decimal.Decimal
3206 Context = self.decimal.Context
3209 d = c.logb(Decimal(10))
3214 Decimal = self.decimal.Decimal
3215 Context = self.decimal.Context
3218 d = c.logical_and(Decimal(1), Decimal(1))
3220 self.assertEqual(c.logical_and(Decimal(1), 1), d)
3221 self.assertEqual(c.logical_and(1, Decimal(1)), d)
3226 Decimal = self.decimal.Decimal
3227 Context = self.decimal.Context
3230 d = c.logical_invert(Decimal(1000))
3235 Decimal = self.decimal.Decimal
3236 Context = self.decimal.Context
3239 d = c.logical_or(Decimal(1), Decimal(1))
3241 self.assertEqual(c.logical_or(Decimal(1), 1), d)
3242 self.assertEqual(c.logical_or(1, Decimal(1)), d)
3247 Decimal = self.decimal.Decimal
3248 Context = self.decimal.Context
3251 d = c.logical_xor(Decimal(1), Decimal(1))
3253 self.assertEqual(c.logical_xor(Decimal(1), 1), d)
3254 self.assertEqual(c.logical_xor(1, Decimal(1)), d)
3259 Decimal = self.decimal.Decimal
3260 Context = self.decimal.Context
3263 d = c.max(Decimal(1), Decimal(2))
3265 self.assertEqual(c.max(Decimal(1), 2), d)
3266 self.assertEqual(c.max(1, Decimal(2)), d)
3271 Decimal = self.decimal.Decimal
3272 Context = self.decimal.Context
3275 d = c.max_mag(Decimal(1), Decimal(2))
3277 self.assertEqual(c.max_mag(Decimal(1), 2), d)
3278 self.assertEqual(c.max_mag(1, Decimal(2)), d)
3283 Decimal = self.decimal.Decimal
3284 Context = self.decimal.Context
3287 d = c.min(Decimal(1), Decimal(2))
3289 self.assertEqual(c.min(Decimal(1), 2), d)
3290 self.assertEqual(c.min(1, Decimal(2)), d)
3295 Decimal = self.decimal.Decimal
3296 Context = self.decimal.Context
3299 d = c.min_mag(Decimal(1), Decimal(2))
3301 self.assertEqual(c.min_mag(Decimal(1), 2), d)
3302 self.assertEqual(c.min_mag(1, Decimal(2)), d)
3307 Decimal = self.decimal.Decimal
3308 Context = self.decimal.Context
3311 d = c.minus(Decimal(10))
3316 Decimal = self.decimal.Decimal
3317 Context = self.decimal.Context
3320 d = c.multiply(Decimal(1), Decimal(2))
3322 self.assertEqual(c.multiply(Decimal(1), 2), d)
3323 self.assertEqual(c.multiply(1, Decimal(2)), d)
3328 Decimal = self.decimal.Decimal
3329 Context = self.decimal.Context
3332 d = c.next_minus(Decimal(10))
3337 Decimal = self.decimal.Decimal
3338 Context = self.decimal.Context
3341 d = c.next_plus(Decimal(10))
3346 Decimal = self.decimal.Decimal
3347 Context = self.decimal.Context
3350 d = c.next_toward(Decimal(1), Decimal(2))
3352 self.assertEqual(c.next_toward(Decimal(1), 2), d)
3353 self.assertEqual(c.next_toward(1, Decimal(2)), d)
3358 Decimal = self.decimal.Decimal
3359 Context = self.decimal.Context
3362 d = c.normalize(Decimal(10))
3367 Decimal = self.decimal.Decimal
3368 Context = self.decimal.Context
3371 self.assertEqual(c.number_class(123), c.number_class(Decimal(123)))
3372 self.assertEqual(c.number_class(0), c.number_class(Decimal(0)))
3373 self.assertEqual(c.number_class(-45), c.number_class(Decimal(-45)))
3376 Decimal = self.decimal.Decimal
3377 Context = self.decimal.Context
3380 d = c.plus(Decimal(10))
3385 Decimal = self.decimal.Decimal
3386 Context = self.decimal.Context
3389 d = c.power(Decimal(1), Decimal(4))
3391 self.assertEqual(c.power(Decimal(1), 4), d)
3392 self.assertEqual(c.power(1, Decimal(4)), d)
3393 self.assertEqual(c.power(Decimal(1), Decimal(4)), d)
3399 Decimal = self.decimal.Decimal
3400 Context = self.decimal.Context
3403 d = c.quantize(Decimal(1), Decimal(2))
3405 self.assertEqual(c.quantize(Decimal(1), 2), d)
3406 self.assertEqual(c.quantize(1, Decimal(2)), d)
3411 Decimal = self.decimal.Decimal
3412 Context = self.decimal.Context
3415 d = c.remainder(Decimal(1), Decimal(2))
3417 self.assertEqual(c.remainder(Decimal(1), 2), d)
3418 self.assertEqual(c.remainder(1, Decimal(2)), d)
3423 Decimal = self.decimal.Decimal
3424 Context = self.decimal.Context
3427 d = c.remainder_near(Decimal(1), Decimal(2))
3429 self.assertEqual(c.remainder_near(Decimal(1), 2), d)
3430 self.assertEqual(c.remainder_near(1, Decimal(2)), d)
3435 Decimal = self.decimal.Decimal
3436 Context = self.decimal.Context
3439 d = c.rotate(Decimal(1), Decimal(2))
3441 self.assertEqual(c.rotate(Decimal(1), 2), d)
3442 self.assertEqual(c.rotate(1, Decimal(2)), d)
3447 Decimal = self.decimal.Decimal
3448 Context = self.decimal.Context
3451 d = c.sqrt(Decimal(10))
3456 Decimal = self.decimal.Decimal
3457 Context = self.decimal.Context
3460 d = c.same_quantum(Decimal(1), Decimal(2))
3462 self.assertEqual(c.same_quantum(Decimal(1), 2), d)
3463 self.assertEqual(c.same_quantum(1, Decimal(2)), d)
3468 Decimal = self.decimal.Decimal
3469 Context = self.decimal.Context
3472 d = c.scaleb(Decimal(1), Decimal(2))
3474 self.assertEqual(c.scaleb(Decimal(1), 2), d)
3475 self.assertEqual(c.scaleb(1, Decimal(2)), d)
3480 Decimal = self.decimal.Decimal
3481 Context = self.decimal.Context
3484 d = c.shift(Decimal(1), Decimal(2))
3486 self.assertEqual(c.shift(Decimal(1), 2), d)
3487 self.assertEqual(c.shift(1, Decimal(2)), d)
3492 Decimal = self.decimal.Decimal
3493 Context = self.decimal.Context
3496 d = c.subtract(Decimal(1), Decimal(2))
3498 self.assertEqual(c.subtract(Decimal(1), 2), d)
3499 self.assertEqual(c.subtract(1, Decimal(2)), d)
3504 Decimal = self.decimal.Decimal
3505 Context = self.decimal.Context
3508 d = c.to_eng_string(Decimal(10))
3513 Decimal = self.decimal.Decimal
3514 Context = self.decimal.Context
3517 d = c.to_sci_string(Decimal(10))
3522 Decimal = self.decimal.Decimal
3523 Context = self.decimal.Context
3526 d = c.to_integral_exact(Decimal(10))
3531 Decimal = self.decimal.Decimal
3532 Context = self.decimal.Context
3535 d = c.to_integral_value(Decimal(10))
3541 decimal = C variable in CContextAPItests
3543 decimal = P variable in PyContextAPItests
3551 getcontext = self.decimal.getcontext
3552 localcontext = self.decimal.localcontext
3564 Context = self.decimal.Context
3565 getcontext = self.decimal.getcontext
3566 localcontext = self.decimal.localcontext
3568 localcontext = self.decimal.localcontext
3581 Decimal = self.decimal.Decimal
3582 Context = self.decimal.Context
3583 getcontext = self.decimal.getcontext
3584 localcontext = self.decimal.localcontext
3585 Clamped = self.decimal.Clamped
3586 Overflow = self.decimal.Overflow
3602 self.assertRaises(Overflow, c2.power, Decimal('3.4e200'), 2)
3615 localcontext = self.decimal.localcontext
3627 localcontext = self.decimal.localcontext
3639 Context = self.decimal.Context
3640 localcontext = self.decimal.localcontext
3641 getcontext = self.decimal.getcontext
3642 setcontext = self.decimal.setcontext
3672 decimal = C variable in CContextWithStatement
3674 decimal = P variable in PyContextWithStatement
3681 Decimal = self.decimal.Decimal
3682 Context = self.decimal.Context
3683 Inexact = self.decimal.Inexact
3684 Rounded = self.decimal.Rounded
3685 Underflow = self.decimal.Underflow
3686 Clamped = self.decimal.Clamped
3687 Subnormal = self.decimal.Subnormal
3690 if self.decimal == C:
3702 (context._apply, [Decimal("100E-425000010")]),
3703 (context.sqrt, [Decimal(2)]),
3704 (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]),
3705 (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]),
3706 (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]),
3745 Context = self.decimal.Context
3746 Inexact = self.decimal.Inexact
3747 Rounded = self.decimal.Rounded
3784 Decimal = self.decimal.Decimal
3785 FloatOperation = self.decimal.FloatOperation
3786 localcontext = self.decimal.localcontext
3794 self.assertEqual(Decimal(7.5), 7.5)
3803 x = Decimal.from_float(7.5)
3820 self.assertRaises(FloatOperation, Decimal, 7.5)
3829 x = Decimal.from_float(7.5)
3837 Decimal = self.decimal.Decimal
3838 Context = self.decimal.Context
3839 FloatOperation = self.decimal.FloatOperation
3840 localcontext = self.decimal.localcontext
3851 small_d = Decimal('0.25')
3852 big_d = Decimal('3.0')
3856 zero_d = Decimal('0.0')
3857 neg_zero_d = Decimal('-0.0')
3861 inf_d = Decimal('Infinity')
3862 neg_inf_d = Decimal('-Infinity')
3889 assert_attr(Decimal('0.1'), 0.1, '__ne__', c, None)
3894 assert_attr(Decimal('NaN'), float('nan'), '__ne__', c, None)
3898 s = set([100.0, Decimal('100.0')])
3904 self.assertRaises(signal, sorted, [1.0, Decimal('10.0')])
3906 s = sorted([10.0, Decimal('10.0')])
3910 b = 10.0 in [Decimal('10.0'), 1.0]
3914 b = 10.0 in {Decimal('10.0'):'a', 1.0:'b'}
3928 Decimal = self.decimal.Decimal
3929 Context = self.decimal.Context
3930 Inexact = self.decimal.Inexact
3931 FloatOperation= self.decimal.FloatOperation
3944 decimal = C variable in CContextFlags
3946 decimal = P variable in PyContextFlags
3952 BasicContext = self.decimal.BasicContext
3953 ExtendedContext = self.decimal.ExtendedContext
3954 getcontext = self.decimal.getcontext
3955 setcontext = self.decimal.setcontext
3956 InvalidOperation = self.decimal.InvalidOperation
3957 DivisionByZero = self.decimal.DivisionByZero
3958 Overflow = self.decimal.Overflow
3959 Underflow = self.decimal.Underflow
3960 Clamped = self.decimal.Clamped
3988 DefaultContext = self.decimal.DefaultContext
3989 BasicContext = self.decimal.BasicContext
3990 ExtendedContext = self.decimal.ExtendedContext
3991 getcontext = self.decimal.getcontext
3992 setcontext = self.decimal.setcontext
3993 InvalidOperation = self.decimal.InvalidOperation
3994 DivisionByZero = self.decimal.DivisionByZero
3995 Overflow = self.decimal.Overflow
4029 decimal = C variable in CSpecialContexts
4031 decimal = P variable in PySpecialContexts
4036 Context = self.decimal.Context
4037 DefaultContext = self.decimal.DefaultContext
4096 decimal = C variable in CContextInputValidation
4098 decimal = P variable in PyContextInputValidation
4103 decimal = self.decimal
4104 Decimal = decimal.Decimal
4105 Context = decimal.Context
4106 Clamped = decimal.Clamped
4107 DivisionByZero = decimal.DivisionByZero
4108 Inexact = decimal.Inexact
4109 Overflow = decimal.Overflow
4110 Rounded = decimal.Rounded
4111 Subnormal = decimal.Subnormal
4112 Underflow = decimal.Underflow
4113 InvalidOperation = decimal.InvalidOperation
4134 flags = {v:(v in flags) for v in OrderedSignals[decimal] + flags}
4138 traps = {v:(v in traps) for v in OrderedSignals[decimal] + traps}
4151 self.assertRaises(InvalidOperation, c.quantize, Decimal('9e2'), 0)
4157 self.assertEqual(c.plus(Decimal('9.9')), 9)
4163 x = c.add(Decimal('1e-99'), Decimal('2.234e-2000'))
4164 self.assertEqual(x, Decimal('0.0'))
4172 self.assertRaises(Overflow, c.add, Decimal('1e99'), Decimal('2.234e2000'))
4173 if self.decimal == C:
4188 x = c.plus(Decimal('1e99'))
4197 for signal in OrderedSignals[decimal]:
4206 for signal in OrderedSignals[decimal]:
4210 decimal = C variable in CContextSubclassing
4212 decimal = P variable in PyContextSubclassing
4240 x = [s for s in dir(C.Decimal(9)) if '__' in s or not s.startswith('_')]
4241 y = [s for s in dir(C.Decimal(9)) if '__' in s or not s.startswith('_')]
4247 Decimal = self.decimal.Decimal
4249 self.assertEqual(Decimal('1234e9999').adjusted(), 10002)
4251 self.assertEqual(Decimal('nan').adjusted(), 0)
4252 self.assertEqual(Decimal('inf').adjusted(), 0)
4255 Decimal = self.decimal.Decimal
4256 getcontext = self.decimal.getcontext
4258 x = Decimal(9).canonical()
4262 x = c.canonical(Decimal(9))
4266 c = self.decimal.DefaultContext.copy()
4274 for sig in OrderedSignals[self.decimal]:
4285 Decimal = self.decimal.Decimal
4286 localcontext = self.decimal.localcontext
4294 self.assertEqual(abs(Decimal("-10")), 10)
4296 self.assertEqual(Decimal("7") + 1, 8)
4298 self.assertEqual(Decimal("10") / 5, 2)
4300 self.assertEqual(Decimal("10") // 7, 1)
4302 self.assertEqual(Decimal("1.2").fma(Decimal("0.01"), 1), 1)
4303 self.assertIs(Decimal("NaN").fma(7, 1).is_nan(), True)
4305 self.assertEqual(pow(Decimal(10), 2, 7), 2)
4307 self.assertEqual(Decimal("1.01").exp(), 3)
4309 self.assertIs(Decimal("0.01").is_normal(), False)
4311 self.assertIs(Decimal("0.01").is_subnormal(), True)
4313 self.assertEqual(Decimal("20").ln(), 3)
4315 self.assertEqual(Decimal("20").log10(), 1)
4317 self.assertEqual(Decimal("580").logb(), 2)
4319 self.assertEqual(Decimal("10").logical_invert(), 1)
4321 self.assertEqual(-Decimal("-10"), 10)
4323 self.assertEqual(Decimal("2") * 4, 8)
4325 self.assertEqual(Decimal("10").next_minus(), 9)
4327 self.assertEqual(Decimal("10").next_plus(), Decimal('2E+1'))
4329 self.assertEqual(Decimal("-10").normalize(), Decimal('-1E+1'))
4331 self.assertEqual(Decimal("10").number_class(), '+Normal')
4333 self.assertEqual(+Decimal("-1"), -1)
4335 self.assertEqual(Decimal("10") % 7, 3)
4337 self.assertEqual(Decimal("10") - 7, 3)
4339 self.assertEqual(Decimal("1.12345").to_integral_exact(), 1)
4342 self.assertTrue(Decimal("1").is_canonical())
4343 self.assertTrue(Decimal("1").is_finite())
4344 self.assertTrue(Decimal("1").is_finite())
4345 self.assertTrue(Decimal("snan").is_snan())
4346 self.assertTrue(Decimal("-1").is_signed())
4347 self.assertTrue(Decimal("0").is_zero())
4348 self.assertTrue(Decimal("0").is_zero())
4354 y = -Decimal(x)
4362 z = y.copy_sign(Decimal(1))
4366 Decimal = self.decimal.Decimal
4367 localcontext = self.decimal.localcontext
4368 InvalidOperation = self.decimal.InvalidOperation
4369 DivisionByZero = self.decimal.DivisionByZero
4372 q, r = divmod(Decimal("10912837129"), 1001)
4373 self.assertEqual(q, Decimal('10901935'))
4374 self.assertEqual(r, Decimal('194'))
4376 q, r = divmod(Decimal("NaN"), 7)
4380 q, r = divmod(Decimal("NaN"), 7)
4385 q, r = divmod(Decimal("inf"), Decimal("inf"))
4390 q, r = divmod(Decimal("inf"), 101)
4395 q, r = divmod(Decimal(0), 0)
4401 q, r = divmod(Decimal(11), 0)
4407 Decimal = self.decimal.Decimal
4408 localcontext = self.decimal.localcontext
4409 Overflow = self.decimal.Overflow
4410 Rounded = self.decimal.Rounded
4415 self.assertEqual(Decimal("1.0") ** 100, Decimal('1.00'))
4423 self.assertEqual(Decimal(10000) ** Decimal("0.5"), Decimal('inf'))
4427 Decimal = self.decimal.Decimal
4428 localcontext = self.decimal.localcontext
4429 InvalidOperation = self.decimal.InvalidOperation
4436 x = Decimal(99).quantize(Decimal("1e1"))
4440 Decimal = self.decimal.Decimal
4441 getcontext = self.decimal.getcontext
4444 self.assertEqual(Decimal("1").radix(), 10)
4448 Decimal = self.decimal.Decimal
4452 self.assertIs(getattr(Decimal("1"), attr)("xyz"), NotImplemented)
4455 # Python3 behavior: round() returns Decimal
4456 Decimal = self.decimal.Decimal
4457 localcontext = self.decimal.localcontext
4462 self.assertEqual(str(Decimal("9.99").__round__()), "10")
4463 self.assertEqual(str(Decimal("9.99e-5").__round__()), "0")
4464 self.assertEqual(str(Decimal("1.23456789").__round__(5)), "1.23457")
4465 self.assertEqual(str(Decimal("1.2345").__round__(10)), "1.2345000000")
4466 self.assertEqual(str(Decimal("1.2345").__round__(-10)), "0E+10")
4468 self.assertRaises(TypeError, Decimal("1.23").__round__, "5")
4469 self.assertRaises(TypeError, Decimal("1.23").__round__, 5, 8)
4472 c = self.decimal.Context()
4476 Decimal = self.decimal.Decimal
4477 localcontext = self.decimal.localcontext
4481 x = Decimal(1221**1271) / 10**3923
4486 Context = self.decimal.Context
4502 decimal = C variable in CCoverage
4504 decimal = P variable in PyCoverage
4507 """Extra functionality in decimal.py"""
4510 # triples giving a format, a Decimal, and the expected result
4511 Decimal = P.Decimal
4528 self.assertEqual(format(Decimal(d), fmt), result)
4531 """White box testing for decimal.py"""
4535 Decimal = P.Decimal
4540 x = Decimal(2**16) ** Decimal("-0.5")
4541 self.assertEqual(x, Decimal('0.00390625'))
4543 x = Decimal(2**16) ** Decimal("-0.6")
4544 self.assertEqual(x, Decimal('0.0012885819'))
4546 x = Decimal("256e7") ** Decimal("-0.5")
4548 x = Decimal(152587890625) ** Decimal('-0.0625')
4549 self.assertEqual(x, Decimal("0.2"))
4551 x = Decimal("152587890625e7") ** Decimal('-0.0625')
4553 x = Decimal(5**2659) ** Decimal('-0.0625')
4556 x = Decimal("152587890625") ** Decimal('-0.5')
4558 x = Decimal(2**578) ** Decimal("-0.5")
4562 Decimal = P.Decimal
4570 d1 = Decimal('-25e55')
4571 b1 = Decimal('-25e55')
4572 d2 = Decimal('33e+33')
4573 b2 = Decimal('33e+33')
4590 Decimal(d1)
4640 Decimal = P.Decimal
4642 d = Decimal(45)
4643 e = Decimal(d)
4649 Decimal = P.Decimal
4653 x = Decimal("NaN")._rescale(3, ROUND_UP)
4658 Decimal = P.Decimal
4660 self.assertRaises(ValueError, Decimal("3.1234")._round, 0, ROUND_UP)
4667 # issue 8786: Add support for IEEE 754 contexts to decimal module.
4753 Decimal = C.Decimal
4764 y = Decimal(a) ** Decimal(b)
4768 self.assertRaises(TypeError, C.Decimal, 9, "xyz")
4772 Decimal = C.Decimal
4778 self.assertRaises(InvalidOperation, Decimal,
5014 Decimal = C.Decimal
5023 self.assertRaises(InvalidOperation, Decimal("1.23").__round__,
5025 self.assertRaises(InvalidOperation, Decimal("1.23").__round__,
5027 self.assertRaises(InvalidOperation, Decimal("1").__round__,
5029 self.assertRaises(C.InvalidOperation, Decimal("1").__round__,
5031 self.assertRaises(OverflowError, Decimal("1.23").__round__,
5033 self.assertRaises(OverflowError, Decimal("1.23").__round__,
5038 Decimal = C.Decimal
5041 self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", [], 9)
5042 self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", 9)
5043 self.assertRaises(TypeError, Decimal(1).__format__, [])
5045 self.assertRaises(ValueError, Decimal(1).__format__, "<>=10.10")
5047 self.assertRaises(ValueError, Decimal("1.23456789").__format__,
5051 Decimal = C.Decimal
5055 x = Decimal(10)
5072 x = Decimal("99999999999999999999999999.9").to_integral_value(ROUND_UP)
5073 self.assertEqual(x, Decimal('100000000000000000000000000'))
5075 x = Decimal("99999999999999999999999999.9").to_integral_exact(ROUND_UP)
5076 self.assertEqual(x, Decimal('100000000000000000000000000'))
5079 self.assertRaises(Inexact, Decimal("999.9").to_integral_exact, ROUND_UP)
5083 Decimal = C.Decimal
5089 self.assertEqual(Decimal('9.99e10').to_eng_string(), '99.9E+9')
5091 self.assertRaises(TypeError, pow, Decimal(1), 2, "3")
5092 self.assertRaises(TypeError, Decimal(9).number_class, "x", "y")
5093 self.assertRaises(TypeError, Decimal(9).same_quantum, 3, "x", "y")
5097 Decimal("1.23456789").quantize, Decimal('1e-100000'), []
5101 Decimal("1.23456789").quantize, Decimal('1e-100000'), getcontext()
5105 Decimal("1.23456789").quantize, Decimal('1e-100000'), 10
5109 Decimal("1.23456789").quantize, Decimal('1e-100000'), ROUND_UP, 1000
5116 self.assertRaises(TypeError, c.copy_sign, Decimal(1), "x", "y")
5122 self.assertEqual(str(c.canonical(Decimal(200))), '200')
5126 self.assertRaises(DivisionByZero, Decimal(9).__divmod__, 0)
5132 self.assertRaises(InvalidOperation, Decimal(9).__divmod__, 0)
5138 self.assertRaises(InvalidOperation, pow, Decimal(1000), 1, 501)
5141 Decimal = C.Decimal
5144 x = Decimal("10001111111")
5329 Decimal = C.Decimal
5340 return Decimal(x).__format__(fmt, override)
5366 Decimal = C.Decimal
5376 self.assertRaises(InvalidOperation, Decimal, x)
5379 self.assertRaises(InvalidOperation, Decimal, x)
5383 self.assertRaises(InvalidOperation, Decimal, x)
5387 self.assertRaises(InvalidOperation, Decimal, x)
5390 Decimal = C.Decimal
5405 self.assertRaises(InvalidOperation, Decimal, x)
5409 self.assertRaises(InvalidOperation, Decimal, x)
5414 self.assertRaises(InvalidOperation, Decimal, x)
5418 self.assertRaises(InvalidOperation, Decimal, x)
5423 self.assertRaises(OverflowError, Decimal, x)
5427 self.assertRaises(OverflowError, Decimal, x)
5431 self.assertEqual(str(Decimal(x)), '-sNaN')
5433 self.assertEqual(str(Decimal(x)), '-sNaN')
5435 self.assertEqual(str(Decimal(x)), '-sNaN1')
5438 Decimal = C.Decimal
5441 self.assertGreater(Decimal(0).__sizeof__(), 0)
5443 x = Decimal(10**(19*24)).__sizeof__()
5444 y = Decimal(10**(19*25)).__sizeof__()
5447 x = Decimal(10**(9*24)).__sizeof__()
5448 y = Decimal(10**(9*25)).__sizeof__()
5452 Decimal = C.Decimal
5476 self.assertEqual(Decimal.from_float(cls(101.1)),
5477 Decimal.from_float(101.1))
5490 if (attr == 'Decimal' or attr == 'Context' or
5518 pdict = {C: {'other': C.Decimal(1),
5519 'third': C.Decimal(1),
5520 'x': C.Decimal(1),
5521 'y': C.Decimal(1),
5522 'z': C.Decimal(1),
5523 'a': C.Decimal(1),
5524 'b': C.Decimal(1),
5525 'c': C.Decimal(1),
5526 'exp': C.Decimal(1),
5527 'modulo': C.Decimal(1),
5532 P: {'other': P.Decimal(1),
5533 'third': P.Decimal(1),
5534 'a': P.Decimal(1),
5535 'b': P.Decimal(1),
5536 'c': P.Decimal(1),
5537 'exp': P.Decimal(1),
5538 'modulo': P.Decimal(1),
5592 if ty == 'Decimal':
5612 doit('Decimal')
5647 Runs all arithmetic tests if arith is True or if the "decimal" resource
5654 TEST_ALL = arith if arith is not None else is_resource_enabled('decimal')
5681 savedecimal = sys.modules['decimal']
5683 sys.modules['decimal'] = C
5685 sys.modules['decimal'] = P
5687 sys.modules['decimal'] = savedecimal
5694 if not orig_sys_decimal is sys.modules['decimal']:
5696 "sys.modules['decimal'].")