10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom __future__ import division 20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# When true division is the default, get rid of this and add it to 30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# test_long.py instead. In the meantime, it's too obscure to try to 40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# trick just part of test_long into using future division. 50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport sys 70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport random 80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport math 90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport unittest 100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test.test_support import run_unittest 110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# decorator for skipping tests on non-IEEE 754 platforms 130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yirequires_IEEE_754 = unittest.skipUnless( 140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi float.__getformat__("double").startswith("IEEE"), 150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi "test requires IEEE 754 doubles") 160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiDBL_MAX = sys.float_info.max 180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiDBL_MAX_EXP = sys.float_info.max_exp 190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiDBL_MIN_EXP = sys.float_info.min_exp 200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiDBL_MANT_DIG = sys.float_info.mant_dig 210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiDBL_MIN_OVERFLOW = 2**DBL_MAX_EXP - 2**(DBL_MAX_EXP - DBL_MANT_DIG - 1) 220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# pure Python version of correctly-rounded true division 240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef truediv(a, b): 250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """Correctly-rounded true division for integers.""" 260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi negative = a^b < 0 270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a, b = abs(a), abs(b) 280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # exceptions: division by zero, overflow 300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if not b: 310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raise ZeroDivisionError("division by zero") 320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if a >= DBL_MIN_OVERFLOW * b: 330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raise OverflowError("int/int too large to represent as a float") 340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # find integer d satisfying 2**(d - 1) <= a/b < 2**d 360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d = a.bit_length() - b.bit_length() 370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if d >= 0 and a >= 2**d * b or d < 0 and a * 2**-d >= b: 380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d += 1 390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # compute 2**-exp * a / b for suitable exp 410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exp = max(d, DBL_MIN_EXP) - DBL_MANT_DIG 420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a, b = a << max(-exp, 0), b << max(exp, 0) 430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi q, r = divmod(a, b) 440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # round-half-to-even: fractional part is r/b, which is > 0.5 iff 460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 2*r > b, and == 0.5 iff 2*r == b. 470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 2*r > b or 2*r == b and q % 2 == 1: 480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi q += 1 490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi result = math.ldexp(float(q), exp) 510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return -result if negative else result 520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TrueDivisionTests(unittest.TestCase): 540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test(self): 550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi huge = 1L << 40000 560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi mhuge = -huge 570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(huge / huge, 1.0) 580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(mhuge / mhuge, 1.0) 590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(huge / mhuge, -1.0) 600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(mhuge / huge, -1.0) 610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(1 / huge, 0.0) 620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(1L / huge, 0.0) 630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(1 / mhuge, 0.0) 640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(1L / mhuge, 0.0) 650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((666 * huge + (huge >> 1)) / huge, 666.5) 660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((666 * mhuge + (mhuge >> 1)) / mhuge, 666.5) 670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((666 * huge + (huge >> 1)) / mhuge, -666.5) 680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((666 * mhuge + (mhuge >> 1)) / huge, -666.5) 690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(huge / (huge << 1), 0.5) 700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((1000000 * huge) / huge, 1000000) 710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi namespace = {'huge': huge, 'mhuge': mhuge} 730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for overflow in ["float(huge)", "float(mhuge)", 750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi "huge / 1", "huge / 2L", "huge / -1", "huge / -2L", 760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi "mhuge / 100", "mhuge / 100L"]: 770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # If the "eval" does not happen in this module, 780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # true division is not enabled 790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with self.assertRaises(OverflowError): 800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi eval(overflow, namespace) 810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for underflow in ["1 / huge", "2L / huge", "-1 / huge", "-2L / huge", 830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi "100 / mhuge", "100L / mhuge"]: 840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi result = eval(underflow, namespace) 850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(result, 0.0, 'expected underflow to 0 ' 860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'from {!r}'.format(underflow)) 870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for zero in ["huge / 0", "huge / 0L", "mhuge / 0", "mhuge / 0L"]: 890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with self.assertRaises(ZeroDivisionError): 900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi eval(zero, namespace) 910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def check_truediv(self, a, b, skip_small=True): 930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """Verify that the result of a/b is correctly rounded, by 940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi comparing it with a pure Python implementation of correctly 950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi rounded division. b should be nonzero.""" 960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a, b = long(a), long(b) 980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # skip check for small a and b: in this case, the current 1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # implementation converts the arguments to float directly and 1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # then applies a float division. This can give doubly-rounded 1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # results on x87-using machines (particularly 32-bit Linux). 1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if skip_small and max(abs(a), abs(b)) < 2**DBL_MANT_DIG: 1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return 1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # use repr so that we can distinguish between -0.0 and 0.0 1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi expected = repr(truediv(a, b)) 1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except OverflowError: 1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi expected = 'overflow' 1110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except ZeroDivisionError: 1120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi expected = 'zerodivision' 1130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi got = repr(a / b) 1160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except OverflowError: 1170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi got = 'overflow' 1180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except ZeroDivisionError: 1190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi got = 'zerodivision' 1200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(expected, got, "Incorrectly rounded division {}/{}: " 1220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi "expected {}, got {}".format(a, b, expected, got)) 1230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi @requires_IEEE_754 1250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_correctly_rounded_true_division(self): 1260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # more stringent tests than those above, checking that the 1270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # result of true division of ints is always correctly rounded. 1280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # This test should probably be considered CPython-specific. 1290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Exercise all the code paths not involving Gb-sized ints. 1310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # ... divisions involving zero 1320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(123, 0) 1330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(-456, 0) 1340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(0, 3) 1350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(0, -3) 1360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(0, 0) 1370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # ... overflow or underflow by large margin 1380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(671 * 12345 * 2**DBL_MAX_EXP, 12345) 1390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(12345, 345678 * 2**(DBL_MANT_DIG - DBL_MIN_EXP)) 1400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # ... a much larger or smaller than b 1410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(12345*2**100, 98765) 1420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(12345*2**30, 98765*7**81) 1430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # ... a / b near a boundary: one of 1, 2**DBL_MANT_DIG, 2**DBL_MIN_EXP, 1440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 2**DBL_MAX_EXP, 2**(DBL_MIN_EXP-DBL_MANT_DIG) 1450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi bases = (0, DBL_MANT_DIG, DBL_MIN_EXP, 1460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi DBL_MAX_EXP, DBL_MIN_EXP - DBL_MANT_DIG) 1470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for base in bases: 1480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for exp in range(base - 15, base + 15): 1490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(75312*2**max(exp, 0), 69187*2**max(-exp, 0)) 1500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(69187*2**max(exp, 0), 75312*2**max(-exp, 0)) 1510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # overflow corner case 1530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for m in [1, 2, 7, 17, 12345, 7**100, 1540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi -1, -2, -5, -23, -67891, -41**50]: 1550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for n in range(-10, 10): 1560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(m*DBL_MIN_OVERFLOW + n, m) 1570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(m*DBL_MIN_OVERFLOW + n, -m) 1580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # check detection of inexactness in shifting stage 1600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for n in range(250): 1610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # (2**DBL_MANT_DIG+1)/(2**DBL_MANT_DIG) lies halfway 1620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # between two representable floats, and would usually be 1630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # rounded down under round-half-to-even. The tiniest of 1640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # additions to the numerator should cause it to be rounded 1650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # up instead. 1660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv((2**DBL_MANT_DIG + 1)*12345*2**200 + 2**n, 1670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2**DBL_MANT_DIG*12345) 1680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 1/2731 is one of the smallest division cases that's subject 1700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # to double rounding on IEEE 754 machines working internally with 1710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 64-bit precision. On such machines, the next check would fail, 1720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # were it not explicitly skipped in check_truediv. 1730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(1, 2731) 1740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # a particularly bad case for the old algorithm: gives an 1760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # error of close to 3.5 ulps. 1770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(295147931372582273023, 295147932265116303360) 1780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for i in range(1000): 1790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(10**(i+1), 10**i) 1800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(10**i, 10**(i+1)) 1810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # test round-half-to-even behaviour, normal result 1830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for m in [1, 2, 4, 7, 8, 16, 17, 32, 12345, 7**100, 1840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi -1, -2, -5, -23, -67891, -41**50]: 1850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for n in range(-10, 10): 1860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(2**DBL_MANT_DIG*m + n, m) 1870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # test round-half-to-even, subnormal result 1890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for n in range(-20, 20): 1900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(n, 2**1076) 1910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # largeish random divisions: a/b where |a| <= |b| <= 1930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 2*|a|; |ans| is between 0.5 and 1.0, so error should 1940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # always be bounded by 2**-54 with equality possible only 1950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # if the least significant bit of q=ans*2**53 is zero. 1960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for M in [10**10, 10**100, 10**1000]: 1970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for i in range(1000): 1980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a = random.randrange(1, M) 1990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi b = random.randrange(a, 2*a+1) 2000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(a, b) 2010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(-a, b) 2020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(a, -b) 2030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(-a, -b) 2040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # and some (genuinely) random tests 2060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for _ in range(10000): 2070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a_bits = random.randrange(1000) 2080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi b_bits = random.randrange(1, 1000) 2090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = random.randrange(2**a_bits) 2100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi y = random.randrange(1, 2**b_bits) 2110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(x, y) 2120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(x, -y) 2130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(-x, y) 2140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.check_truediv(-x, -y) 2150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef test_main(): 2180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi run_unittest(TrueDivisionTests) 2190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == "__main__": 2210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi test_main() 222