blob: 16f0f4df760c30d500ef59b5ebd2f88fc0a41337 [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
Georg Brandl2f037602006-10-28 13:51:49 +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
Georg Brandl2f037602006-10-28 13:51:49 +000011class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000012
Georg Brandl2f037602006-10-28 13:51:49 +000013 def ftest(self, name, value, expected):
14 if abs(value-expected) > eps:
Nick Coghlanec2ce9b2007-07-27 10:36:30 +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' %
Georg Brandl2f037602006-10-28 13:51:49 +000020 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000021
Georg Brandl2f037602006-10-28 13:51:49 +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
Georg Brandl2f037602006-10-28 13:51:49 +000026 def testAcos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000027 self.assertRaises(TypeError, math.acos)
Georg Brandl2f037602006-10-28 13:51:49 +000028 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
Georg Brandl2f037602006-10-28 13:51:49 +000032 def testAsin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000033 self.assertRaises(TypeError, math.asin)
Georg Brandl2f037602006-10-28 13:51:49 +000034 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
Georg Brandl2f037602006-10-28 13:51:49 +000038 def testAtan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000039 self.assertRaises(TypeError, math.atan)
Georg Brandl2f037602006-10-28 13:51:49 +000040 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
Georg Brandl2f037602006-10-28 13:51:49 +000044 def testAtan2(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000045 self.assertRaises(TypeError, math.atan2)
Georg Brandl2f037602006-10-28 13:51:49 +000046 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
Georg Brandl2f037602006-10-28 13:51:49 +000052 def testCeil(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000053 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +000054 # These types will be int in py3k.
55 self.assertEquals(float, type(math.ceil(1)))
56 self.assertEquals(float, type(math.ceil(1L)))
57 self.assertEquals(float, type(math.ceil(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +000058 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
59 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
60 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
61 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
62 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
63 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000064
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +000065 class TestCeil(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +000066 def __float__(self):
67 return 41.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +000068 class TestNoCeil(object):
69 pass
70 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
71 self.assertRaises(TypeError, math.ceil, TestNoCeil())
72
73 t = TestNoCeil()
74 t.__ceil__ = lambda *args: args
75 self.assertRaises(TypeError, math.ceil, t)
76 self.assertRaises(TypeError, math.ceil, t, 0)
77
Georg Brandl2f037602006-10-28 13:51:49 +000078 def testCos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000079 self.assertRaises(TypeError, math.cos)
Georg Brandl2f037602006-10-28 13:51:49 +000080 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
81 self.ftest('cos(0)', math.cos(0), 1)
82 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
83 self.ftest('cos(pi)', math.cos(math.pi), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000084
Georg Brandl2f037602006-10-28 13:51:49 +000085 def testCosh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000086 self.assertRaises(TypeError, math.cosh)
Georg Brandl2f037602006-10-28 13:51:49 +000087 self.ftest('cosh(0)', math.cosh(0), 1)
88 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 +000089
Georg Brandl2f037602006-10-28 13:51:49 +000090 def testDegrees(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000091 self.assertRaises(TypeError, math.degrees)
Georg Brandl2f037602006-10-28 13:51:49 +000092 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
93 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
94 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000095
Georg Brandl2f037602006-10-28 13:51:49 +000096 def testExp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000097 self.assertRaises(TypeError, math.exp)
Georg Brandl2f037602006-10-28 13:51:49 +000098 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
99 self.ftest('exp(0)', math.exp(0), 1)
100 self.ftest('exp(1)', math.exp(1), math.e)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000101
Georg Brandl2f037602006-10-28 13:51:49 +0000102 def testFabs(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000103 self.assertRaises(TypeError, math.fabs)
Georg Brandl2f037602006-10-28 13:51:49 +0000104 self.ftest('fabs(-1)', math.fabs(-1), 1)
105 self.ftest('fabs(0)', math.fabs(0), 0)
106 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000107
Georg Brandl2f037602006-10-28 13:51:49 +0000108 def testFloor(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000109 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000110 # These types will be int in py3k.
111 self.assertEquals(float, type(math.floor(1)))
112 self.assertEquals(float, type(math.floor(1L)))
113 self.assertEquals(float, type(math.floor(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000114 self.ftest('floor(0.5)', math.floor(0.5), 0)
115 self.ftest('floor(1.0)', math.floor(1.0), 1)
116 self.ftest('floor(1.5)', math.floor(1.5), 1)
117 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
118 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
119 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Nick Coghlan00f20292007-07-26 14:03:00 +0000120 # pow() relies on floor() to check for integers
121 # This fails on some platforms - so check it here
122 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
123 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000124
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000125 class TestFloor(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000126 def __float__(self):
127 return 42.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000128 class TestNoFloor(object):
129 pass
130 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
131 self.assertRaises(TypeError, math.floor, TestNoFloor())
132
133 t = TestNoFloor()
134 t.__floor__ = lambda *args: args
135 self.assertRaises(TypeError, math.floor, t)
136 self.assertRaises(TypeError, math.floor, t, 0)
137
Georg Brandl2f037602006-10-28 13:51:49 +0000138 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000139 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +0000140 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
141 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
142 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
143 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
144 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
145 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000146
Georg Brandl2f037602006-10-28 13:51:49 +0000147 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000148 self.assertRaises(TypeError, math.frexp)
149
Georg Brandl2f037602006-10-28 13:51:49 +0000150 def testfrexp(name, (mant, exp), (emant, eexp)):
151 if abs(mant-emant) > eps or exp != eexp:
152 self.fail('%s returned %r, expected %r'%\
153 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000154
Georg Brandl2f037602006-10-28 13:51:49 +0000155 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
156 testfrexp('frexp(0)', math.frexp(0), (0, 0))
157 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
158 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000159
Georg Brandl2f037602006-10-28 13:51:49 +0000160 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000161 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000162 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
163 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000164
Georg Brandl2f037602006-10-28 13:51:49 +0000165 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000166 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000167 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
168 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
169 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
170 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000171
Georg Brandl2f037602006-10-28 13:51:49 +0000172 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000173 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000174 self.ftest('log(1/e)', math.log(1/math.e), -1)
175 self.ftest('log(1)', math.log(1), 0)
176 self.ftest('log(e)', math.log(math.e), 1)
177 self.ftest('log(32,2)', math.log(32,2), 5)
178 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
179 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000180
Georg Brandl2f037602006-10-28 13:51:49 +0000181 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000182 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000183 self.ftest('log10(0.1)', math.log10(0.1), -1)
184 self.ftest('log10(1)', math.log10(1), 0)
185 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000186
Georg Brandl2f037602006-10-28 13:51:49 +0000187 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000188 self.assertRaises(TypeError, math.modf)
189
Georg Brandl2f037602006-10-28 13:51:49 +0000190 def testmodf(name, (v1, v2), (e1, e2)):
191 if abs(v1-e1) > eps or abs(v2-e2):
192 self.fail('%s returned %r, expected %r'%\
193 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000194
Georg Brandl2f037602006-10-28 13:51:49 +0000195 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
196 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Tim Petersabd8a332006-11-03 02:32:46 +0000197
Georg Brandl2f037602006-10-28 13:51:49 +0000198 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000199 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000200 self.ftest('pow(0,1)', math.pow(0,1), 0)
201 self.ftest('pow(1,0)', math.pow(1,0), 1)
202 self.ftest('pow(2,1)', math.pow(2,1), 2)
203 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000204
Georg Brandl2f037602006-10-28 13:51:49 +0000205 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000206 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000207 self.ftest('radians(180)', math.radians(180), math.pi)
208 self.ftest('radians(90)', math.radians(90), math.pi/2)
209 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000210
Georg Brandl2f037602006-10-28 13:51:49 +0000211 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000212 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000213 self.ftest('sin(0)', math.sin(0), 0)
214 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
215 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000216
Georg Brandl2f037602006-10-28 13:51:49 +0000217 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000218 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000219 self.ftest('sinh(0)', math.sinh(0), 0)
220 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
221 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000222
Georg Brandl2f037602006-10-28 13:51:49 +0000223 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000224 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000225 self.ftest('sqrt(0)', math.sqrt(0), 0)
226 self.ftest('sqrt(1)', math.sqrt(1), 1)
227 self.ftest('sqrt(4)', math.sqrt(4), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000228
Georg Brandl2f037602006-10-28 13:51:49 +0000229 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000230 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000231 self.ftest('tan(0)', math.tan(0), 0)
232 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
233 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000234
Georg Brandl2f037602006-10-28 13:51:49 +0000235 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000236 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000237 self.ftest('tanh(0)', math.tanh(0), 0)
238 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000239
Christian Heimeseebb79c2008-01-03 22:32:26 +0000240 def testCopysign(self):
241 self.assertEqual(math.copysign(1, 42), 1.0)
242 self.assertEqual(math.copysign(0., 42), 0.0)
243 self.assertEqual(math.copysign(1., -42), -1.0)
244 self.assertEqual(math.copysign(3, 0.), 3.0)
245 self.assertEqual(math.copysign(4., -0.), -4.0)
246
Christian Heimese2ca4242008-01-03 20:23:15 +0000247 def testIsnan(self):
248 self.assert_(math.isnan(float("nan")))
249 self.assert_(math.isnan(float("inf")* 0.))
250 self.failIf(math.isnan(float("inf")))
251 self.failIf(math.isnan(0.))
252 self.failIf(math.isnan(1.))
253
254 def testIsinf(self):
255 self.assert_(math.isinf(float("inf")))
256 self.assert_(math.isinf(float("-inf")))
257 self.assert_(math.isinf(1E400))
258 self.assert_(math.isinf(-1E400))
259 self.failIf(math.isinf(float("nan")))
260 self.failIf(math.isinf(0.))
261 self.failIf(math.isinf(1.))
262
Georg Brandl2f037602006-10-28 13:51:49 +0000263 # RED_FLAG 16-Oct-2000 Tim
264 # While 2.0 is more consistent about exceptions than previous releases, it
265 # still fails this part of the test on some platforms. For now, we only
266 # *run* test_exceptions() in verbose mode, so that this isn't normally
267 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000268
Georg Brandl2f037602006-10-28 13:51:49 +0000269 if verbose:
270 def test_exceptions(self):
271 try:
272 x = math.exp(-1000000000)
273 except:
274 # mathmodule.c is failing to weed out underflows from libm, or
275 # we've got an fp format with huge dynamic range
276 self.fail("underflowing exp() should not have raised "
277 "an exception")
278 if x != 0:
279 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000280
Georg Brandl2f037602006-10-28 13:51:49 +0000281 # If this fails, probably using a strict IEEE-754 conforming libm, and x
282 # is +Inf afterwards. But Python wants overflows detected by default.
283 try:
284 x = math.exp(1000000000)
285 except OverflowError:
286 pass
287 else:
288 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000289
Georg Brandl2f037602006-10-28 13:51:49 +0000290 # If this fails, it could be a puzzle. One odd possibility is that
291 # mathmodule.c's macros are getting confused while comparing
292 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
293 # as a result (and so raising OverflowError instead).
294 try:
295 x = math.sqrt(-1.0)
296 except ValueError:
297 pass
298 else:
299 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000300
Georg Brandl2f037602006-10-28 13:51:49 +0000301
302def test_main():
303 run_unittest(MathTests)
304
305if __name__ == '__main__':
306 test_main()