1996-08-08 15:26:25 -03:00
|
|
|
# Python test set -- math module
|
|
|
|
# XXXX Should not do tests around zero only
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
from test.test_support import run_unittest, verbose
|
|
|
|
import unittest
|
|
|
|
import math
|
1996-08-08 15:26:25 -03:00
|
|
|
|
1996-08-29 16:00:46 -03:00
|
|
|
seps='1e-05'
|
|
|
|
eps = eval(seps)
|
1996-08-08 15:26:25 -03:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
class MathTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def ftest(self, name, value, expected):
|
|
|
|
if abs(value-expected) > eps:
|
2007-07-27 07:36:30 -03:00
|
|
|
# Use %r instead of %f so the error message
|
|
|
|
# displays full precision. Otherwise discrepancies
|
|
|
|
# in the last few bits will lead to very confusing
|
|
|
|
# error messages
|
|
|
|
self.fail('%s returned %r, expected %r' %
|
2006-10-28 10:51:49 -03:00
|
|
|
(name, value, expected))
|
|
|
|
|
|
|
|
def testConstants(self):
|
|
|
|
self.ftest('pi', math.pi, 3.1415926)
|
|
|
|
self.ftest('e', math.e, 2.7182818)
|
|
|
|
|
|
|
|
def testAcos(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.acos)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testAsin(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.asin)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testAtan(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.atan)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testAtan2(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.atan2)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testCeil(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.ceil)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
class TestCeil(object):
|
|
|
|
def __ceil__(self):
|
|
|
|
return 42
|
|
|
|
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)
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testCos(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.cos)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testCosh(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.cosh)
|
2006-10-28 10:51:49 -03:00
|
|
|
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
|
|
|
|
|
|
|
|
def testDegrees(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.degrees)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
|
|
|
|
self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
|
|
|
|
self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
|
|
|
|
|
|
|
|
def testExp(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.exp)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testFabs(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.fabs)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('fabs(-1)', math.fabs(-1), 1)
|
|
|
|
self.ftest('fabs(0)', math.fabs(0), 0)
|
|
|
|
self.ftest('fabs(1)', math.fabs(1), 1)
|
|
|
|
|
|
|
|
def testFloor(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.floor)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
2007-07-26 11:03:00 -03:00
|
|
|
# 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)
|
2006-10-28 10:51:49 -03:00
|
|
|
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
class TestFloor(object):
|
|
|
|
def __floor__(self):
|
|
|
|
return 42
|
|
|
|
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)
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testFmod(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.fmod)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testFrexp(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.frexp)
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testfrexp(name, (mant, exp), (emant, eexp)):
|
|
|
|
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))
|
|
|
|
|
|
|
|
def testHypot(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.hypot)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('hypot(0,0)', math.hypot(0,0), 0)
|
|
|
|
self.ftest('hypot(3,4)', math.hypot(3,4), 5)
|
|
|
|
|
|
|
|
def testLdexp(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.ldexp)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testLog(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.log)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testLog10(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.log10)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testModf(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.modf)
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testmodf(name, (v1, v2), (e1, e2)):
|
|
|
|
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))
|
2006-11-02 22:32:46 -04:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testPow(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.pow)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testRadians(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.radians)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('radians(180)', math.radians(180), math.pi)
|
|
|
|
self.ftest('radians(90)', math.radians(90), math.pi/2)
|
|
|
|
self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
|
|
|
|
|
|
|
|
def testSin(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.sin)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testSinh(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.sinh)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testSqrt(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.sqrt)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('sqrt(0)', math.sqrt(0), 0)
|
|
|
|
self.ftest('sqrt(1)', math.sqrt(1), 1)
|
|
|
|
self.ftest('sqrt(4)', math.sqrt(4), 2)
|
|
|
|
|
|
|
|
def testTan(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.tan)
|
2006-10-28 10:51:49 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def testTanh(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.tanh)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('tanh(0)', math.tanh(0), 0)
|
|
|
|
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
|
|
|
|
|
2008-01-03 18:32:26 -04:00
|
|
|
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)
|
|
|
|
|
2008-01-03 16:23:15 -04:00
|
|
|
def testIsnan(self):
|
|
|
|
self.assert_(math.isnan(float("nan")))
|
|
|
|
self.assert_(math.isnan(float("inf")* 0.))
|
|
|
|
self.failIf(math.isnan(float("inf")))
|
|
|
|
self.failIf(math.isnan(0.))
|
|
|
|
self.failIf(math.isnan(1.))
|
|
|
|
|
|
|
|
def testIsinf(self):
|
|
|
|
self.assert_(math.isinf(float("inf")))
|
|
|
|
self.assert_(math.isinf(float("-inf")))
|
|
|
|
self.assert_(math.isinf(1E400))
|
|
|
|
self.assert_(math.isinf(-1E400))
|
|
|
|
self.failIf(math.isinf(float("nan")))
|
|
|
|
self.failIf(math.isinf(0.))
|
|
|
|
self.failIf(math.isinf(1.))
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
# RED_FLAG 16-Oct-2000 Tim
|
|
|
|
# While 2.0 is more consistent about exceptions than previous releases, it
|
|
|
|
# still fails this part of the test on some platforms. For now, we only
|
|
|
|
# *run* test_exceptions() in verbose mode, so that this isn't normally
|
|
|
|
# tested.
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
def test_exceptions(self):
|
|
|
|
try:
|
|
|
|
x = math.exp(-1000000000)
|
|
|
|
except:
|
|
|
|
# mathmodule.c is failing to weed out underflows from libm, or
|
|
|
|
# we've got an fp format with huge dynamic range
|
|
|
|
self.fail("underflowing exp() should not have raised "
|
|
|
|
"an exception")
|
|
|
|
if x != 0:
|
|
|
|
self.fail("underflowing exp() should have returned 0")
|
|
|
|
|
|
|
|
# If this fails, probably using a strict IEEE-754 conforming libm, and x
|
|
|
|
# is +Inf afterwards. But Python wants overflows detected by default.
|
|
|
|
try:
|
|
|
|
x = math.exp(1000000000)
|
|
|
|
except OverflowError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("overflowing exp() didn't trigger OverflowError")
|
|
|
|
|
|
|
|
# If this fails, it could be a puzzle. One odd possibility is that
|
|
|
|
# mathmodule.c's macros are getting confused while comparing
|
|
|
|
# Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
|
|
|
|
# as a result (and so raising OverflowError instead).
|
|
|
|
try:
|
|
|
|
x = math.sqrt(-1.0)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("sqrt(-1) didn't raise ValueError")
|
|
|
|
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(MathTests)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|