blob: 379ff89bdbc9d7e6a051c60f07e44bfa3444e851 [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
Christian Heimes53876d92008-04-19 00:31:39 +00007import os
8import sys
Guido van Rossumfcce6301996-08-08 18:26:25 +00009
Christian Heimes53876d92008-04-19 00:31:39 +000010eps = 1E-05
11NAN = float('nan')
12INF = float('inf')
13NINF = float('-inf')
14
15# locate file with test values
16if __name__ == '__main__':
17 file = sys.argv[0]
18else:
19 file = __file__
20test_dir = os.path.dirname(file) or os.curdir
21test_file = os.path.join(test_dir, 'cmath_testcases.txt')
22
23def parse_testfile(fname):
24 """Parse a file with test values
25
26 Empty lines or lines starting with -- are ignored
27 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
28 """
29 with open(fname) as fp:
30 for line in fp:
31 # skip comment lines and blank lines
32 if line.startswith('--') or not line.strip():
33 continue
34
35 lhs, rhs = line.split('->')
36 id, fn, arg_real, arg_imag = lhs.split()
37 rhs_pieces = rhs.split()
38 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
39 flags = rhs_pieces[2:]
40
41 yield (id, fn,
42 float(arg_real), float(arg_imag),
43 float(exp_real), float(exp_imag),
44 flags
45 )
Guido van Rossumfcce6301996-08-08 18:26:25 +000046
Thomas Wouters89f507f2006-12-13 04:49:30 +000047class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000048
Thomas Wouters89f507f2006-12-13 04:49:30 +000049 def ftest(self, name, value, expected):
50 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +000051 # Use %r instead of %f so the error message
52 # displays full precision. Otherwise discrepancies
53 # in the last few bits will lead to very confusing
54 # error messages
55 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +000056 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000057
Thomas Wouters89f507f2006-12-13 04:49:30 +000058 def testConstants(self):
59 self.ftest('pi', math.pi, 3.1415926)
60 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000061
Thomas Wouters89f507f2006-12-13 04:49:30 +000062 def testAcos(self):
63 self.assertRaises(TypeError, math.acos)
64 self.ftest('acos(-1)', math.acos(-1), math.pi)
65 self.ftest('acos(0)', math.acos(0), math.pi/2)
66 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +000067 self.assertRaises(ValueError, math.acos, INF)
68 self.assertRaises(ValueError, math.acos, NINF)
69 self.assert_(math.isnan(math.acos(NAN)))
70
71 def testAcosh(self):
72 self.assertRaises(TypeError, math.acosh)
73 self.ftest('acosh(1)', math.acosh(1), 0)
74 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
75 self.assertRaises(ValueError, math.acosh, 0)
76 self.assertRaises(ValueError, math.acosh, -1)
77 self.assertEquals(math.acosh(INF), INF)
78 self.assertRaises(ValueError, math.acosh, NINF)
79 self.assert_(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000080
Thomas Wouters89f507f2006-12-13 04:49:30 +000081 def testAsin(self):
82 self.assertRaises(TypeError, math.asin)
83 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
84 self.ftest('asin(0)', math.asin(0), 0)
85 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +000086 self.assertRaises(ValueError, math.asin, INF)
87 self.assertRaises(ValueError, math.asin, NINF)
88 self.assert_(math.isnan(math.asin(NAN)))
89
90 def testAsinh(self):
91 self.assertRaises(TypeError, math.asinh)
92 self.ftest('asinh(0)', math.asinh(0), 0)
93 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
94 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
95 self.assertEquals(math.asinh(INF), INF)
96 self.assertEquals(math.asinh(NINF), NINF)
97 self.assert_(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000098
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 def testAtan(self):
100 self.assertRaises(TypeError, math.atan)
101 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
102 self.ftest('atan(0)', math.atan(0), 0)
103 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes53876d92008-04-19 00:31:39 +0000104 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Christian Heimesa342c012008-04-20 21:01:16 +0000105 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000106 self.assert_(math.isnan(math.atan(NAN)))
107
108 def testAtanh(self):
109 self.assertRaises(TypeError, math.atan)
110 self.ftest('atanh(0)', math.atanh(0), 0)
111 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
112 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
113 self.assertRaises(ValueError, math.atanh, 1)
114 self.assertRaises(ValueError, math.atanh, -1)
115 self.assertRaises(ValueError, math.atanh, INF)
116 self.assertRaises(ValueError, math.atanh, NINF)
117 self.assert_(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000118
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 def testAtan2(self):
120 self.assertRaises(TypeError, math.atan2)
121 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
122 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
123 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
124 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
125 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000126
Christian Heimese57950f2008-04-21 13:08:03 +0000127 # math.atan2(0, x)
128 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
129 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
130 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
131 self.assertEqual(math.atan2(0., 0.), 0.)
132 self.assertEqual(math.atan2(0., 2.3), 0.)
133 self.assertEqual(math.atan2(0., INF), 0.)
134 self.assert_(math.isnan(math.atan2(0., NAN)))
135 # math.atan2(-0, x)
136 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
137 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
138 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
139 self.assertEqual(math.atan2(-0., 0.), -0.)
140 self.assertEqual(math.atan2(-0., 2.3), -0.)
141 self.assertEqual(math.atan2(-0., INF), -0.)
142 self.assert_(math.isnan(math.atan2(-0., NAN)))
143 # math.atan2(INF, x)
144 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
145 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
146 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
147 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
148 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
149 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
150 self.assert_(math.isnan(math.atan2(INF, NAN)))
151 # math.atan2(NINF, x)
152 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
153 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
154 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
155 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
156 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
157 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
158 self.assert_(math.isnan(math.atan2(NINF, NAN)))
159 # math.atan2(+finite, x)
160 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
161 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
162 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
163 self.assertEqual(math.atan2(2.3, INF), 0.)
164 self.assert_(math.isnan(math.atan2(2.3, NAN)))
165 # math.atan2(-finite, x)
166 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
167 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
168 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
169 self.assertEqual(math.atan2(-2.3, INF), -0.)
170 self.assert_(math.isnan(math.atan2(-2.3, NAN)))
171 # math.atan2(NAN, x)
172 self.assert_(math.isnan(math.atan2(NAN, NINF)))
173 self.assert_(math.isnan(math.atan2(NAN, -2.3)))
174 self.assert_(math.isnan(math.atan2(NAN, -0.)))
175 self.assert_(math.isnan(math.atan2(NAN, 0.)))
176 self.assert_(math.isnan(math.atan2(NAN, 2.3)))
177 self.assert_(math.isnan(math.atan2(NAN, INF)))
178 self.assert_(math.isnan(math.atan2(NAN, NAN)))
179
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180 def testCeil(self):
181 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000182 self.assertEquals(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
184 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
185 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
186 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
187 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
188 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000189 #self.assertEquals(math.ceil(INF), INF)
190 #self.assertEquals(math.ceil(NINF), NINF)
191 #self.assert_(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000192
Guido van Rossum13e05de2007-08-23 22:56:55 +0000193 class TestCeil:
194 def __ceil__(self):
195 return 42
196 class TestNoCeil:
197 pass
198 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
199 self.assertRaises(TypeError, math.ceil, TestNoCeil())
200
201 t = TestNoCeil()
202 t.__ceil__ = lambda *args: args
203 self.assertRaises(TypeError, math.ceil, t)
204 self.assertRaises(TypeError, math.ceil, t, 0)
205
Christian Heimes53876d92008-04-19 00:31:39 +0000206 if float.__getformat__("double").startswith("IEEE"):
207 def testCopysign(self):
208 self.assertRaises(TypeError, math.copysign)
209 # copysign should let us distinguish signs of zeros
210 self.assertEquals(copysign(1., 0.), 1.)
211 self.assertEquals(copysign(1., -0.), -1.)
212 self.assertEquals(copysign(INF, 0.), INF)
213 self.assertEquals(copysign(INF, -0.), NINF)
214 self.assertEquals(copysign(NINF, 0.), INF)
215 self.assertEquals(copysign(NINF, -0.), NINF)
216 # and of infinities
217 self.assertEquals(copysign(1., INF), 1.)
218 self.assertEquals(copysign(1., NINF), -1.)
219 self.assertEquals(copysign(INF, INF), INF)
220 self.assertEquals(copysign(INF, NINF), NINF)
221 self.assertEquals(copysign(NINF, INF), INF)
222 self.assertEquals(copysign(NINF, NINF), NINF)
223 self.assert_(math.isnan(copysign(NAN, 1.)))
224 self.assert_(math.isnan(copysign(NAN, INF)))
225 self.assert_(math.isnan(copysign(NAN, NINF)))
226 self.assert_(math.isnan(copysign(NAN, NAN)))
227 # copysign(INF, NAN) may be INF or it may be NINF, since
228 # we don't know whether the sign bit of NAN is set on any
229 # given platform.
230 self.assert_(math.isinf(copysign(INF, NAN)))
231 # similarly, copysign(2., NAN) could be 2. or -2.
232 self.assertEquals(abs(copysign(2., NAN)), 2.)
233
Thomas Wouters89f507f2006-12-13 04:49:30 +0000234 def testCos(self):
235 self.assertRaises(TypeError, math.cos)
236 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
237 self.ftest('cos(0)', math.cos(0), 1)
238 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
239 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000240 try:
241 self.assert_(math.isnan(math.cos(INF)))
242 self.assert_(math.isnan(math.cos(NINF)))
243 except ValueError:
244 self.assertRaises(ValueError, math.cos, INF)
245 self.assertRaises(ValueError, math.cos, NINF)
246 self.assert_(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000247
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248 def testCosh(self):
249 self.assertRaises(TypeError, math.cosh)
250 self.ftest('cosh(0)', math.cosh(0), 1)
251 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
Christian Heimes53876d92008-04-19 00:31:39 +0000252 self.assertEquals(math.cosh(INF), INF)
253 self.assertEquals(math.cosh(NINF), INF)
254 self.assert_(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000255
Thomas Wouters89f507f2006-12-13 04:49:30 +0000256 def testDegrees(self):
257 self.assertRaises(TypeError, math.degrees)
258 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
259 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
260 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000261
Thomas Wouters89f507f2006-12-13 04:49:30 +0000262 def testExp(self):
263 self.assertRaises(TypeError, math.exp)
264 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
265 self.ftest('exp(0)', math.exp(0), 1)
266 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes53876d92008-04-19 00:31:39 +0000267 self.assertEquals(math.exp(INF), INF)
268 self.assertEquals(math.exp(NINF), 0.)
269 self.assert_(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000270
Thomas Wouters89f507f2006-12-13 04:49:30 +0000271 def testFabs(self):
272 self.assertRaises(TypeError, math.fabs)
273 self.ftest('fabs(-1)', math.fabs(-1), 1)
274 self.ftest('fabs(0)', math.fabs(0), 0)
275 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000276
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277 def testFloor(self):
278 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000279 self.assertEquals(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000280 self.ftest('floor(0.5)', math.floor(0.5), 0)
281 self.ftest('floor(1.0)', math.floor(1.0), 1)
282 self.ftest('floor(1.5)', math.floor(1.5), 1)
283 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
284 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
285 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000286 # pow() relies on floor() to check for integers
287 # This fails on some platforms - so check it here
288 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
289 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes53876d92008-04-19 00:31:39 +0000290 #self.assertEquals(math.ceil(INF), INF)
291 #self.assertEquals(math.ceil(NINF), NINF)
292 #self.assert_(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000293
Guido van Rossum13e05de2007-08-23 22:56:55 +0000294 class TestFloor:
295 def __floor__(self):
296 return 42
297 class TestNoFloor:
298 pass
299 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
300 self.assertRaises(TypeError, math.floor, TestNoFloor())
301
302 t = TestNoFloor()
303 t.__floor__ = lambda *args: args
304 self.assertRaises(TypeError, math.floor, t)
305 self.assertRaises(TypeError, math.floor, t, 0)
306
Thomas Wouters89f507f2006-12-13 04:49:30 +0000307 def testFmod(self):
308 self.assertRaises(TypeError, math.fmod)
309 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
310 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
311 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
312 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
313 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
314 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000315 self.assert_(math.isnan(math.fmod(NAN, 1.)))
316 self.assert_(math.isnan(math.fmod(1., NAN)))
317 self.assert_(math.isnan(math.fmod(NAN, NAN)))
318 self.assertRaises(ValueError, math.fmod, 1., 0.)
319 self.assertRaises(ValueError, math.fmod, INF, 1.)
320 self.assertRaises(ValueError, math.fmod, NINF, 1.)
321 self.assertRaises(ValueError, math.fmod, INF, 0.)
322 self.assertEquals(math.fmod(3.0, INF), 3.0)
323 self.assertEquals(math.fmod(-3.0, INF), -3.0)
324 self.assertEquals(math.fmod(3.0, NINF), 3.0)
325 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
326 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
327 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000328
Thomas Wouters89f507f2006-12-13 04:49:30 +0000329 def testFrexp(self):
330 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000331
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000332 def testfrexp(name, result, expected):
333 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000334 if abs(mant-emant) > eps or exp != eexp:
335 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000336 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000337
Thomas Wouters89f507f2006-12-13 04:49:30 +0000338 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
339 testfrexp('frexp(0)', math.frexp(0), (0, 0))
340 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
341 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000342
Christian Heimes53876d92008-04-19 00:31:39 +0000343 self.assertEquals(math.frexp(INF)[0], INF)
344 self.assertEquals(math.frexp(NINF)[0], NINF)
345 self.assert_(math.isnan(math.frexp(NAN)[0]))
346
Thomas Wouters89f507f2006-12-13 04:49:30 +0000347 def testHypot(self):
348 self.assertRaises(TypeError, math.hypot)
349 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
350 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000351 self.assertEqual(math.hypot(NAN, INF), INF)
352 self.assertEqual(math.hypot(INF, NAN), INF)
353 self.assertEqual(math.hypot(NAN, NINF), INF)
354 self.assertEqual(math.hypot(NINF, NAN), INF)
355 self.assert_(math.isnan(math.hypot(1.0, NAN)))
356 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000357
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 def testLdexp(self):
359 self.assertRaises(TypeError, math.ldexp)
360 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
361 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
362 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
363 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000364 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
365 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
366 self.assertEquals(math.ldexp(1., -1000000), 0.)
367 self.assertEquals(math.ldexp(-1., -1000000), -0.)
368 self.assertEquals(math.ldexp(INF, 30), INF)
369 self.assertEquals(math.ldexp(NINF, -213), NINF)
370 self.assert_(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000371
Thomas Wouters89f507f2006-12-13 04:49:30 +0000372 def testLog(self):
373 self.assertRaises(TypeError, math.log)
374 self.ftest('log(1/e)', math.log(1/math.e), -1)
375 self.ftest('log(1)', math.log(1), 0)
376 self.ftest('log(e)', math.log(math.e), 1)
377 self.ftest('log(32,2)', math.log(32,2), 5)
378 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
379 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000380 self.assertEquals(math.log(INF), INF)
381 self.assertRaises(ValueError, math.log, NINF)
382 self.assert_(math.isnan(math.log(NAN)))
383
384 def testLog1p(self):
385 self.assertRaises(TypeError, math.log1p)
386 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
387 self.ftest('log1p(0)', math.log1p(0), 0)
388 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
389 self.ftest('log1p(1)', math.log1p(1), math.log(2))
390 self.assertEquals(math.log1p(INF), INF)
391 self.assertRaises(ValueError, math.log1p, NINF)
392 self.assert_(math.isnan(math.log1p(NAN)))
393 n= 2**90
394 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
395 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000396
Thomas Wouters89f507f2006-12-13 04:49:30 +0000397 def testLog10(self):
398 self.assertRaises(TypeError, math.log10)
399 self.ftest('log10(0.1)', math.log10(0.1), -1)
400 self.ftest('log10(1)', math.log10(1), 0)
401 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes53876d92008-04-19 00:31:39 +0000402 self.assertEquals(math.log(INF), INF)
403 self.assertRaises(ValueError, math.log10, NINF)
404 self.assert_(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000405
Thomas Wouters89f507f2006-12-13 04:49:30 +0000406 def testModf(self):
407 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000408
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000409 def testmodf(name, result, expected):
410 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000411 if abs(v1-e1) > eps or abs(v2-e2):
412 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000413 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000414
Thomas Wouters89f507f2006-12-13 04:49:30 +0000415 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
416 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000417
Christian Heimes53876d92008-04-19 00:31:39 +0000418 self.assertEquals(math.modf(INF), (0.0, INF))
419 self.assertEquals(math.modf(NINF), (-0.0, NINF))
420
421 modf_nan = math.modf(NAN)
422 self.assert_(math.isnan(modf_nan[0]))
423 self.assert_(math.isnan(modf_nan[1]))
424
Thomas Wouters89f507f2006-12-13 04:49:30 +0000425 def testPow(self):
426 self.assertRaises(TypeError, math.pow)
427 self.ftest('pow(0,1)', math.pow(0,1), 0)
428 self.ftest('pow(1,0)', math.pow(1,0), 1)
429 self.ftest('pow(2,1)', math.pow(2,1), 2)
430 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000431 self.assertEqual(math.pow(INF, 1), INF)
432 self.assertEqual(math.pow(NINF, 1), NINF)
433 self.assertEqual((math.pow(1, INF)), 1.)
434 self.assertEqual((math.pow(1, NINF)), 1.)
435 self.assert_(math.isnan(math.pow(NAN, 1)))
436 self.assert_(math.isnan(math.pow(2, NAN)))
437 self.assert_(math.isnan(math.pow(0, NAN)))
438 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000439
440 # pow(0., x)
441 self.assertEqual(math.pow(0., INF), 0.)
442 self.assertEqual(math.pow(0., 3.), 0.)
443 self.assertEqual(math.pow(0., 2.3), 0.)
444 self.assertEqual(math.pow(0., 2.), 0.)
445 self.assertEqual(math.pow(0., 0.), 1.)
446 self.assertEqual(math.pow(0., -0.), 1.)
447 self.assertRaises(ValueError, math.pow, 0., -2.)
448 self.assertRaises(ValueError, math.pow, 0., -2.3)
449 self.assertRaises(ValueError, math.pow, 0., -3.)
450 self.assertRaises(ValueError, math.pow, 0., NINF)
451 self.assert_(math.isnan(math.pow(0., NAN)))
452
453 # pow(INF, x)
454 self.assertEqual(math.pow(INF, INF), INF)
455 self.assertEqual(math.pow(INF, 3.), INF)
456 self.assertEqual(math.pow(INF, 2.3), INF)
457 self.assertEqual(math.pow(INF, 2.), INF)
458 self.assertEqual(math.pow(INF, 0.), 1.)
459 self.assertEqual(math.pow(INF, -0.), 1.)
460 self.assertEqual(math.pow(INF, -2.), 0.)
461 self.assertEqual(math.pow(INF, -2.3), 0.)
462 self.assertEqual(math.pow(INF, -3.), 0.)
463 self.assertEqual(math.pow(INF, NINF), 0.)
464 self.assert_(math.isnan(math.pow(INF, NAN)))
465
466 # pow(-0., x)
467 self.assertEqual(math.pow(-0., INF), 0.)
468 self.assertEqual(math.pow(-0., 3.), -0.)
469 self.assertEqual(math.pow(-0., 2.3), 0.)
470 self.assertEqual(math.pow(-0., 2.), 0.)
471 self.assertEqual(math.pow(-0., 0.), 1.)
472 self.assertEqual(math.pow(-0., -0.), 1.)
473 self.assertRaises(ValueError, math.pow, -0., -2.)
474 self.assertRaises(ValueError, math.pow, -0., -2.3)
475 self.assertRaises(ValueError, math.pow, -0., -3.)
476 self.assertRaises(ValueError, math.pow, -0., NINF)
477 self.assert_(math.isnan(math.pow(-0., NAN)))
478
479 # pow(NINF, x)
480 self.assertEqual(math.pow(NINF, INF), INF)
481 self.assertEqual(math.pow(NINF, 3.), NINF)
482 self.assertEqual(math.pow(NINF, 2.3), INF)
483 self.assertEqual(math.pow(NINF, 2.), INF)
484 self.assertEqual(math.pow(NINF, 0.), 1.)
485 self.assertEqual(math.pow(NINF, -0.), 1.)
486 self.assertEqual(math.pow(NINF, -2.), 0.)
487 self.assertEqual(math.pow(NINF, -2.3), 0.)
488 self.assertEqual(math.pow(NINF, -3.), -0.)
489 self.assertEqual(math.pow(NINF, NINF), 0.)
490 self.assert_(math.isnan(math.pow(NINF, NAN)))
491
492 # pow(-1, x)
493 self.assertEqual(math.pow(-1., INF), 1.)
494 self.assertEqual(math.pow(-1., 3.), -1.)
495 self.assertRaises(ValueError, math.pow, -1., 2.3)
496 self.assertEqual(math.pow(-1., 2.), 1.)
497 self.assertEqual(math.pow(-1., 0.), 1.)
498 self.assertEqual(math.pow(-1., -0.), 1.)
499 self.assertEqual(math.pow(-1., -2.), 1.)
500 self.assertRaises(ValueError, math.pow, -1., -2.3)
501 self.assertEqual(math.pow(-1., -3.), -1.)
502 self.assertEqual(math.pow(-1., NINF), 1.)
503 self.assert_(math.isnan(math.pow(-1., NAN)))
504
505 # pow(1, x)
506 self.assertEqual(math.pow(1., INF), 1.)
507 self.assertEqual(math.pow(1., 3.), 1.)
508 self.assertEqual(math.pow(1., 2.3), 1.)
509 self.assertEqual(math.pow(1., 2.), 1.)
510 self.assertEqual(math.pow(1., 0.), 1.)
511 self.assertEqual(math.pow(1., -0.), 1.)
512 self.assertEqual(math.pow(1., -2.), 1.)
513 self.assertEqual(math.pow(1., -2.3), 1.)
514 self.assertEqual(math.pow(1., -3.), 1.)
515 self.assertEqual(math.pow(1., NINF), 1.)
516 self.assertEqual(math.pow(1., NAN), 1.)
517
518 # pow(x, 0) should be 1 for any x
519 self.assertEqual(math.pow(2.3, 0.), 1.)
520 self.assertEqual(math.pow(-2.3, 0.), 1.)
521 self.assertEqual(math.pow(NAN, 0.), 1.)
522 self.assertEqual(math.pow(2.3, -0.), 1.)
523 self.assertEqual(math.pow(-2.3, -0.), 1.)
524 self.assertEqual(math.pow(NAN, -0.), 1.)
525
526 # pow(x, y) is invalid if x is negative and y is not integral
527 self.assertRaises(ValueError, math.pow, -1., 2.3)
528 self.assertRaises(ValueError, math.pow, -15., -3.1)
529
530 # pow(x, NINF)
531 self.assertEqual(math.pow(1.9, NINF), 0.)
532 self.assertEqual(math.pow(1.1, NINF), 0.)
533 self.assertEqual(math.pow(0.9, NINF), INF)
534 self.assertEqual(math.pow(0.1, NINF), INF)
535 self.assertEqual(math.pow(-0.1, NINF), INF)
536 self.assertEqual(math.pow(-0.9, NINF), INF)
537 self.assertEqual(math.pow(-1.1, NINF), 0.)
538 self.assertEqual(math.pow(-1.9, NINF), 0.)
539
540 # pow(x, INF)
541 self.assertEqual(math.pow(1.9, INF), INF)
542 self.assertEqual(math.pow(1.1, INF), INF)
543 self.assertEqual(math.pow(0.9, INF), 0.)
544 self.assertEqual(math.pow(0.1, INF), 0.)
545 self.assertEqual(math.pow(-0.1, INF), 0.)
546 self.assertEqual(math.pow(-0.9, INF), 0.)
547 self.assertEqual(math.pow(-1.1, INF), INF)
548 self.assertEqual(math.pow(-1.9, INF), INF)
549
550 # pow(x, y) should work for x negative, y an integer
551 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
552 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
553 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
554 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
555 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
556 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
557 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
558 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
559 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
560 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
561
562 # the following tests have been commented out since they don't
563 # really belong here: the implementation of ** for floats is
564 # independent of the implemention of math.pow
565 #self.assertEqual(1**NAN, 1)
566 #self.assertEqual(1**INF, 1)
567 #self.assertEqual(1**NINF, 1)
568 #self.assertEqual(1**0, 1)
569 #self.assertEqual(1.**NAN, 1)
570 #self.assertEqual(1.**INF, 1)
571 #self.assertEqual(1.**NINF, 1)
572 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000573
Thomas Wouters89f507f2006-12-13 04:49:30 +0000574 def testRadians(self):
575 self.assertRaises(TypeError, math.radians)
576 self.ftest('radians(180)', math.radians(180), math.pi)
577 self.ftest('radians(90)', math.radians(90), math.pi/2)
578 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000579
Thomas Wouters89f507f2006-12-13 04:49:30 +0000580 def testSin(self):
581 self.assertRaises(TypeError, math.sin)
582 self.ftest('sin(0)', math.sin(0), 0)
583 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
584 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000585 try:
586 self.assert_(math.isnan(math.sin(INF)))
587 self.assert_(math.isnan(math.sin(NINF)))
588 except ValueError:
589 self.assertRaises(ValueError, math.sin, INF)
590 self.assertRaises(ValueError, math.sin, NINF)
591 self.assert_(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000592
Thomas Wouters89f507f2006-12-13 04:49:30 +0000593 def testSinh(self):
594 self.assertRaises(TypeError, math.sinh)
595 self.ftest('sinh(0)', math.sinh(0), 0)
596 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
597 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000598 self.assertEquals(math.sinh(INF), INF)
Christian Heimesa342c012008-04-20 21:01:16 +0000599 self.assertEquals(math.sinh(NINF), NINF)
Christian Heimes53876d92008-04-19 00:31:39 +0000600 self.assert_(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000601
Thomas Wouters89f507f2006-12-13 04:49:30 +0000602 def testSqrt(self):
603 self.assertRaises(TypeError, math.sqrt)
604 self.ftest('sqrt(0)', math.sqrt(0), 0)
605 self.ftest('sqrt(1)', math.sqrt(1), 1)
606 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000607 self.assertEquals(math.sqrt(INF), INF)
608 self.assertRaises(ValueError, math.sqrt, NINF)
609 self.assert_(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000610
Thomas Wouters89f507f2006-12-13 04:49:30 +0000611 def testTan(self):
612 self.assertRaises(TypeError, math.tan)
613 self.ftest('tan(0)', math.tan(0), 0)
614 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
615 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000616 try:
617 self.assert_(math.isnan(math.tan(INF)))
618 self.assert_(math.isnan(math.tan(NINF)))
619 except:
620 self.assertRaises(ValueError, math.tan, INF)
621 self.assertRaises(ValueError, math.tan, NINF)
622 self.assert_(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000623
Thomas Wouters89f507f2006-12-13 04:49:30 +0000624 def testTanh(self):
625 self.assertRaises(TypeError, math.tanh)
626 self.ftest('tanh(0)', math.tanh(0), 0)
627 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000628 self.ftest('tanh(inf)', math.tanh(INF), 1)
629 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
630 self.assert_(math.isnan(math.tanh(NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000631 # check that tanh(-0.) == -0. on IEEE 754 systems
632 if float.__getformat__("double").startswith("IEEE"):
633 self.assertEqual(math.tanh(-0.), -0.)
634 self.assertEqual(math.copysign(1., math.tanh(-0.)),
635 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000636
Christian Heimes400adb02008-02-01 08:12:03 +0000637 def test_trunc(self):
638 self.assertEqual(math.trunc(1), 1)
639 self.assertEqual(math.trunc(-1), -1)
640 self.assertEqual(type(math.trunc(1)), int)
641 self.assertEqual(type(math.trunc(1.5)), int)
642 self.assertEqual(math.trunc(1.5), 1)
643 self.assertEqual(math.trunc(-1.5), -1)
644 self.assertEqual(math.trunc(1.999999), 1)
645 self.assertEqual(math.trunc(-1.999999), -1)
646 self.assertEqual(math.trunc(-0.999999), -0)
647 self.assertEqual(math.trunc(-100.999), -100)
648
649 class TestTrunc(object):
650 def __trunc__(self):
651 return 23
652
653 class TestNoTrunc(object):
654 pass
655
656 self.assertEqual(math.trunc(TestTrunc()), 23)
657
658 self.assertRaises(TypeError, math.trunc)
659 self.assertRaises(TypeError, math.trunc, 1, 2)
660 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
661
662 # XXX Doesn't work because the method is looked up on
663 # the type only.
664 #t = TestNoTrunc()
665 #t.__trunc__ = lambda *args: args
666 #self.assertEquals((), math.trunc(t))
667 #self.assertRaises(TypeError, math.trunc, t, 0)
668
Christian Heimes072c0f12008-01-03 23:01:04 +0000669 def testCopysign(self):
670 self.assertEqual(math.copysign(1, 42), 1.0)
671 self.assertEqual(math.copysign(0., 42), 0.0)
672 self.assertEqual(math.copysign(1., -42), -1.0)
673 self.assertEqual(math.copysign(3, 0.), 3.0)
674 self.assertEqual(math.copysign(4., -0.), -4.0)
675
676 def testIsnan(self):
677 self.assert_(math.isnan(float("nan")))
678 self.assert_(math.isnan(float("inf")* 0.))
679 self.failIf(math.isnan(float("inf")))
680 self.failIf(math.isnan(0.))
681 self.failIf(math.isnan(1.))
682
683 def testIsinf(self):
684 self.assert_(math.isinf(float("inf")))
685 self.assert_(math.isinf(float("-inf")))
686 self.assert_(math.isinf(1E400))
687 self.assert_(math.isinf(-1E400))
688 self.failIf(math.isinf(float("nan")))
689 self.failIf(math.isinf(0.))
690 self.failIf(math.isinf(1.))
691
Thomas Wouters89f507f2006-12-13 04:49:30 +0000692 # RED_FLAG 16-Oct-2000 Tim
693 # While 2.0 is more consistent about exceptions than previous releases, it
694 # still fails this part of the test on some platforms. For now, we only
695 # *run* test_exceptions() in verbose mode, so that this isn't normally
696 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000697
Thomas Wouters89f507f2006-12-13 04:49:30 +0000698 if verbose:
699 def test_exceptions(self):
700 try:
701 x = math.exp(-1000000000)
702 except:
703 # mathmodule.c is failing to weed out underflows from libm, or
704 # we've got an fp format with huge dynamic range
705 self.fail("underflowing exp() should not have raised "
706 "an exception")
707 if x != 0:
708 self.fail("underflowing exp() should have returned 0")
709
710 # If this fails, probably using a strict IEEE-754 conforming libm, and x
711 # is +Inf afterwards. But Python wants overflows detected by default.
712 try:
713 x = math.exp(1000000000)
714 except OverflowError:
715 pass
716 else:
717 self.fail("overflowing exp() didn't trigger OverflowError")
718
719 # If this fails, it could be a puzzle. One odd possibility is that
720 # mathmodule.c's macros are getting confused while comparing
721 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
722 # as a result (and so raising OverflowError instead).
723 try:
724 x = math.sqrt(-1.0)
725 except ValueError:
726 pass
727 else:
728 self.fail("sqrt(-1) didn't raise ValueError")
729
Christian Heimes53876d92008-04-19 00:31:39 +0000730 def test_testfile(self):
731 if not float.__getformat__("double").startswith("IEEE"):
732 return
733 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
734 # Skip if either the input or result is complex, or if
735 # flags is nonempty
736 if ai != 0. or ei != 0. or flags:
737 continue
738 if fn in ['rect', 'polar']:
739 # no real versions of rect, polar
740 continue
741 func = getattr(math, fn)
Christian Heimesa342c012008-04-20 21:01:16 +0000742 try:
743 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +0000744 except ValueError as exc:
745 message = (("Unexpected ValueError: %s\n " +
746 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +0000747 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +0000748 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000749
750def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +0000751 from doctest import DocFileSuite
752 suite = unittest.TestSuite()
753 suite.addTest(unittest.makeSuite(MathTests))
754 suite.addTest(DocFileSuite("ieee754.txt"))
755 run_unittest(suite)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000756
757if __name__ == '__main__':
758 test_main()