def testLdexp(self): self.assertRaises(TypeError, math.ldexp) self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) #self.assertRaises(OverflowError, math.ldexp, 1., 1000000) #self.assertRaises(OverflowError, math.ldexp, -1., 1000000) #self.assertEqual(math.ldexp(1., -1000000), 0.) #self.assertEqual(math.ldexp(-1., -1000000), -0.) self.assertEqual(math.ldexp(INF, 30), INF) self.assertEqual(math.ldexp(NINF, -213), NINF) self.assertTrue(math.isnan(math.ldexp(NAN, 0))) # large second argument for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]: self.assertEqual(math.ldexp(INF, -n), INF) self.assertEqual(math.ldexp(NINF, -n), NINF) #self.assertEqual(math.ldexp(1., -n), 0.) #self.assertEqual(math.ldexp(-1., -n), -0.) self.assertEqual(math.ldexp(0., -n), 0.) self.assertEqual(math.ldexp(-0., -n), -0.) self.assertTrue(math.isnan(math.ldexp(NAN, -n))) #self.assertRaises(OverflowError, math.ldexp, 1., n) #self.assertRaises(OverflowError, math.ldexp, -1., n) self.assertEqual(math.ldexp(0., n), 0.) self.assertEqual(math.ldexp(-0., n), -0.) self.assertEqual(math.ldexp(INF, n), INF) self.assertEqual(math.ldexp(NINF, n), NINF) self.assertTrue(math.isnan(math.ldexp(NAN, n)))
def testCopysign(self): self.assertEqual(math.copysign(1, 42), 1.0) self.assertEqual(math.copysign(0., 42), 0.0) self.assertEqual(math.copysign(1., -42), -1.0) self.assertEqual(math.copysign(3, 0.), 3.0) self.assertEqual(math.copysign(4., -0.), -4.0) self.assertRaises(TypeError, math.copysign) # copysign should let us distinguish signs of zeros self.assertEqual(math.copysign(1., 0.), 1.) self.assertEqual(math.copysign(1., -0.), -1.) self.assertEqual(math.copysign(INF, 0.), INF) self.assertEqual(math.copysign(INF, -0.), NINF) self.assertEqual(math.copysign(NINF, 0.), INF) self.assertEqual(math.copysign(NINF, -0.), NINF) # and of infinities self.assertEqual(math.copysign(1., INF), 1.) self.assertEqual(math.copysign(1., NINF), -1.) self.assertEqual(math.copysign(INF, INF), INF) self.assertEqual(math.copysign(INF, NINF), NINF) self.assertEqual(math.copysign(NINF, INF), INF) self.assertEqual(math.copysign(NINF, NINF), NINF) self.assertTrue(math.isnan(math.copysign(NAN, 1.))) self.assertTrue(math.isnan(math.copysign(NAN, INF))) self.assertTrue(math.isnan(math.copysign(NAN, NINF))) self.assertTrue(math.isnan(math.copysign(NAN, NAN))) # copysign(INF, NAN) may be INF or it may be NINF, since # we don't know whether the sign bit of NAN is set on any # given platform. self.assertTrue(math.isinf(math.copysign(INF, NAN))) # similarly, copysign(2., NAN) could be 2. or -2. self.assertEqual(abs(math.copysign(2., NAN)), 2.)
def testLdexp(self): self.assertRaises(TypeError, math.ldexp) self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) ###self.assertRaises(OverflowError, math.ldexp, 1., 1000000) ###self.assertRaises(OverflowError, math.ldexp, -1., 1000000) ###self.assertEqual(math.ldexp(1., -1000000), 0.) ###self.assertEqual(math.ldexp(-1., -1000000), -0.) self.assertEqual(math.ldexp(INF, 30), INF) self.assertEqual(math.ldexp(NINF, -213), NINF) self.assertTrue(math.isnan(math.ldexp(NAN, 0))) # large second argument for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]: self.assertEqual(math.ldexp(INF, -n), INF) self.assertEqual(math.ldexp(NINF, -n), NINF) ###self.assertEqual(math.ldexp(1., -n), 0.) ###self.assertEqual(math.ldexp(-1., -n), -0.) self.assertEqual(math.ldexp(0., -n), 0.) self.assertEqual(math.ldexp(-0., -n), -0.) self.assertTrue(math.isnan(math.ldexp(NAN, -n))) ###self.assertRaises(OverflowError, math.ldexp, 1., n) ###self.assertRaises(OverflowError, math.ldexp, -1., n) self.assertEqual(math.ldexp(0., n), 0.) self.assertEqual(math.ldexp(-0., n), -0.) self.assertEqual(math.ldexp(INF, n), INF) self.assertEqual(math.ldexp(NINF, n), NINF) self.assertTrue(math.isnan(math.ldexp(NAN, n)))
def checker(self,n): fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}" failures = [] idn=n if n=='setattr_test': self.assertTrue(True) else: fn, arg, expected, flags=self.te[n] func = getattr(math, fn) if 'invalid' in flags or 'divide-by-zero' in flags: expected = 'ValueError' elif 'overflow' in flags: expected = 'OverflowError' try: got = func(arg) except ValueError: got = 'ValueError' except OverflowError: got = 'OverflowError' accuracy_failure = None if isinstance(got, (math.cf_base,float)) and isinstance(expected, float): if math.isnan(expected) and math.isnan(got): return if not math.isnan(expected) and not math.isnan(got): if fn == 'lgamma': # we use a weaker accuracy test for lgamma; # lgamma only achieves an absolute error of # a few multiples of the machine accuracy, in # general. accuracy_failure = acc_check(expected, got, rel_err = 5e-15, abs_err = 5e-15) elif fn == 'erfc': # erfc has less-than-ideal accuracy for large # arguments (x ~ 25 or so), mainly due to the # error involved in computing exp(-x*x). # # XXX Would be better to weaken this test only # for large x, instead of for all x. accuracy_failure = ulps_check(expected, got, 2000) else: accuracy_failure = ulps_check(expected, got, 20) if accuracy_failure is None: return if isinstance(got, str) and isinstance(expected, str): if got == expected: return fail_msg = fail_fmt.format(idn, fn, arg, expected, got) if accuracy_failure is not None: fail_msg += ' ({})'.format(accuracy_failure) failures.append(fail_msg) if failures: self.fail('Failures in test_mtestfile:\n ' + '\n '.join(failures))
def test_mtestfile(self): ALLOWED_ERROR = 20 # permitted error, in ulps fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}" failures = [] for id, fn, arg, expected, flags in parse_mtestfile(math_testcases): func = getattr(math, fn) if 'invalid' in flags or 'divide-by-zero' in flags: expected = 'ValueError' elif 'overflow' in flags: expected = 'OverflowError' try: got = func(arg) except ValueError: got = 'ValueError' except OverflowError: got = 'OverflowError' accuracy_failure = None if isinstance(got, float) and isinstance(expected, float): if math.isnan(expected) and math.isnan(got): continue if not math.isnan(expected) and not math.isnan(got): if fn == 'lgamma': # we use a weaker accuracy test for lgamma; # lgamma only achieves an absolute error of # a few multiples of the machine accuracy, in # general. accuracy_failure = acc_check(expected, got, rel_err = 5e-15, abs_err = 5e-15) elif fn == 'erfc': # erfc has less-than-ideal accuracy for large # arguments (x ~ 25 or so), mainly due to the # error involved in computing exp(-x*x). # # XXX Would be better to weaken this test only # for large x, instead of for all x. accuracy_failure = ulps_check(expected, got, 2000) else: accuracy_failure = ulps_check(expected, got, 20) if accuracy_failure is None: continue if isinstance(got, str) and isinstance(expected, str): if got == expected: continue fail_msg = fail_fmt.format(id, fn, arg, expected, got) if accuracy_failure is not None: fail_msg += ' ({})'.format(accuracy_failure) failures.append(fail_msg) if failures: self.fail('Failures in test_mtestfile:\n ' + '\n '.join(failures))
def testHypot(self): self.assertRaises(TypeError, math.hypot) self.ftest('hypot(0,0)', math.hypot(0,0), 0) self.ftest('hypot(3,4)', math.hypot(3,4), 5) self.assertEqual(math.hypot(NAN, INF), INF) self.assertEqual(math.hypot(INF, NAN), INF) self.assertEqual(math.hypot(NAN, NINF), INF) self.assertEqual(math.hypot(NINF, NAN), INF) self.assertTrue(math.isnan(math.hypot(1.0, NAN))) self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
def testSin(self): self.assertRaises(TypeError, math.sin) self.ftest('sin(0)', math.sin(0), 0) self.ftest('sin(pi/2)', math.sin(math.pi/2), 1) self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) try: self.assertTrue(math.isnan(math.sin(INF))) self.assertTrue(math.isnan(math.sin(NINF))) except ValueError: self.assertRaises(ValueError, math.sin, INF) self.assertRaises(ValueError, math.sin, NINF) self.assertTrue(math.isnan(math.sin(NAN)))
def testTan(self): self.assertRaises(TypeError, math.tan) self.ftest('tan(0)', math.tan(0), 0) self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) try: self.assertTrue(math.isnan(math.tan(INF))) self.assertTrue(math.isnan(math.tan(NINF))) except: self.assertRaises(ValueError, math.tan, INF) self.assertRaises(ValueError, math.tan, NINF) self.assertTrue(math.isnan(math.tan(NAN)))
def testCos(self): self.assertRaises(TypeError, math.cos) self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0) self.ftest('cos(0)', math.cos(0), 1) self.ftest('cos(pi/2)', math.cos(math.pi/2), 0) self.ftest('cos(pi)', math.cos(math.pi), -1) try: self.assertTrue(math.isnan(math.cos(INF))) self.assertTrue(math.isnan(math.cos(NINF))) except ValueError: self.assertRaises(ValueError, math.cos, INF) self.assertRaises(ValueError, math.cos, NINF) self.assertTrue(math.isnan(math.cos(NAN)))
def testFloor(self): self.assertRaises(TypeError, math.floor) # These types will be int in py3k. self.assertEqual(float, type(math.floor(1))) self.assertEqual(float, type(math.floor(1L))) self.assertEqual(float, type(math.floor(1.0))) self.ftest('floor(0.5)', math.floor(0.5), 0) self.ftest('floor(1.0)', math.floor(1.0), 1) self.ftest('floor(1.5)', math.floor(1.5), 1) self.ftest('floor(-0.5)', math.floor(-0.5), -1) self.ftest('floor(-1.0)', math.floor(-1.0), -1) self.ftest('floor(-1.5)', math.floor(-1.5), -2) # pow() relies on floor() to check for integers # This fails on some platforms - so check it here self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) self.assertEqual(math.ceil(INF), INF) self.assertEqual(math.ceil(NINF), NINF) self.assertTrue(math.isnan(math.floor(NAN))) class TestFloor(object): def __float__(self): return 42.3 class TestNoFloor(object): pass self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42) self.assertRaises(TypeError, math.floor, TestNoFloor()) t = TestNoFloor() t.__floor__ = lambda *args: args self.assertRaises(TypeError, math.floor, t) self.assertRaises(TypeError, math.floor, t, 0)
def testCosh(self): self.assertRaises(TypeError, math.cosh) self.ftest('cosh(0)', math.cosh(0), 1) self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert self.assertEqual(math.cosh(INF), INF) self.assertEqual(math.cosh(NINF), INF) self.assertTrue(math.isnan(math.cosh(NAN)))
def testCeil(self): self.assertRaises(TypeError, math.ceil) # These types will be int in py3k. #self.assertEqual(float, type(math.ceil(1))) #self.assertEqual(float, type(math.ceil(1L))) #self.assertEqual(float, type(math.ceil(1.0))) self.ftest('ceil(0.5)', math.ceil(0.5), 1) self.ftest('ceil(1.0)', math.ceil(1.0), 1) self.ftest('ceil(1.5)', math.ceil(1.5), 2) self.ftest('ceil(-0.5)', math.ceil(-0.5), 0) self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) self.assertEqual(math.ceil(INF), INF) self.assertEqual(math.ceil(NINF), NINF) self.assertTrue(math.isnan(math.ceil(NAN))) class TestCeil(object): def __float__(self): return 41.3 class TestNoCeil(object): pass self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) self.assertRaises(TypeError, math.ceil, TestNoCeil()) t = TestNoCeil() t.__ceil__ = lambda *args: args self.assertRaises(TypeError, math.ceil, t) self.assertRaises(TypeError, math.ceil, t, 0)
def testCeil(self): self.assertRaises(TypeError, math.ceil) # These types will be int in py3k. self.assertEquals(type(math.cf(0)), type(math.ceil(1))) self.assertEquals(type(math.cf(0)), type(math.ceil(1L))) self.assertEquals(type(math.cf(0)), type(math.ceil(1.0))) #self.assertEqual(float, type(math.ceil(1))) #self.assertEqual(float, type(math.ceil(1L))) #self.assertEqual(float, type(math.ceil(1.0))) self.ftest('ceil(0.5)', math.ceil(0.5), 1) self.ftest('ceil(1.0)', math.ceil(1.0), 1) self.ftest('ceil(1.5)', math.ceil(1.5), 2) self.ftest('ceil(-0.5)', math.ceil(-0.5), 0) self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) self.assertEqual(math.ceil(INF), INF) self.assertEqual(math.ceil(NINF), NINF) self.assertTrue(math.isnan(math.ceil(NAN))) class TestCeil(object): def __float__(self): return 41.3 class TestNoCeil(object): pass self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) self.assertRaises(TypeError, math.ceil, TestNoCeil()) t = TestNoCeil() t.__ceil__ = lambda *args: args self.assertRaises(TypeError, math.ceil, t) self.assertRaises(TypeError, math.ceil, t, 0)
def testLog10(self): self.assertRaises(TypeError, math.log10) self.ftest('log10(0.1)', math.log10(0.1), -1) self.ftest('log10(1)', math.log10(1), 0) self.ftest('log10(10)', math.log10(10), 1) self.assertEqual(math.log(INF), INF) self.assertRaises(ValueError, math.log10, NINF) self.assertTrue(math.isnan(math.log10(NAN)))
def testExp(self): self.assertRaises(TypeError, math.exp) self.ftest('exp(-1)', math.exp(-1), 1/math.e) self.ftest('exp(0)', math.exp(0), 1) self.ftest('exp(1)', math.exp(1), math.e) self.assertEqual(math.exp(INF), INF) self.assertEqual(math.exp(NINF), 0.) self.assertTrue(math.isnan(math.exp(NAN)))
def testAsinh(self): self.assertRaises(TypeError, math.asinh) self.ftest('asinh(0)', math.asinh(0), 0) self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305) self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305) self.assertEqual(math.asinh(INF), INF) self.assertEqual(math.asinh(NINF), NINF) self.assertTrue(math.isnan(math.asinh(NAN)))
def testSqrt(self): self.assertRaises(TypeError, math.sqrt) self.ftest('sqrt(0)', math.sqrt(0), 0) self.ftest('sqrt(1)', math.sqrt(1), 1) self.ftest('sqrt(4)', math.sqrt(4), 2) self.assertEqual(math.sqrt(INF), INF) self.assertRaises(ValueError, math.sqrt, NINF) self.assertTrue(math.isnan(math.sqrt(NAN)))
def testSinh(self): self.assertRaises(TypeError, math.sinh) self.ftest('sinh(0)', math.sinh(0), 0) self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) self.assertEqual(math.sinh(INF), INF) self.assertEqual(math.sinh(NINF), NINF) self.assertTrue(math.isnan(math.sinh(NAN)))
def testAcos(self): self.assertRaises(TypeError, math.acos) self.ftest('acos(-1)', math.acos(-1), math.pi) self.ftest('acos(0)', math.acos(0), math.pi/2) self.ftest('acos(1)', math.acos(1), 0) self.assertRaises(ValueError, math.acos, INF) self.assertRaises(ValueError, math.acos, NINF) self.assertTrue(math.isnan(math.acos(NAN)))
def testAsin(self): self.assertRaises(TypeError, math.asin) self.ftest('asin(-1)', math.asin(-1), -math.pi/2) self.ftest('asin(0)', math.asin(0), 0) self.ftest('asin(1)', math.asin(1), math.pi/2) self.assertRaises(ValueError, math.asin, INF) self.assertRaises(ValueError, math.asin, NINF) self.assertTrue(math.isnan(math.asin(NAN)))
def testAtan(self): self.assertRaises(TypeError, math.atan) self.ftest('atan(-1)', math.atan(-1), -math.pi/4) self.ftest('atan(0)', math.atan(0), 0) self.ftest('atan(1)', math.atan(1), math.pi/4) self.ftest('atan(inf)', math.atan(INF), math.pi/2) self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2) self.assertTrue(math.isnan(math.atan(NAN)))
def testModf(self): self.assertRaises(TypeError, math.modf) def testmodf(name, result, expected): (v1, v2), (e1, e2) = result, expected if abs(v1-e1) > eps or abs(v2-e2): self.fail('%s returned %r, expected %r'%\ (name, (v1,v2), (e1,e2))) testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) self.assertEqual(math.modf(INF), (0.0, INF)) self.assertEqual(math.modf(NINF), (-0.0, NINF)) modf_nan = math.modf(NAN) self.assertTrue(math.isnan(modf_nan[0])) self.assertTrue(math.isnan(modf_nan[1]))
def testAcosh(self): self.assertRaises(TypeError, math.acosh) self.ftest('acosh(1)', math.acosh(1), 0) self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168) self.assertRaises(ValueError, math.acosh, 0) self.assertRaises(ValueError, math.acosh, -1) self.assertEqual(math.acosh(INF), INF) self.assertRaises(ValueError, math.acosh, NINF) self.assertTrue(math.isnan(math.acosh(NAN)))
def testAtanh(self): self.assertRaises(TypeError, math.atan) self.ftest('atanh(0)', math.atanh(0), 0) self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489) self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489) self.assertRaises(ValueError, math.atanh, 1) self.assertRaises(ValueError, math.atanh, -1) self.assertRaises(ValueError, math.atanh, INF) self.assertRaises(ValueError, math.atanh, NINF) self.assertTrue(math.isnan(math.atanh(NAN)))
def testLog(self): self.assertRaises(TypeError, math.log) self.ftest('log(1/e)', math.log(1/math.e), -1) self.ftest('log(1)', math.log(1), 0) self.ftest('log(e)', math.log(math.e), 1) self.ftest('log(32,2)', math.log(32,2), 5) self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) self.assertEqual(math.log(INF), INF) self.assertRaises(ValueError, math.log, NINF) self.assertTrue(math.isnan(math.log(NAN)))
def testFmod(self): self.assertRaises(TypeError, math.fmod) self.ftest('fmod(10,1)', math.fmod(10,1), 0) self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0) self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1) self.ftest('fmod(-10,1)', math.fmod(-10,1), 0) self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0) self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1) self.assertTrue(math.isnan(math.fmod(NAN, 1.))) self.assertTrue(math.isnan(math.fmod(1., NAN))) self.assertTrue(math.isnan(math.fmod(NAN, NAN))) self.assertRaises(ValueError, math.fmod, 1., 0.) self.assertRaises(ValueError, math.fmod, INF, 1.) self.assertRaises(ValueError, math.fmod, NINF, 1.) self.assertRaises(ValueError, math.fmod, INF, 0.) self.assertEqual(math.fmod(3.0, INF), 3.0) self.assertEqual(math.fmod(-3.0, INF), -3.0) self.assertEqual(math.fmod(3.0, NINF), 3.0) self.assertEqual(math.fmod(-3.0, NINF), -3.0) self.assertEqual(math.fmod(0.0, 3.0), 0.0) self.assertEqual(math.fmod(0.0, NINF), 0.0)
def testTanh(self): self.assertRaises(TypeError, math.tanh) self.ftest('tanh(0)', math.tanh(0), 0) self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) self.ftest('tanh(inf)', math.tanh(INF), 1) self.ftest('tanh(-inf)', math.tanh(NINF), -1) self.assertTrue(math.isnan(math.tanh(NAN))) # check that tanh(-0.) == -0. on IEEE 754 systems if float.__getformat__("double").startswith("IEEE"): self.assertEqual(math.tanh(-0.), -0.) self.assertEqual(math.copysign(1., math.tanh(-0.)), math.copysign(1., -0.))
def testLog1p(self): self.assertRaises(TypeError, math.log1p) self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1) self.ftest('log1p(0)', math.log1p(0), 0) self.ftest('log1p(e-1)', math.log1p(math.e-1), 1) self.ftest('log1p(1)', math.log1p(1), math.log(2)) self.assertEqual(math.log1p(INF), INF) self.assertRaises(ValueError, math.log1p, NINF) self.assertTrue(math.isnan(math.log1p(NAN))) n= 2**90 self.assertAlmostEqual(math.log1p(n), 62.383246250395075) self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
def testLdexp(self): self.assertRaises(TypeError, math.ldexp) self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) #self.assertRaises(OverflowError, math.ldexp, 1., 1000000) #self.assertRaises(OverflowError, math.ldexp, -1., 1000000) self.assertTrue(math.ldexp(1.0,100000)>0) self.assertTrue(math.ldexp(-1.0,100000)<0) #cf don't return +-0 for big numbers in ldexp function #self.assertEquals(math.ldexp(1., -1000000), 0.) #self.assertEquals(math.ldexp(-1., -1000000), -0.) self.assertEquals(math.ldexp(INF, 30), INF) self.assertEquals(math.ldexp(NINF, -213), NINF) self.assert_(math.isnan(math.ldexp(NAN, 0))) # large second argument for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]: self.assertEquals(math.ldexp(INF, -n), INF) self.assertEquals(math.ldexp(NINF, -n), NINF) #cf doesn't return 0/-0 #self.assertEquals(math.ldexp(1., -n), 0.) #self.assertEquals(math.ldexp(-1., -n), -0.) self.assertEquals(math.ldexp(0., -n), 0.) self.assertEquals(math.ldexp(-0., -n), -0.) self.assert_(math.isnan(math.ldexp(NAN, -n))) #cf doesn't raise exceptions #self.assertRaises(OverflowError, math.ldexp, 1., n) #self.assertRaises(OverflowError, math.ldexp, -1., n) self.assertEquals(math.ldexp(0., n), 0.) self.assertEquals(math.ldexp(-0., n), -0.) self.assertEquals(math.ldexp(INF, n), INF) self.assertEquals(math.ldexp(NINF, n), NINF) self.assert_(math.isnan(math.ldexp(NAN, n)))
def testLdexp(self): self.assertRaises(TypeError, math.ldexp) self.ftest('ldexp(0,1)', math.ldexp(0, 1), 0) self.ftest('ldexp(1,1)', math.ldexp(1, 1), 2) self.ftest('ldexp(1,-1)', math.ldexp(1, -1), 0.5) self.ftest('ldexp(-1,1)', math.ldexp(-1, 1), -2) #self.assertRaises(OverflowError, math.ldexp, 1., 1000000) #self.assertRaises(OverflowError, math.ldexp, -1., 1000000) self.assertTrue(math.ldexp(1.0, 100000) > 0) self.assertTrue(math.ldexp(-1.0, 100000) < 0) #cf don't return +-0 for big numbers in ldexp function #self.assertEquals(math.ldexp(1., -1000000), 0.) #self.assertEquals(math.ldexp(-1., -1000000), -0.) self.assertEquals(math.ldexp(INF, 30), INF) self.assertEquals(math.ldexp(NINF, -213), NINF) self.assert_(math.isnan(math.ldexp(NAN, 0))) # large second argument for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]: self.assertEquals(math.ldexp(INF, -n), INF) self.assertEquals(math.ldexp(NINF, -n), NINF) #cf doesn't return 0/-0 #self.assertEquals(math.ldexp(1., -n), 0.) #self.assertEquals(math.ldexp(-1., -n), -0.) self.assertEquals(math.ldexp(0., -n), 0.) self.assertEquals(math.ldexp(-0., -n), -0.) self.assert_(math.isnan(math.ldexp(NAN, -n))) #cf doesn't raise exceptions #self.assertRaises(OverflowError, math.ldexp, 1., n) #self.assertRaises(OverflowError, math.ldexp, -1., n) self.assertEquals(math.ldexp(0., n), 0.) self.assertEquals(math.ldexp(-0., n), -0.) self.assertEquals(math.ldexp(INF, n), INF) self.assertEquals(math.ldexp(NINF, n), NINF) self.assert_(math.isnan(math.ldexp(NAN, n)))
def testFrexp(self): self.assertRaises(TypeError, math.frexp) def testfrexp(name, result, expected): (mant, exp), (emant, eexp) = result, expected if abs(mant-emant) > eps or exp != eexp: self.fail('%s returned %r, expected %r'%\ (name, (mant, exp), (emant,eexp))) testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) testfrexp('frexp(0)', math.frexp(0), (0, 0)) testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) self.assertEqual(math.frexp(INF)[0], INF) self.assertEqual(math.frexp(NINF)[0], NINF) self.assertTrue(math.isnan(math.frexp(NAN)[0]))
def testPow(self): self.assertRaises(TypeError, math.pow) self.ftest('pow(0,1)', math.pow(0,1), 0) self.ftest('pow(1,0)', math.pow(1,0), 1) self.ftest('pow(2,1)', math.pow(2,1), 2) self.ftest('pow(2,-1)', math.pow(2,-1), 0.5) self.assertEqual(math.pow(INF, 1), INF) self.assertEqual(math.pow(NINF, 1), NINF) self.assertEqual((math.pow(1, INF)), 1.) self.assertEqual((math.pow(1, NINF)), 1.) self.assertTrue(math.isnan(math.pow(NAN, 1))) self.assertTrue(math.isnan(math.pow(2, NAN))) self.assertTrue(math.isnan(math.pow(0, NAN))) self.assertEqual(math.pow(1, NAN), 1) # pow(0., x) self.assertEqual(math.pow(0., INF), 0.) self.assertEqual(math.pow(0., 3.), 0.) self.assertEqual(math.pow(0., 2.3), 0.) self.assertEqual(math.pow(0., 2.), 0.) self.assertEqual(math.pow(0., 0.), 1.) self.assertEqual(math.pow(0., -0.), 1.) self.assertRaises(ValueError, math.pow, 0., -2.) self.assertRaises(ValueError, math.pow, 0., -2.3) self.assertRaises(ValueError, math.pow, 0., -3.) self.assertRaises(ValueError, math.pow, 0., NINF) self.assertTrue(math.isnan(math.pow(0., NAN))) # pow(INF, x) self.assertEqual(math.pow(INF, INF), INF) self.assertEqual(math.pow(INF, 3.), INF) self.assertEqual(math.pow(INF, 2.3), INF) self.assertEqual(math.pow(INF, 2.), INF) self.assertEqual(math.pow(INF, 0.), 1.) self.assertEqual(math.pow(INF, -0.), 1.) self.assertEqual(math.pow(INF, -2.), 0.) self.assertEqual(math.pow(INF, -2.3), 0.) self.assertEqual(math.pow(INF, -3.), 0.) self.assertEqual(math.pow(INF, NINF), 0.) self.assertTrue(math.isnan(math.pow(INF, NAN))) # pow(-0., x) self.assertEqual(math.pow(-0., INF), 0.) self.assertEqual(math.pow(-0., 3.), -0.) self.assertEqual(math.pow(-0., 2.3), 0.) self.assertEqual(math.pow(-0., 2.), 0.) self.assertEqual(math.pow(-0., 0.), 1.) self.assertEqual(math.pow(-0., -0.), 1.) self.assertRaises(ValueError, math.pow, -0., -2.) self.assertRaises(ValueError, math.pow, -0., -2.3) self.assertRaises(ValueError, math.pow, -0., -3.) self.assertRaises(ValueError, math.pow, -0., NINF) self.assertTrue(math.isnan(math.pow(-0., NAN))) # pow(NINF, x) self.assertEqual(math.pow(NINF, INF), INF) self.assertEqual(math.pow(NINF, 3.), NINF) self.assertEqual(math.pow(NINF, 2.3), INF) self.assertEqual(math.pow(NINF, 2.), INF) self.assertEqual(math.pow(NINF, 0.), 1.) self.assertEqual(math.pow(NINF, -0.), 1.) self.assertEqual(math.pow(NINF, -2.), 0.) self.assertEqual(math.pow(NINF, -2.3), 0.) self.assertEqual(math.pow(NINF, -3.), -0.) self.assertEqual(math.pow(NINF, NINF), 0.) self.assertTrue(math.isnan(math.pow(NINF, NAN))) # pow(-1, x) self.assertEqual(math.pow(-1., INF), 1.) self.assertEqual(math.pow(-1., 3.), -1.) self.assertRaises(ValueError, math.pow, -1., 2.3) self.assertEqual(math.pow(-1., 2.), 1.) self.assertEqual(math.pow(-1., 0.), 1.) self.assertEqual(math.pow(-1., -0.), 1.) self.assertEqual(math.pow(-1., -2.), 1.) self.assertRaises(ValueError, math.pow, -1., -2.3) self.assertEqual(math.pow(-1., -3.), -1.) self.assertEqual(math.pow(-1., NINF), 1.) self.assertTrue(math.isnan(math.pow(-1., NAN))) # pow(1, x) self.assertEqual(math.pow(1., INF), 1.) self.assertEqual(math.pow(1., 3.), 1.) self.assertEqual(math.pow(1., 2.3), 1.) self.assertEqual(math.pow(1., 2.), 1.) self.assertEqual(math.pow(1., 0.), 1.) self.assertEqual(math.pow(1., -0.), 1.) self.assertEqual(math.pow(1., -2.), 1.) self.assertEqual(math.pow(1., -2.3), 1.) self.assertEqual(math.pow(1., -3.), 1.) self.assertEqual(math.pow(1., NINF), 1.) self.assertEqual(math.pow(1., NAN), 1.) # pow(x, 0) should be 1 for any x self.assertEqual(math.pow(2.3, 0.), 1.) self.assertEqual(math.pow(-2.3, 0.), 1.) self.assertEqual(math.pow(NAN, 0.), 1.) self.assertEqual(math.pow(2.3, -0.), 1.) self.assertEqual(math.pow(-2.3, -0.), 1.) self.assertEqual(math.pow(NAN, -0.), 1.) # pow(x, y) is invalid if x is negative and y is not integral self.assertRaises(ValueError, math.pow, -1., 2.3) self.assertRaises(ValueError, math.pow, -15., -3.1) # pow(x, NINF) self.assertEqual(math.pow(1.9, NINF), 0.) self.assertEqual(math.pow(1.1, NINF), 0.) self.assertEqual(math.pow(0.9, NINF), INF) self.assertEqual(math.pow(0.1, NINF), INF) self.assertEqual(math.pow(-0.1, NINF), INF) self.assertEqual(math.pow(-0.9, NINF), INF) self.assertEqual(math.pow(-1.1, NINF), 0.) self.assertEqual(math.pow(-1.9, NINF), 0.) # pow(x, INF) self.assertEqual(math.pow(1.9, INF), INF) self.assertEqual(math.pow(1.1, INF), INF) self.assertEqual(math.pow(0.9, INF), 0.) self.assertEqual(math.pow(0.1, INF), 0.) self.assertEqual(math.pow(-0.1, INF), 0.) self.assertEqual(math.pow(-0.9, INF), 0.) self.assertEqual(math.pow(-1.1, INF), INF) self.assertEqual(math.pow(-1.9, INF), INF) # pow(x, y) should work for x negative, y an integer self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0) self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0) self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0) self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0) self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0) self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5) self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25) self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125) self.assertRaises(ValueError, math.pow, -2.0, -0.5) self.assertRaises(ValueError, math.pow, -2.0, 0.5)
def testIsnan(self): self.assertTrue(math.isnan(float("nan"))) self.assertTrue(math.isnan(float("inf")* 0.)) self.assertFalse(math.isnan(float("inf"))) self.assertFalse(math.isnan(0.)) self.assertFalse(math.isnan(1.))
def testAtan2(self): self.assertRaises(TypeError, math.atan2) self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) self.ftest('atan2(0, 1)', math.atan2(0, 1), 0) self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4) self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2) # math.atan2(0, x) self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi) self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi) self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi) self.assertEqual(math.atan2(0., 0.), 0.) self.assertEqual(math.atan2(0., 2.3), 0.) self.assertEqual(math.atan2(0., INF), 0.) self.assertTrue(math.isnan(math.atan2(0., NAN))) # math.atan2(-0, x) self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi) self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi) self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi) self.assertEqual(math.atan2(-0., 0.), -0.) self.assertEqual(math.atan2(-0., 2.3), -0.) self.assertEqual(math.atan2(-0., INF), -0.) self.assertTrue(math.isnan(math.atan2(-0., NAN))) # math.atan2(INF, x) self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4) self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2) self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2) self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2) self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2) self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4) self.assertTrue(math.isnan(math.atan2(INF, NAN))) # math.atan2(NINF, x) self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4) self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2) self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2) self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2) self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2) self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4) self.assertTrue(math.isnan(math.atan2(NINF, NAN))) # math.atan2(+finite, x) self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi) self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2) self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2) self.assertEqual(math.atan2(2.3, INF), 0.) self.assertTrue(math.isnan(math.atan2(2.3, NAN))) # math.atan2(-finite, x) self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi) self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2) self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2) self.assertEqual(math.atan2(-2.3, INF), -0.) self.assertTrue(math.isnan(math.atan2(-2.3, NAN))) # math.atan2(NAN, x) self.assertTrue(math.isnan(math.atan2(NAN, NINF))) self.assertTrue(math.isnan(math.atan2(NAN, -2.3))) self.assertTrue(math.isnan(math.atan2(NAN, -0.))) self.assertTrue(math.isnan(math.atan2(NAN, 0.))) self.assertTrue(math.isnan(math.atan2(NAN, 2.3))) self.assertTrue(math.isnan(math.atan2(NAN, INF))) self.assertTrue(math.isnan(math.atan2(NAN, NAN)))