blob: 2d15f3aa41b5e9487ae7babb5b9bfc69ea7a7d60 [file] [log] [blame]
Guido van Rossumfcce6301996-08-08 18:26:25 +00001# Python test set -- math module
2# XXXX Should not do tests around zero only
3
Thomas Wouters89f507f2006-12-13 04:49:30 +00004from test.test_support import run_unittest, verbose
5import unittest
6import math
Guido van Rossumfcce6301996-08-08 18:26:25 +00007
Guido van Rossum5ab007b1996-08-29 19:00:46 +00008seps='1e-05'
9eps = eval(seps)
Guido van Rossumfcce6301996-08-08 18:26:25 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000012
Thomas Wouters89f507f2006-12-13 04:49:30 +000013 def ftest(self, name, value, expected):
14 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +000015 # Use %r instead of %f so the error message
16 # displays full precision. Otherwise discrepancies
17 # in the last few bits will lead to very confusing
18 # error messages
19 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000021
Thomas Wouters89f507f2006-12-13 04:49:30 +000022 def testConstants(self):
23 self.ftest('pi', math.pi, 3.1415926)
24 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000025
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 def testAcos(self):
27 self.assertRaises(TypeError, math.acos)
28 self.ftest('acos(-1)', math.acos(-1), math.pi)
29 self.ftest('acos(0)', math.acos(0), math.pi/2)
30 self.ftest('acos(1)', math.acos(1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000031
Thomas Wouters89f507f2006-12-13 04:49:30 +000032 def testAsin(self):
33 self.assertRaises(TypeError, math.asin)
34 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
35 self.ftest('asin(0)', math.asin(0), 0)
36 self.ftest('asin(1)', math.asin(1), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000037
Thomas Wouters89f507f2006-12-13 04:49:30 +000038 def testAtan(self):
39 self.assertRaises(TypeError, math.atan)
40 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
41 self.ftest('atan(0)', math.atan(0), 0)
42 self.ftest('atan(1)', math.atan(1), math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +000043
Thomas Wouters89f507f2006-12-13 04:49:30 +000044 def testAtan2(self):
45 self.assertRaises(TypeError, math.atan2)
46 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
47 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
48 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
49 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
50 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000051
Thomas Wouters89f507f2006-12-13 04:49:30 +000052 def testCeil(self):
53 self.assertRaises(TypeError, math.ceil)
54 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
55 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
56 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
57 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
58 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
59 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000060
Guido van Rossum13e05de2007-08-23 22:56:55 +000061 class TestCeil:
62 def __ceil__(self):
63 return 42
64 class TestNoCeil:
65 pass
66 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
67 self.assertRaises(TypeError, math.ceil, TestNoCeil())
68
69 t = TestNoCeil()
70 t.__ceil__ = lambda *args: args
71 self.assertRaises(TypeError, math.ceil, t)
72 self.assertRaises(TypeError, math.ceil, t, 0)
73
Thomas Wouters89f507f2006-12-13 04:49:30 +000074 def testCos(self):
75 self.assertRaises(TypeError, math.cos)
76 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
77 self.ftest('cos(0)', math.cos(0), 1)
78 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
79 self.ftest('cos(pi)', math.cos(math.pi), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000080
Thomas Wouters89f507f2006-12-13 04:49:30 +000081 def testCosh(self):
82 self.assertRaises(TypeError, math.cosh)
83 self.ftest('cosh(0)', math.cosh(0), 1)
84 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
Raymond Hettinger64108af2002-05-13 03:55:01 +000085
Thomas Wouters89f507f2006-12-13 04:49:30 +000086 def testDegrees(self):
87 self.assertRaises(TypeError, math.degrees)
88 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
89 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
90 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000091
Thomas Wouters89f507f2006-12-13 04:49:30 +000092 def testExp(self):
93 self.assertRaises(TypeError, math.exp)
94 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
95 self.ftest('exp(0)', math.exp(0), 1)
96 self.ftest('exp(1)', math.exp(1), math.e)
Guido van Rossumfcce6301996-08-08 18:26:25 +000097
Thomas Wouters89f507f2006-12-13 04:49:30 +000098 def testFabs(self):
99 self.assertRaises(TypeError, math.fabs)
100 self.ftest('fabs(-1)', math.fabs(-1), 1)
101 self.ftest('fabs(0)', math.fabs(0), 0)
102 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000103
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104 def testFloor(self):
105 self.assertRaises(TypeError, math.floor)
106 self.ftest('floor(0.5)', math.floor(0.5), 0)
107 self.ftest('floor(1.0)', math.floor(1.0), 1)
108 self.ftest('floor(1.5)', math.floor(1.5), 1)
109 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
110 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
111 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000112 # pow() relies on floor() to check for integers
113 # This fails on some platforms - so check it here
114 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
115 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000116
Guido van Rossum13e05de2007-08-23 22:56:55 +0000117 class TestFloor:
118 def __floor__(self):
119 return 42
120 class TestNoFloor:
121 pass
122 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
123 self.assertRaises(TypeError, math.floor, TestNoFloor())
124
125 t = TestNoFloor()
126 t.__floor__ = lambda *args: args
127 self.assertRaises(TypeError, math.floor, t)
128 self.assertRaises(TypeError, math.floor, t, 0)
129
Thomas Wouters89f507f2006-12-13 04:49:30 +0000130 def testFmod(self):
131 self.assertRaises(TypeError, math.fmod)
132 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
133 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
134 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
135 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
136 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
137 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000138
Thomas Wouters89f507f2006-12-13 04:49:30 +0000139 def testFrexp(self):
140 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000141
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000142 def testfrexp(name, result, expected):
143 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000144 if abs(mant-emant) > eps or exp != eexp:
145 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000146 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000147
Thomas Wouters89f507f2006-12-13 04:49:30 +0000148 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
149 testfrexp('frexp(0)', math.frexp(0), (0, 0))
150 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
151 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000152
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153 def testHypot(self):
154 self.assertRaises(TypeError, math.hypot)
155 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
156 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000157
Thomas Wouters89f507f2006-12-13 04:49:30 +0000158 def testLdexp(self):
159 self.assertRaises(TypeError, math.ldexp)
160 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
161 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
162 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
163 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000164
Thomas Wouters89f507f2006-12-13 04:49:30 +0000165 def testLog(self):
166 self.assertRaises(TypeError, math.log)
167 self.ftest('log(1/e)', math.log(1/math.e), -1)
168 self.ftest('log(1)', math.log(1), 0)
169 self.ftest('log(e)', math.log(math.e), 1)
170 self.ftest('log(32,2)', math.log(32,2), 5)
171 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
172 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000173
Thomas Wouters89f507f2006-12-13 04:49:30 +0000174 def testLog10(self):
175 self.assertRaises(TypeError, math.log10)
176 self.ftest('log10(0.1)', math.log10(0.1), -1)
177 self.ftest('log10(1)', math.log10(1), 0)
178 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000179
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180 def testModf(self):
181 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000182
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000183 def testmodf(name, result, expected):
184 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000185 if abs(v1-e1) > eps or abs(v2-e2):
186 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000187 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000188
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
190 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000191
Thomas Wouters89f507f2006-12-13 04:49:30 +0000192 def testPow(self):
193 self.assertRaises(TypeError, math.pow)
194 self.ftest('pow(0,1)', math.pow(0,1), 0)
195 self.ftest('pow(1,0)', math.pow(1,0), 1)
196 self.ftest('pow(2,1)', math.pow(2,1), 2)
197 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000198
Thomas Wouters89f507f2006-12-13 04:49:30 +0000199 def testRadians(self):
200 self.assertRaises(TypeError, math.radians)
201 self.ftest('radians(180)', math.radians(180), math.pi)
202 self.ftest('radians(90)', math.radians(90), math.pi/2)
203 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000204
Thomas Wouters89f507f2006-12-13 04:49:30 +0000205 def testSin(self):
206 self.assertRaises(TypeError, math.sin)
207 self.ftest('sin(0)', math.sin(0), 0)
208 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
209 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000210
Thomas Wouters89f507f2006-12-13 04:49:30 +0000211 def testSinh(self):
212 self.assertRaises(TypeError, math.sinh)
213 self.ftest('sinh(0)', math.sinh(0), 0)
214 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
215 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000216
Thomas Wouters89f507f2006-12-13 04:49:30 +0000217 def testSqrt(self):
218 self.assertRaises(TypeError, math.sqrt)
219 self.ftest('sqrt(0)', math.sqrt(0), 0)
220 self.ftest('sqrt(1)', math.sqrt(1), 1)
221 self.ftest('sqrt(4)', math.sqrt(4), 2)
Tim Peters1d120612000-10-12 06:10:25 +0000222
Thomas Wouters89f507f2006-12-13 04:49:30 +0000223 def testTan(self):
224 self.assertRaises(TypeError, math.tan)
225 self.ftest('tan(0)', math.tan(0), 0)
226 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
227 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Tim Peters1d120612000-10-12 06:10:25 +0000228
Thomas Wouters89f507f2006-12-13 04:49:30 +0000229 def testTanh(self):
230 self.assertRaises(TypeError, math.tanh)
231 self.ftest('tanh(0)', math.tanh(0), 0)
232 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000233
Thomas Wouters89f507f2006-12-13 04:49:30 +0000234 # RED_FLAG 16-Oct-2000 Tim
235 # While 2.0 is more consistent about exceptions than previous releases, it
236 # still fails this part of the test on some platforms. For now, we only
237 # *run* test_exceptions() in verbose mode, so that this isn't normally
238 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000239
Thomas Wouters89f507f2006-12-13 04:49:30 +0000240 if verbose:
241 def test_exceptions(self):
242 try:
243 x = math.exp(-1000000000)
244 except:
245 # mathmodule.c is failing to weed out underflows from libm, or
246 # we've got an fp format with huge dynamic range
247 self.fail("underflowing exp() should not have raised "
248 "an exception")
249 if x != 0:
250 self.fail("underflowing exp() should have returned 0")
251
252 # If this fails, probably using a strict IEEE-754 conforming libm, and x
253 # is +Inf afterwards. But Python wants overflows detected by default.
254 try:
255 x = math.exp(1000000000)
256 except OverflowError:
257 pass
258 else:
259 self.fail("overflowing exp() didn't trigger OverflowError")
260
261 # If this fails, it could be a puzzle. One odd possibility is that
262 # mathmodule.c's macros are getting confused while comparing
263 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
264 # as a result (and so raising OverflowError instead).
265 try:
266 x = math.sqrt(-1.0)
267 except ValueError:
268 pass
269 else:
270 self.fail("sqrt(-1) didn't raise ValueError")
271
272
273def test_main():
274 run_unittest(MathTests)
275
276if __name__ == '__main__':
277 test_main()