blob: aa44253d2be1bfdc53d7a8db0541652be9d79d40 [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)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +000054 self.assertEquals(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +000055 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
56 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
57 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
58 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
59 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
60 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000061
Guido van Rossum13e05de2007-08-23 22:56:55 +000062 class TestCeil:
63 def __ceil__(self):
64 return 42
65 class TestNoCeil:
66 pass
67 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
68 self.assertRaises(TypeError, math.ceil, TestNoCeil())
69
70 t = TestNoCeil()
71 t.__ceil__ = lambda *args: args
72 self.assertRaises(TypeError, math.ceil, t)
73 self.assertRaises(TypeError, math.ceil, t, 0)
74
Thomas Wouters89f507f2006-12-13 04:49:30 +000075 def testCos(self):
76 self.assertRaises(TypeError, math.cos)
77 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
78 self.ftest('cos(0)', math.cos(0), 1)
79 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
80 self.ftest('cos(pi)', math.cos(math.pi), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000081
Thomas Wouters89f507f2006-12-13 04:49:30 +000082 def testCosh(self):
83 self.assertRaises(TypeError, math.cosh)
84 self.ftest('cosh(0)', math.cosh(0), 1)
85 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 +000086
Thomas Wouters89f507f2006-12-13 04:49:30 +000087 def testDegrees(self):
88 self.assertRaises(TypeError, math.degrees)
89 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
90 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
91 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000092
Thomas Wouters89f507f2006-12-13 04:49:30 +000093 def testExp(self):
94 self.assertRaises(TypeError, math.exp)
95 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
96 self.ftest('exp(0)', math.exp(0), 1)
97 self.ftest('exp(1)', math.exp(1), math.e)
Guido van Rossumfcce6301996-08-08 18:26:25 +000098
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 def testFabs(self):
100 self.assertRaises(TypeError, math.fabs)
101 self.ftest('fabs(-1)', math.fabs(-1), 1)
102 self.ftest('fabs(0)', math.fabs(0), 0)
103 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000104
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 def testFloor(self):
106 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000107 self.assertEquals(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108 self.ftest('floor(0.5)', math.floor(0.5), 0)
109 self.ftest('floor(1.0)', math.floor(1.0), 1)
110 self.ftest('floor(1.5)', math.floor(1.5), 1)
111 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
112 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
113 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000114 # pow() relies on floor() to check for integers
115 # This fails on some platforms - so check it here
116 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
117 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000118
Guido van Rossum13e05de2007-08-23 22:56:55 +0000119 class TestFloor:
120 def __floor__(self):
121 return 42
122 class TestNoFloor:
123 pass
124 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
125 self.assertRaises(TypeError, math.floor, TestNoFloor())
126
127 t = TestNoFloor()
128 t.__floor__ = lambda *args: args
129 self.assertRaises(TypeError, math.floor, t)
130 self.assertRaises(TypeError, math.floor, t, 0)
131
Thomas Wouters89f507f2006-12-13 04:49:30 +0000132 def testFmod(self):
133 self.assertRaises(TypeError, math.fmod)
134 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
135 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
136 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
137 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
138 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
139 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000140
Thomas Wouters89f507f2006-12-13 04:49:30 +0000141 def testFrexp(self):
142 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000143
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000144 def testfrexp(name, result, expected):
145 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146 if abs(mant-emant) > eps or exp != eexp:
147 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000148 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000149
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
151 testfrexp('frexp(0)', math.frexp(0), (0, 0))
152 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
153 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000154
Thomas Wouters89f507f2006-12-13 04:49:30 +0000155 def testHypot(self):
156 self.assertRaises(TypeError, math.hypot)
157 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
158 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000159
Thomas Wouters89f507f2006-12-13 04:49:30 +0000160 def testLdexp(self):
161 self.assertRaises(TypeError, math.ldexp)
162 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
163 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
164 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
165 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000166
Thomas Wouters89f507f2006-12-13 04:49:30 +0000167 def testLog(self):
168 self.assertRaises(TypeError, math.log)
169 self.ftest('log(1/e)', math.log(1/math.e), -1)
170 self.ftest('log(1)', math.log(1), 0)
171 self.ftest('log(e)', math.log(math.e), 1)
172 self.ftest('log(32,2)', math.log(32,2), 5)
173 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
174 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000175
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176 def testLog10(self):
177 self.assertRaises(TypeError, math.log10)
178 self.ftest('log10(0.1)', math.log10(0.1), -1)
179 self.ftest('log10(1)', math.log10(1), 0)
180 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000181
Thomas Wouters89f507f2006-12-13 04:49:30 +0000182 def testModf(self):
183 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000184
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000185 def testmodf(name, result, expected):
186 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 if abs(v1-e1) > eps or abs(v2-e2):
188 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000189 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000190
Thomas Wouters89f507f2006-12-13 04:49:30 +0000191 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
192 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000193
Thomas Wouters89f507f2006-12-13 04:49:30 +0000194 def testPow(self):
195 self.assertRaises(TypeError, math.pow)
196 self.ftest('pow(0,1)', math.pow(0,1), 0)
197 self.ftest('pow(1,0)', math.pow(1,0), 1)
198 self.ftest('pow(2,1)', math.pow(2,1), 2)
199 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000200
Thomas Wouters89f507f2006-12-13 04:49:30 +0000201 def testRadians(self):
202 self.assertRaises(TypeError, math.radians)
203 self.ftest('radians(180)', math.radians(180), math.pi)
204 self.ftest('radians(90)', math.radians(90), math.pi/2)
205 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000206
Thomas Wouters89f507f2006-12-13 04:49:30 +0000207 def testSin(self):
208 self.assertRaises(TypeError, math.sin)
209 self.ftest('sin(0)', math.sin(0), 0)
210 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
211 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000212
Thomas Wouters89f507f2006-12-13 04:49:30 +0000213 def testSinh(self):
214 self.assertRaises(TypeError, math.sinh)
215 self.ftest('sinh(0)', math.sinh(0), 0)
216 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
217 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000218
Thomas Wouters89f507f2006-12-13 04:49:30 +0000219 def testSqrt(self):
220 self.assertRaises(TypeError, math.sqrt)
221 self.ftest('sqrt(0)', math.sqrt(0), 0)
222 self.ftest('sqrt(1)', math.sqrt(1), 1)
223 self.ftest('sqrt(4)', math.sqrt(4), 2)
Tim Peters1d120612000-10-12 06:10:25 +0000224
Thomas Wouters89f507f2006-12-13 04:49:30 +0000225 def testTan(self):
226 self.assertRaises(TypeError, math.tan)
227 self.ftest('tan(0)', math.tan(0), 0)
228 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
229 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Tim Peters1d120612000-10-12 06:10:25 +0000230
Thomas Wouters89f507f2006-12-13 04:49:30 +0000231 def testTanh(self):
232 self.assertRaises(TypeError, math.tanh)
233 self.ftest('tanh(0)', math.tanh(0), 0)
234 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000235
Christian Heimes400adb02008-02-01 08:12:03 +0000236 def test_trunc(self):
237 self.assertEqual(math.trunc(1), 1)
238 self.assertEqual(math.trunc(-1), -1)
239 self.assertEqual(type(math.trunc(1)), int)
240 self.assertEqual(type(math.trunc(1.5)), int)
241 self.assertEqual(math.trunc(1.5), 1)
242 self.assertEqual(math.trunc(-1.5), -1)
243 self.assertEqual(math.trunc(1.999999), 1)
244 self.assertEqual(math.trunc(-1.999999), -1)
245 self.assertEqual(math.trunc(-0.999999), -0)
246 self.assertEqual(math.trunc(-100.999), -100)
247
248 class TestTrunc(object):
249 def __trunc__(self):
250 return 23
251
252 class TestNoTrunc(object):
253 pass
254
255 self.assertEqual(math.trunc(TestTrunc()), 23)
256
257 self.assertRaises(TypeError, math.trunc)
258 self.assertRaises(TypeError, math.trunc, 1, 2)
259 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
260
261 # XXX Doesn't work because the method is looked up on
262 # the type only.
263 #t = TestNoTrunc()
264 #t.__trunc__ = lambda *args: args
265 #self.assertEquals((), math.trunc(t))
266 #self.assertRaises(TypeError, math.trunc, t, 0)
267
Christian Heimes072c0f12008-01-03 23:01:04 +0000268 def testCopysign(self):
269 self.assertEqual(math.copysign(1, 42), 1.0)
270 self.assertEqual(math.copysign(0., 42), 0.0)
271 self.assertEqual(math.copysign(1., -42), -1.0)
272 self.assertEqual(math.copysign(3, 0.), 3.0)
273 self.assertEqual(math.copysign(4., -0.), -4.0)
274
275 def testIsnan(self):
276 self.assert_(math.isnan(float("nan")))
277 self.assert_(math.isnan(float("inf")* 0.))
278 self.failIf(math.isnan(float("inf")))
279 self.failIf(math.isnan(0.))
280 self.failIf(math.isnan(1.))
281
282 def testIsinf(self):
283 self.assert_(math.isinf(float("inf")))
284 self.assert_(math.isinf(float("-inf")))
285 self.assert_(math.isinf(1E400))
286 self.assert_(math.isinf(-1E400))
287 self.failIf(math.isinf(float("nan")))
288 self.failIf(math.isinf(0.))
289 self.failIf(math.isinf(1.))
290
Thomas Wouters89f507f2006-12-13 04:49:30 +0000291 # RED_FLAG 16-Oct-2000 Tim
292 # While 2.0 is more consistent about exceptions than previous releases, it
293 # still fails this part of the test on some platforms. For now, we only
294 # *run* test_exceptions() in verbose mode, so that this isn't normally
295 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000296
Thomas Wouters89f507f2006-12-13 04:49:30 +0000297 if verbose:
298 def test_exceptions(self):
299 try:
300 x = math.exp(-1000000000)
301 except:
302 # mathmodule.c is failing to weed out underflows from libm, or
303 # we've got an fp format with huge dynamic range
304 self.fail("underflowing exp() should not have raised "
305 "an exception")
306 if x != 0:
307 self.fail("underflowing exp() should have returned 0")
308
309 # If this fails, probably using a strict IEEE-754 conforming libm, and x
310 # is +Inf afterwards. But Python wants overflows detected by default.
311 try:
312 x = math.exp(1000000000)
313 except OverflowError:
314 pass
315 else:
316 self.fail("overflowing exp() didn't trigger OverflowError")
317
318 # If this fails, it could be a puzzle. One odd possibility is that
319 # mathmodule.c's macros are getting confused while comparing
320 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
321 # as a result (and so raising OverflowError instead).
322 try:
323 x = math.sqrt(-1.0)
324 except ValueError:
325 pass
326 else:
327 self.fail("sqrt(-1) didn't raise ValueError")
328
329
330def test_main():
331 run_unittest(MathTests)
332
333if __name__ == '__main__':
334 test_main()