blob: 1eafebad22276d311a5eda06cbbe80e415bbc4dc [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test.support import run_unittest, verbose
Thomas Wouters89f507f2006-12-13 04:49:30 +00005import unittest
6import math
Christian Heimes53876d92008-04-19 00:31:39 +00007import os
8import sys
Georg Brandlc28e1fa2008-06-10 19:20:26 +00009import random
Guido van Rossumfcce6301996-08-08 18:26:25 +000010
Christian Heimes53876d92008-04-19 00:31:39 +000011eps = 1E-05
12NAN = float('nan')
13INF = float('inf')
14NINF = float('-inf')
15
16# locate file with test values
17if __name__ == '__main__':
18 file = sys.argv[0]
19else:
20 file = __file__
21test_dir = os.path.dirname(file) or os.curdir
22test_file = os.path.join(test_dir, 'cmath_testcases.txt')
23
24def parse_testfile(fname):
25 """Parse a file with test values
26
27 Empty lines or lines starting with -- are ignored
28 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
29 """
30 with open(fname) as fp:
31 for line in fp:
32 # skip comment lines and blank lines
33 if line.startswith('--') or not line.strip():
34 continue
35
36 lhs, rhs = line.split('->')
37 id, fn, arg_real, arg_imag = lhs.split()
38 rhs_pieces = rhs.split()
39 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
40 flags = rhs_pieces[2:]
41
42 yield (id, fn,
43 float(arg_real), float(arg_imag),
44 float(exp_real), float(exp_imag),
45 flags
46 )
Guido van Rossumfcce6301996-08-08 18:26:25 +000047
Thomas Wouters89f507f2006-12-13 04:49:30 +000048class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000049
Thomas Wouters89f507f2006-12-13 04:49:30 +000050 def ftest(self, name, value, expected):
51 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +000052 # Use %r instead of %f so the error message
53 # displays full precision. Otherwise discrepancies
54 # in the last few bits will lead to very confusing
55 # error messages
56 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +000057 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000058
Thomas Wouters89f507f2006-12-13 04:49:30 +000059 def testConstants(self):
60 self.ftest('pi', math.pi, 3.1415926)
61 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000062
Thomas Wouters89f507f2006-12-13 04:49:30 +000063 def testAcos(self):
64 self.assertRaises(TypeError, math.acos)
65 self.ftest('acos(-1)', math.acos(-1), math.pi)
66 self.ftest('acos(0)', math.acos(0), math.pi/2)
67 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +000068 self.assertRaises(ValueError, math.acos, INF)
69 self.assertRaises(ValueError, math.acos, NINF)
70 self.assert_(math.isnan(math.acos(NAN)))
71
72 def testAcosh(self):
73 self.assertRaises(TypeError, math.acosh)
74 self.ftest('acosh(1)', math.acosh(1), 0)
75 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
76 self.assertRaises(ValueError, math.acosh, 0)
77 self.assertRaises(ValueError, math.acosh, -1)
78 self.assertEquals(math.acosh(INF), INF)
79 self.assertRaises(ValueError, math.acosh, NINF)
80 self.assert_(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000081
Thomas Wouters89f507f2006-12-13 04:49:30 +000082 def testAsin(self):
83 self.assertRaises(TypeError, math.asin)
84 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
85 self.ftest('asin(0)', math.asin(0), 0)
86 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +000087 self.assertRaises(ValueError, math.asin, INF)
88 self.assertRaises(ValueError, math.asin, NINF)
89 self.assert_(math.isnan(math.asin(NAN)))
90
91 def testAsinh(self):
92 self.assertRaises(TypeError, math.asinh)
93 self.ftest('asinh(0)', math.asinh(0), 0)
94 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
95 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
96 self.assertEquals(math.asinh(INF), INF)
97 self.assertEquals(math.asinh(NINF), NINF)
98 self.assert_(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000099
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100 def testAtan(self):
101 self.assertRaises(TypeError, math.atan)
102 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
103 self.ftest('atan(0)', math.atan(0), 0)
104 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes53876d92008-04-19 00:31:39 +0000105 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Christian Heimesa342c012008-04-20 21:01:16 +0000106 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000107 self.assert_(math.isnan(math.atan(NAN)))
108
109 def testAtanh(self):
110 self.assertRaises(TypeError, math.atan)
111 self.ftest('atanh(0)', math.atanh(0), 0)
112 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
113 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
114 self.assertRaises(ValueError, math.atanh, 1)
115 self.assertRaises(ValueError, math.atanh, -1)
116 self.assertRaises(ValueError, math.atanh, INF)
117 self.assertRaises(ValueError, math.atanh, NINF)
118 self.assert_(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000119
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 def testAtan2(self):
121 self.assertRaises(TypeError, math.atan2)
122 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
123 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
124 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
125 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
126 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000127
Christian Heimese57950f2008-04-21 13:08:03 +0000128 # math.atan2(0, x)
129 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
130 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
131 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
132 self.assertEqual(math.atan2(0., 0.), 0.)
133 self.assertEqual(math.atan2(0., 2.3), 0.)
134 self.assertEqual(math.atan2(0., INF), 0.)
135 self.assert_(math.isnan(math.atan2(0., NAN)))
136 # math.atan2(-0, x)
137 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
138 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
139 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
140 self.assertEqual(math.atan2(-0., 0.), -0.)
141 self.assertEqual(math.atan2(-0., 2.3), -0.)
142 self.assertEqual(math.atan2(-0., INF), -0.)
143 self.assert_(math.isnan(math.atan2(-0., NAN)))
144 # math.atan2(INF, x)
145 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
146 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
147 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
148 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
149 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
150 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
151 self.assert_(math.isnan(math.atan2(INF, NAN)))
152 # math.atan2(NINF, x)
153 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
154 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
155 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
156 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
157 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
158 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
159 self.assert_(math.isnan(math.atan2(NINF, NAN)))
160 # math.atan2(+finite, x)
161 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
162 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
163 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
164 self.assertEqual(math.atan2(2.3, INF), 0.)
165 self.assert_(math.isnan(math.atan2(2.3, NAN)))
166 # math.atan2(-finite, x)
167 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
168 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
169 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
170 self.assertEqual(math.atan2(-2.3, INF), -0.)
171 self.assert_(math.isnan(math.atan2(-2.3, NAN)))
172 # math.atan2(NAN, x)
173 self.assert_(math.isnan(math.atan2(NAN, NINF)))
174 self.assert_(math.isnan(math.atan2(NAN, -2.3)))
175 self.assert_(math.isnan(math.atan2(NAN, -0.)))
176 self.assert_(math.isnan(math.atan2(NAN, 0.)))
177 self.assert_(math.isnan(math.atan2(NAN, 2.3)))
178 self.assert_(math.isnan(math.atan2(NAN, INF)))
179 self.assert_(math.isnan(math.atan2(NAN, NAN)))
180
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181 def testCeil(self):
182 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000183 self.assertEquals(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000184 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
185 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
186 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
187 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
188 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
189 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000190 #self.assertEquals(math.ceil(INF), INF)
191 #self.assertEquals(math.ceil(NINF), NINF)
192 #self.assert_(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000193
Guido van Rossum13e05de2007-08-23 22:56:55 +0000194 class TestCeil:
195 def __ceil__(self):
196 return 42
197 class TestNoCeil:
198 pass
199 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
200 self.assertRaises(TypeError, math.ceil, TestNoCeil())
201
202 t = TestNoCeil()
203 t.__ceil__ = lambda *args: args
204 self.assertRaises(TypeError, math.ceil, t)
205 self.assertRaises(TypeError, math.ceil, t, 0)
206
Christian Heimes53876d92008-04-19 00:31:39 +0000207 if float.__getformat__("double").startswith("IEEE"):
208 def testCopysign(self):
209 self.assertRaises(TypeError, math.copysign)
210 # copysign should let us distinguish signs of zeros
211 self.assertEquals(copysign(1., 0.), 1.)
212 self.assertEquals(copysign(1., -0.), -1.)
213 self.assertEquals(copysign(INF, 0.), INF)
214 self.assertEquals(copysign(INF, -0.), NINF)
215 self.assertEquals(copysign(NINF, 0.), INF)
216 self.assertEquals(copysign(NINF, -0.), NINF)
217 # and of infinities
218 self.assertEquals(copysign(1., INF), 1.)
219 self.assertEquals(copysign(1., NINF), -1.)
220 self.assertEquals(copysign(INF, INF), INF)
221 self.assertEquals(copysign(INF, NINF), NINF)
222 self.assertEquals(copysign(NINF, INF), INF)
223 self.assertEquals(copysign(NINF, NINF), NINF)
224 self.assert_(math.isnan(copysign(NAN, 1.)))
225 self.assert_(math.isnan(copysign(NAN, INF)))
226 self.assert_(math.isnan(copysign(NAN, NINF)))
227 self.assert_(math.isnan(copysign(NAN, NAN)))
228 # copysign(INF, NAN) may be INF or it may be NINF, since
229 # we don't know whether the sign bit of NAN is set on any
230 # given platform.
231 self.assert_(math.isinf(copysign(INF, NAN)))
232 # similarly, copysign(2., NAN) could be 2. or -2.
233 self.assertEquals(abs(copysign(2., NAN)), 2.)
234
Thomas Wouters89f507f2006-12-13 04:49:30 +0000235 def testCos(self):
236 self.assertRaises(TypeError, math.cos)
237 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
238 self.ftest('cos(0)', math.cos(0), 1)
239 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
240 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000241 try:
242 self.assert_(math.isnan(math.cos(INF)))
243 self.assert_(math.isnan(math.cos(NINF)))
244 except ValueError:
245 self.assertRaises(ValueError, math.cos, INF)
246 self.assertRaises(ValueError, math.cos, NINF)
247 self.assert_(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000248
Thomas Wouters89f507f2006-12-13 04:49:30 +0000249 def testCosh(self):
250 self.assertRaises(TypeError, math.cosh)
251 self.ftest('cosh(0)', math.cosh(0), 1)
252 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 +0000253 self.assertEquals(math.cosh(INF), INF)
254 self.assertEquals(math.cosh(NINF), INF)
255 self.assert_(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000256
Thomas Wouters89f507f2006-12-13 04:49:30 +0000257 def testDegrees(self):
258 self.assertRaises(TypeError, math.degrees)
259 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
260 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
261 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000262
Thomas Wouters89f507f2006-12-13 04:49:30 +0000263 def testExp(self):
264 self.assertRaises(TypeError, math.exp)
265 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
266 self.ftest('exp(0)', math.exp(0), 1)
267 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes53876d92008-04-19 00:31:39 +0000268 self.assertEquals(math.exp(INF), INF)
269 self.assertEquals(math.exp(NINF), 0.)
270 self.assert_(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000271
Thomas Wouters89f507f2006-12-13 04:49:30 +0000272 def testFabs(self):
273 self.assertRaises(TypeError, math.fabs)
274 self.ftest('fabs(-1)', math.fabs(-1), 1)
275 self.ftest('fabs(0)', math.fabs(0), 0)
276 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000277
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000278 def testFactorial(self):
279 def fact(n):
280 result = 1
281 for i in range(1, int(n)+1):
282 result *= i
283 return result
284 values = list(range(10)) + [50, 100, 500]
285 random.shuffle(values)
286 for x in range(10):
287 for cast in (int, float):
288 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
289 self.assertRaises(ValueError, math.factorial, -1)
290 self.assertRaises(ValueError, math.factorial, math.pi)
291
Thomas Wouters89f507f2006-12-13 04:49:30 +0000292 def testFloor(self):
293 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000294 self.assertEquals(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000295 self.ftest('floor(0.5)', math.floor(0.5), 0)
296 self.ftest('floor(1.0)', math.floor(1.0), 1)
297 self.ftest('floor(1.5)', math.floor(1.5), 1)
298 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
299 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
300 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000301 # pow() relies on floor() to check for integers
302 # This fails on some platforms - so check it here
303 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
304 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes53876d92008-04-19 00:31:39 +0000305 #self.assertEquals(math.ceil(INF), INF)
306 #self.assertEquals(math.ceil(NINF), NINF)
307 #self.assert_(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000308
Guido van Rossum13e05de2007-08-23 22:56:55 +0000309 class TestFloor:
310 def __floor__(self):
311 return 42
312 class TestNoFloor:
313 pass
314 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
315 self.assertRaises(TypeError, math.floor, TestNoFloor())
316
317 t = TestNoFloor()
318 t.__floor__ = lambda *args: args
319 self.assertRaises(TypeError, math.floor, t)
320 self.assertRaises(TypeError, math.floor, t, 0)
321
Thomas Wouters89f507f2006-12-13 04:49:30 +0000322 def testFmod(self):
323 self.assertRaises(TypeError, math.fmod)
324 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
325 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
326 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
327 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
328 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
329 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000330 self.assert_(math.isnan(math.fmod(NAN, 1.)))
331 self.assert_(math.isnan(math.fmod(1., NAN)))
332 self.assert_(math.isnan(math.fmod(NAN, NAN)))
333 self.assertRaises(ValueError, math.fmod, 1., 0.)
334 self.assertRaises(ValueError, math.fmod, INF, 1.)
335 self.assertRaises(ValueError, math.fmod, NINF, 1.)
336 self.assertRaises(ValueError, math.fmod, INF, 0.)
337 self.assertEquals(math.fmod(3.0, INF), 3.0)
338 self.assertEquals(math.fmod(-3.0, INF), -3.0)
339 self.assertEquals(math.fmod(3.0, NINF), 3.0)
340 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
341 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
342 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000343
Thomas Wouters89f507f2006-12-13 04:49:30 +0000344 def testFrexp(self):
345 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000346
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000347 def testfrexp(name, result, expected):
348 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349 if abs(mant-emant) > eps or exp != eexp:
350 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000351 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000352
Thomas Wouters89f507f2006-12-13 04:49:30 +0000353 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
354 testfrexp('frexp(0)', math.frexp(0), (0, 0))
355 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
356 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000357
Christian Heimes53876d92008-04-19 00:31:39 +0000358 self.assertEquals(math.frexp(INF)[0], INF)
359 self.assertEquals(math.frexp(NINF)[0], NINF)
360 self.assert_(math.isnan(math.frexp(NAN)[0]))
361
Thomas Wouters89f507f2006-12-13 04:49:30 +0000362 def testHypot(self):
363 self.assertRaises(TypeError, math.hypot)
364 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
365 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000366 self.assertEqual(math.hypot(NAN, INF), INF)
367 self.assertEqual(math.hypot(INF, NAN), INF)
368 self.assertEqual(math.hypot(NAN, NINF), INF)
369 self.assertEqual(math.hypot(NINF, NAN), INF)
370 self.assert_(math.isnan(math.hypot(1.0, NAN)))
371 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000372
Thomas Wouters89f507f2006-12-13 04:49:30 +0000373 def testLdexp(self):
374 self.assertRaises(TypeError, math.ldexp)
375 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
376 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
377 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
378 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000379 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
380 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
381 self.assertEquals(math.ldexp(1., -1000000), 0.)
382 self.assertEquals(math.ldexp(-1., -1000000), -0.)
383 self.assertEquals(math.ldexp(INF, 30), INF)
384 self.assertEquals(math.ldexp(NINF, -213), NINF)
385 self.assert_(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000386
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000387 # large second argument
388 for n in [10**5, 10**10, 10**20, 10**40]:
389 self.assertEquals(math.ldexp(INF, -n), INF)
390 self.assertEquals(math.ldexp(NINF, -n), NINF)
391 self.assertEquals(math.ldexp(1., -n), 0.)
392 self.assertEquals(math.ldexp(-1., -n), -0.)
393 self.assertEquals(math.ldexp(0., -n), 0.)
394 self.assertEquals(math.ldexp(-0., -n), -0.)
395 self.assert_(math.isnan(math.ldexp(NAN, -n)))
396
397 self.assertRaises(OverflowError, math.ldexp, 1., n)
398 self.assertRaises(OverflowError, math.ldexp, -1., n)
399 self.assertEquals(math.ldexp(0., n), 0.)
400 self.assertEquals(math.ldexp(-0., n), -0.)
401 self.assertEquals(math.ldexp(INF, n), INF)
402 self.assertEquals(math.ldexp(NINF, n), NINF)
403 self.assert_(math.isnan(math.ldexp(NAN, n)))
404
Thomas Wouters89f507f2006-12-13 04:49:30 +0000405 def testLog(self):
406 self.assertRaises(TypeError, math.log)
407 self.ftest('log(1/e)', math.log(1/math.e), -1)
408 self.ftest('log(1)', math.log(1), 0)
409 self.ftest('log(e)', math.log(math.e), 1)
410 self.ftest('log(32,2)', math.log(32,2), 5)
411 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
412 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000413 self.assertEquals(math.log(INF), INF)
414 self.assertRaises(ValueError, math.log, NINF)
415 self.assert_(math.isnan(math.log(NAN)))
416
417 def testLog1p(self):
418 self.assertRaises(TypeError, math.log1p)
419 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
420 self.ftest('log1p(0)', math.log1p(0), 0)
421 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
422 self.ftest('log1p(1)', math.log1p(1), math.log(2))
423 self.assertEquals(math.log1p(INF), INF)
424 self.assertRaises(ValueError, math.log1p, NINF)
425 self.assert_(math.isnan(math.log1p(NAN)))
426 n= 2**90
427 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
428 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000429
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430 def testLog10(self):
431 self.assertRaises(TypeError, math.log10)
432 self.ftest('log10(0.1)', math.log10(0.1), -1)
433 self.ftest('log10(1)', math.log10(1), 0)
434 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes53876d92008-04-19 00:31:39 +0000435 self.assertEquals(math.log(INF), INF)
436 self.assertRaises(ValueError, math.log10, NINF)
437 self.assert_(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000438
Thomas Wouters89f507f2006-12-13 04:49:30 +0000439 def testModf(self):
440 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000441
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000442 def testmodf(name, result, expected):
443 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000444 if abs(v1-e1) > eps or abs(v2-e2):
445 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000446 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000447
Thomas Wouters89f507f2006-12-13 04:49:30 +0000448 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
449 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000450
Christian Heimes53876d92008-04-19 00:31:39 +0000451 self.assertEquals(math.modf(INF), (0.0, INF))
452 self.assertEquals(math.modf(NINF), (-0.0, NINF))
453
454 modf_nan = math.modf(NAN)
455 self.assert_(math.isnan(modf_nan[0]))
456 self.assert_(math.isnan(modf_nan[1]))
457
Thomas Wouters89f507f2006-12-13 04:49:30 +0000458 def testPow(self):
459 self.assertRaises(TypeError, math.pow)
460 self.ftest('pow(0,1)', math.pow(0,1), 0)
461 self.ftest('pow(1,0)', math.pow(1,0), 1)
462 self.ftest('pow(2,1)', math.pow(2,1), 2)
463 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000464 self.assertEqual(math.pow(INF, 1), INF)
465 self.assertEqual(math.pow(NINF, 1), NINF)
466 self.assertEqual((math.pow(1, INF)), 1.)
467 self.assertEqual((math.pow(1, NINF)), 1.)
468 self.assert_(math.isnan(math.pow(NAN, 1)))
469 self.assert_(math.isnan(math.pow(2, NAN)))
470 self.assert_(math.isnan(math.pow(0, NAN)))
471 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000472
473 # pow(0., x)
474 self.assertEqual(math.pow(0., INF), 0.)
475 self.assertEqual(math.pow(0., 3.), 0.)
476 self.assertEqual(math.pow(0., 2.3), 0.)
477 self.assertEqual(math.pow(0., 2.), 0.)
478 self.assertEqual(math.pow(0., 0.), 1.)
479 self.assertEqual(math.pow(0., -0.), 1.)
480 self.assertRaises(ValueError, math.pow, 0., -2.)
481 self.assertRaises(ValueError, math.pow, 0., -2.3)
482 self.assertRaises(ValueError, math.pow, 0., -3.)
483 self.assertRaises(ValueError, math.pow, 0., NINF)
484 self.assert_(math.isnan(math.pow(0., NAN)))
485
486 # pow(INF, x)
487 self.assertEqual(math.pow(INF, INF), INF)
488 self.assertEqual(math.pow(INF, 3.), INF)
489 self.assertEqual(math.pow(INF, 2.3), INF)
490 self.assertEqual(math.pow(INF, 2.), INF)
491 self.assertEqual(math.pow(INF, 0.), 1.)
492 self.assertEqual(math.pow(INF, -0.), 1.)
493 self.assertEqual(math.pow(INF, -2.), 0.)
494 self.assertEqual(math.pow(INF, -2.3), 0.)
495 self.assertEqual(math.pow(INF, -3.), 0.)
496 self.assertEqual(math.pow(INF, NINF), 0.)
497 self.assert_(math.isnan(math.pow(INF, NAN)))
498
499 # pow(-0., x)
500 self.assertEqual(math.pow(-0., INF), 0.)
501 self.assertEqual(math.pow(-0., 3.), -0.)
502 self.assertEqual(math.pow(-0., 2.3), 0.)
503 self.assertEqual(math.pow(-0., 2.), 0.)
504 self.assertEqual(math.pow(-0., 0.), 1.)
505 self.assertEqual(math.pow(-0., -0.), 1.)
506 self.assertRaises(ValueError, math.pow, -0., -2.)
507 self.assertRaises(ValueError, math.pow, -0., -2.3)
508 self.assertRaises(ValueError, math.pow, -0., -3.)
509 self.assertRaises(ValueError, math.pow, -0., NINF)
510 self.assert_(math.isnan(math.pow(-0., NAN)))
511
512 # pow(NINF, x)
513 self.assertEqual(math.pow(NINF, INF), INF)
514 self.assertEqual(math.pow(NINF, 3.), NINF)
515 self.assertEqual(math.pow(NINF, 2.3), INF)
516 self.assertEqual(math.pow(NINF, 2.), INF)
517 self.assertEqual(math.pow(NINF, 0.), 1.)
518 self.assertEqual(math.pow(NINF, -0.), 1.)
519 self.assertEqual(math.pow(NINF, -2.), 0.)
520 self.assertEqual(math.pow(NINF, -2.3), 0.)
521 self.assertEqual(math.pow(NINF, -3.), -0.)
522 self.assertEqual(math.pow(NINF, NINF), 0.)
523 self.assert_(math.isnan(math.pow(NINF, NAN)))
524
525 # pow(-1, x)
526 self.assertEqual(math.pow(-1., INF), 1.)
527 self.assertEqual(math.pow(-1., 3.), -1.)
528 self.assertRaises(ValueError, math.pow, -1., 2.3)
529 self.assertEqual(math.pow(-1., 2.), 1.)
530 self.assertEqual(math.pow(-1., 0.), 1.)
531 self.assertEqual(math.pow(-1., -0.), 1.)
532 self.assertEqual(math.pow(-1., -2.), 1.)
533 self.assertRaises(ValueError, math.pow, -1., -2.3)
534 self.assertEqual(math.pow(-1., -3.), -1.)
535 self.assertEqual(math.pow(-1., NINF), 1.)
536 self.assert_(math.isnan(math.pow(-1., NAN)))
537
538 # pow(1, x)
539 self.assertEqual(math.pow(1., INF), 1.)
540 self.assertEqual(math.pow(1., 3.), 1.)
541 self.assertEqual(math.pow(1., 2.3), 1.)
542 self.assertEqual(math.pow(1., 2.), 1.)
543 self.assertEqual(math.pow(1., 0.), 1.)
544 self.assertEqual(math.pow(1., -0.), 1.)
545 self.assertEqual(math.pow(1., -2.), 1.)
546 self.assertEqual(math.pow(1., -2.3), 1.)
547 self.assertEqual(math.pow(1., -3.), 1.)
548 self.assertEqual(math.pow(1., NINF), 1.)
549 self.assertEqual(math.pow(1., NAN), 1.)
550
551 # pow(x, 0) should be 1 for any x
552 self.assertEqual(math.pow(2.3, 0.), 1.)
553 self.assertEqual(math.pow(-2.3, 0.), 1.)
554 self.assertEqual(math.pow(NAN, 0.), 1.)
555 self.assertEqual(math.pow(2.3, -0.), 1.)
556 self.assertEqual(math.pow(-2.3, -0.), 1.)
557 self.assertEqual(math.pow(NAN, -0.), 1.)
558
559 # pow(x, y) is invalid if x is negative and y is not integral
560 self.assertRaises(ValueError, math.pow, -1., 2.3)
561 self.assertRaises(ValueError, math.pow, -15., -3.1)
562
563 # pow(x, NINF)
564 self.assertEqual(math.pow(1.9, NINF), 0.)
565 self.assertEqual(math.pow(1.1, NINF), 0.)
566 self.assertEqual(math.pow(0.9, NINF), INF)
567 self.assertEqual(math.pow(0.1, NINF), INF)
568 self.assertEqual(math.pow(-0.1, NINF), INF)
569 self.assertEqual(math.pow(-0.9, NINF), INF)
570 self.assertEqual(math.pow(-1.1, NINF), 0.)
571 self.assertEqual(math.pow(-1.9, NINF), 0.)
572
573 # pow(x, INF)
574 self.assertEqual(math.pow(1.9, INF), INF)
575 self.assertEqual(math.pow(1.1, INF), INF)
576 self.assertEqual(math.pow(0.9, INF), 0.)
577 self.assertEqual(math.pow(0.1, INF), 0.)
578 self.assertEqual(math.pow(-0.1, INF), 0.)
579 self.assertEqual(math.pow(-0.9, INF), 0.)
580 self.assertEqual(math.pow(-1.1, INF), INF)
581 self.assertEqual(math.pow(-1.9, INF), INF)
582
583 # pow(x, y) should work for x negative, y an integer
584 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
585 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
586 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
587 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
588 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
589 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
590 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
591 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
592 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
593 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
594
595 # the following tests have been commented out since they don't
596 # really belong here: the implementation of ** for floats is
597 # independent of the implemention of math.pow
598 #self.assertEqual(1**NAN, 1)
599 #self.assertEqual(1**INF, 1)
600 #self.assertEqual(1**NINF, 1)
601 #self.assertEqual(1**0, 1)
602 #self.assertEqual(1.**NAN, 1)
603 #self.assertEqual(1.**INF, 1)
604 #self.assertEqual(1.**NINF, 1)
605 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000606
Thomas Wouters89f507f2006-12-13 04:49:30 +0000607 def testRadians(self):
608 self.assertRaises(TypeError, math.radians)
609 self.ftest('radians(180)', math.radians(180), math.pi)
610 self.ftest('radians(90)', math.radians(90), math.pi/2)
611 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000612
Thomas Wouters89f507f2006-12-13 04:49:30 +0000613 def testSin(self):
614 self.assertRaises(TypeError, math.sin)
615 self.ftest('sin(0)', math.sin(0), 0)
616 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
617 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000618 try:
619 self.assert_(math.isnan(math.sin(INF)))
620 self.assert_(math.isnan(math.sin(NINF)))
621 except ValueError:
622 self.assertRaises(ValueError, math.sin, INF)
623 self.assertRaises(ValueError, math.sin, NINF)
624 self.assert_(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000625
Thomas Wouters89f507f2006-12-13 04:49:30 +0000626 def testSinh(self):
627 self.assertRaises(TypeError, math.sinh)
628 self.ftest('sinh(0)', math.sinh(0), 0)
629 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
630 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000631 self.assertEquals(math.sinh(INF), INF)
Christian Heimesa342c012008-04-20 21:01:16 +0000632 self.assertEquals(math.sinh(NINF), NINF)
Christian Heimes53876d92008-04-19 00:31:39 +0000633 self.assert_(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000634
Thomas Wouters89f507f2006-12-13 04:49:30 +0000635 def testSqrt(self):
636 self.assertRaises(TypeError, math.sqrt)
637 self.ftest('sqrt(0)', math.sqrt(0), 0)
638 self.ftest('sqrt(1)', math.sqrt(1), 1)
639 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000640 self.assertEquals(math.sqrt(INF), INF)
641 self.assertRaises(ValueError, math.sqrt, NINF)
642 self.assert_(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000643
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000644 def testSum(self):
645 # math.sum relies on exact rounding for correct operation.
646 # There's a known problem with IA32 floating-point that causes
647 # inexact rounding in some situations, and will cause the
648 # math.sum tests below to fail; see issue #2937. On non IEEE
649 # 754 platforms, and on IEEE 754 platforms that exhibit the
650 # problem described in issue #2937, we simply skip the whole
651 # test.
652
653 if not float.__getformat__("double").startswith("IEEE"):
654 return
655
656 # on IEEE 754 compliant machines, both of the expressions
657 # below should round to 10000000000000002.0.
658 if 1e16+2.999 != 1e16+2.9999:
659 return
660
661 # Python version of math.sum algorithm, for comparison
662 def msum(iterable):
663 """Full precision sum of values in iterable. Returns the value of
664 the sum, rounded to the nearest representable floating-point number
665 using the round-half-to-even rule.
666
667 """
668 # Stage 1: accumulate partials
669 partials = []
670 for x in iterable:
671 i = 0
672 for y in partials:
673 if abs(x) < abs(y):
674 x, y = y, x
675 hi = x + y
676 lo = y - (hi - x)
677 if lo:
678 partials[i] = lo
679 i += 1
680 x = hi
681 partials[i:] = [x] if x else []
682
683 # Stage 2: sum partials
684 if not partials:
685 return 0.0
686
687 # sum from the top, stopping as soon as the sum is inexact.
688 total = partials.pop()
689 while partials:
690 x = partials.pop()
691 old_total, total = total, total + x
692 error = x - (total - old_total)
693 if error != 0.0:
694 # adjust for correct rounding if necessary
695 if partials and (partials[-1] > 0.0) == (error > 0.0) and \
696 total + 2*error - total == 2*error:
697 total += 2*error
698 break
699 return total
700
701 from sys import float_info
702 maxfloat = float_info.max
703 twopow = 2.**(float_info.max_exp - 1)
704
705 test_values = [
706 ([], 0.0),
707 ([0.0], 0.0),
708 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
709 ([1e308, 1e308, -1e308], OverflowError),
710 ([-1e308, 1e308, 1e308], 1e308),
711 ([1e308, -1e308, 1e308], 1e308),
712 ([2.0**1023, 2.0**1023, -2.0**1000], OverflowError),
713 ([twopow, twopow, twopow, twopow, -twopow, -twopow, -twopow],
714 OverflowError),
715 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
716 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
717 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
718
719 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
720 ([2.0**1023-2.0**970, -1.0, 2.0**1023], OverflowError),
721 ([maxfloat, maxfloat*2.**-54], maxfloat),
722 ([maxfloat, maxfloat*2.**-53], OverflowError),
723 ([1./n for n in range(1, 1001)], 7.4854708605503451),
724 ([(-1.)**n/n for n in range(1, 1001)], -0.69264743055982025),
725 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
726 ([INF, -INF, NAN], ValueError),
727 ([NAN, INF, -INF], ValueError),
728 ([INF, NAN, INF], ValueError),
729
730 ([INF, INF], OverflowError),
731 ([INF, -INF], ValueError),
732 ([-INF, 1e308, 1e308, -INF], OverflowError),
733 ([2.0**1023-2.0**970, 0.0, 2.0**1023], OverflowError),
734 ([2.0**1023-2.0**970, 1.0, 2.0**1023], OverflowError),
735 ([2.0**1023, 2.0**1023], OverflowError),
736 ([2.0**1023, 2.0**1023, -1.0], OverflowError),
737 ([twopow, twopow, twopow, twopow, -twopow, -twopow],
738 OverflowError),
739 ([twopow, twopow, twopow, twopow, -twopow, twopow], OverflowError),
740 ([-twopow, -twopow, -twopow, -twopow], OverflowError),
741
742 ([2.**1023, 2.**1023, -2.**971], OverflowError),
743 ([2.**1023, 2.**1023, -2.**970], OverflowError),
744 ([-2.**970, 2.**1023, 2.**1023, -2.**-1074], OverflowError),
745 ([ 2.**1023, 2.**1023, -2.**970, 2.**-1074], OverflowError),
746 ([-2.**1023, 2.**971, -2.**1023], -maxfloat),
747 ([-2.**1023, -2.**1023, 2.**970], OverflowError),
748 ([-2.**1023, -2.**1023, 2.**970, 2.**-1074], OverflowError),
749 ([-2.**-1074, -2.**1023, -2.**1023, 2.**970], OverflowError),
750 ([2.**930, -2.**980, 2.**1023, 2.**1023, twopow, -twopow],
751 OverflowError),
752 ([2.**1023, 2.**1023, -1e307], OverflowError),
753 ([1e16, 1., 1e-16], 10000000000000002.0),
Georg Brandlf78e02b2008-06-10 17:40:04 +0000754 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000755 ]
756
757 for i, (vals, s) in enumerate(test_values):
758 if isinstance(s, type) and issubclass(s, Exception):
759 try:
760 m = math.sum(vals)
761 except s:
762 pass
763 else:
764 self.fail("test %d failed: got %r, expected %r "
765 "for math.sum(%.100r)" %
766 (i, m, s.__name__, vals))
767 else:
768 try:
769 self.assertEqual(math.sum(vals), s)
770 except OverflowError:
771 self.fail("test %d failed: got OverflowError, expected %r "
772 "for math.sum(%.100r)" % (i, s, vals))
773 except ValueError:
774 self.fail("test %d failed: got ValueError, expected %r "
775 "for math.sum(%.100r)" % (i, s, vals))
776
777 # compare with output of msum above, but only when
778 # result isn't an IEEE special or an exception
779 if not math.isinf(s) and not math.isnan(s):
780 self.assertEqual(msum(vals), s)
781
782 from random import random, gauss, shuffle
783 for j in range(1000):
784 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
785 s = 0
786 for i in range(200):
787 v = gauss(0, random()) ** 7 - s
788 s += v
789 vals.append(v)
790 shuffle(vals)
791
792 s = msum(vals)
793 self.assertEqual(msum(vals), math.sum(vals))
794
795
Thomas Wouters89f507f2006-12-13 04:49:30 +0000796 def testTan(self):
797 self.assertRaises(TypeError, math.tan)
798 self.ftest('tan(0)', math.tan(0), 0)
799 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
800 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000801 try:
802 self.assert_(math.isnan(math.tan(INF)))
803 self.assert_(math.isnan(math.tan(NINF)))
804 except:
805 self.assertRaises(ValueError, math.tan, INF)
806 self.assertRaises(ValueError, math.tan, NINF)
807 self.assert_(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000808
Thomas Wouters89f507f2006-12-13 04:49:30 +0000809 def testTanh(self):
810 self.assertRaises(TypeError, math.tanh)
811 self.ftest('tanh(0)', math.tanh(0), 0)
812 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000813 self.ftest('tanh(inf)', math.tanh(INF), 1)
814 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
815 self.assert_(math.isnan(math.tanh(NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000816 # check that tanh(-0.) == -0. on IEEE 754 systems
817 if float.__getformat__("double").startswith("IEEE"):
818 self.assertEqual(math.tanh(-0.), -0.)
819 self.assertEqual(math.copysign(1., math.tanh(-0.)),
820 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000821
Christian Heimes400adb02008-02-01 08:12:03 +0000822 def test_trunc(self):
823 self.assertEqual(math.trunc(1), 1)
824 self.assertEqual(math.trunc(-1), -1)
825 self.assertEqual(type(math.trunc(1)), int)
826 self.assertEqual(type(math.trunc(1.5)), int)
827 self.assertEqual(math.trunc(1.5), 1)
828 self.assertEqual(math.trunc(-1.5), -1)
829 self.assertEqual(math.trunc(1.999999), 1)
830 self.assertEqual(math.trunc(-1.999999), -1)
831 self.assertEqual(math.trunc(-0.999999), -0)
832 self.assertEqual(math.trunc(-100.999), -100)
833
834 class TestTrunc(object):
835 def __trunc__(self):
836 return 23
837
838 class TestNoTrunc(object):
839 pass
840
841 self.assertEqual(math.trunc(TestTrunc()), 23)
842
843 self.assertRaises(TypeError, math.trunc)
844 self.assertRaises(TypeError, math.trunc, 1, 2)
845 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
846
847 # XXX Doesn't work because the method is looked up on
848 # the type only.
849 #t = TestNoTrunc()
850 #t.__trunc__ = lambda *args: args
851 #self.assertEquals((), math.trunc(t))
852 #self.assertRaises(TypeError, math.trunc, t, 0)
853
Christian Heimes072c0f12008-01-03 23:01:04 +0000854 def testCopysign(self):
855 self.assertEqual(math.copysign(1, 42), 1.0)
856 self.assertEqual(math.copysign(0., 42), 0.0)
857 self.assertEqual(math.copysign(1., -42), -1.0)
858 self.assertEqual(math.copysign(3, 0.), 3.0)
859 self.assertEqual(math.copysign(4., -0.), -4.0)
860
861 def testIsnan(self):
862 self.assert_(math.isnan(float("nan")))
863 self.assert_(math.isnan(float("inf")* 0.))
864 self.failIf(math.isnan(float("inf")))
865 self.failIf(math.isnan(0.))
866 self.failIf(math.isnan(1.))
867
868 def testIsinf(self):
869 self.assert_(math.isinf(float("inf")))
870 self.assert_(math.isinf(float("-inf")))
871 self.assert_(math.isinf(1E400))
872 self.assert_(math.isinf(-1E400))
873 self.failIf(math.isinf(float("nan")))
874 self.failIf(math.isinf(0.))
875 self.failIf(math.isinf(1.))
876
Thomas Wouters89f507f2006-12-13 04:49:30 +0000877 # RED_FLAG 16-Oct-2000 Tim
878 # While 2.0 is more consistent about exceptions than previous releases, it
879 # still fails this part of the test on some platforms. For now, we only
880 # *run* test_exceptions() in verbose mode, so that this isn't normally
881 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000882
Thomas Wouters89f507f2006-12-13 04:49:30 +0000883 if verbose:
884 def test_exceptions(self):
885 try:
886 x = math.exp(-1000000000)
887 except:
888 # mathmodule.c is failing to weed out underflows from libm, or
889 # we've got an fp format with huge dynamic range
890 self.fail("underflowing exp() should not have raised "
891 "an exception")
892 if x != 0:
893 self.fail("underflowing exp() should have returned 0")
894
895 # If this fails, probably using a strict IEEE-754 conforming libm, and x
896 # is +Inf afterwards. But Python wants overflows detected by default.
897 try:
898 x = math.exp(1000000000)
899 except OverflowError:
900 pass
901 else:
902 self.fail("overflowing exp() didn't trigger OverflowError")
903
904 # If this fails, it could be a puzzle. One odd possibility is that
905 # mathmodule.c's macros are getting confused while comparing
906 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
907 # as a result (and so raising OverflowError instead).
908 try:
909 x = math.sqrt(-1.0)
910 except ValueError:
911 pass
912 else:
913 self.fail("sqrt(-1) didn't raise ValueError")
914
Christian Heimes53876d92008-04-19 00:31:39 +0000915 def test_testfile(self):
916 if not float.__getformat__("double").startswith("IEEE"):
917 return
918 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
919 # Skip if either the input or result is complex, or if
920 # flags is nonempty
921 if ai != 0. or ei != 0. or flags:
922 continue
923 if fn in ['rect', 'polar']:
924 # no real versions of rect, polar
925 continue
926 func = getattr(math, fn)
Christian Heimesa342c012008-04-20 21:01:16 +0000927 try:
928 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +0000929 except ValueError as exc:
930 message = (("Unexpected ValueError: %s\n " +
931 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +0000932 self.fail(message)
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000933 except OverflowError:
934 message = ("Unexpected OverflowError in " +
935 "test %s:%s(%r)\n" % (id, fn, ar))
936 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +0000937 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000938
939def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +0000940 from doctest import DocFileSuite
941 suite = unittest.TestSuite()
942 suite.addTest(unittest.makeSuite(MathTests))
943 suite.addTest(DocFileSuite("ieee754.txt"))
944 run_unittest(suite)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000945
946if __name__ == '__main__':
947 test_main()