blob: 9e646ac29ce74f193ccb3bd85668ba64a470e834 [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
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000362 def testFsum(self):
363 # math.fsum relies on exact rounding for correct operation.
364 # There's a known problem with IA32 floating-point that causes
365 # inexact rounding in some situations, and will cause the
366 # math.fsum tests below to fail; see issue #2937. On non IEEE
367 # 754 platforms, and on IEEE 754 platforms that exhibit the
368 # problem described in issue #2937, we simply skip the whole
369 # test.
370
371 if not float.__getformat__("double").startswith("IEEE"):
372 return
373
374 # on IEEE 754 compliant machines, both of the expressions
375 # below should round to 10000000000000002.0.
376 if 1e16+2.0 != 1e16+2.9999:
377 return
378
379 # Python version of math.fsum, for comparison. Uses a
380 # different algorithm based on frexp, ldexp and integer
381 # arithmetic.
382 from sys import float_info
383 mant_dig = float_info.mant_dig
384 etiny = float_info.min_exp - mant_dig
385
386 def msum(iterable):
387 """Full precision summation. Compute sum(iterable) without any
388 intermediate accumulation of error. Based on the 'lsum' function
389 at http://code.activestate.com/recipes/393090/
390
391 """
392 tmant, texp = 0, 0
393 for x in iterable:
394 mant, exp = math.frexp(x)
395 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
396 if texp > exp:
397 tmant <<= texp-exp
398 texp = exp
399 else:
400 mant <<= exp-texp
401 tmant += mant
402 # Round tmant * 2**texp to a float. The original recipe
403 # used float(str(tmant)) * 2.0**texp for this, but that's
404 # a little unsafe because str -> float conversion can't be
405 # relied upon to do correct rounding on all platforms.
406 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
407 if tail > 0:
408 h = 1 << (tail-1)
409 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
410 texp += tail
411 return math.ldexp(tmant, texp)
412
413 test_values = [
414 ([], 0.0),
415 ([0.0], 0.0),
416 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
417 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
418 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
419 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
420 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
421 ([1./n for n in range(1, 1001)],
422 float.fromhex('0x1.df11f45f4e61ap+2')),
423 ([(-1.)**n/n for n in range(1, 1001)],
424 float.fromhex('-0x1.62a2af1bd3624p-1')),
425 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
426 ([1e16, 1., 1e-16], 10000000000000002.0),
427 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
428 # exercise code for resizing partials array
429 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
430 [-2.**1022],
431 float.fromhex('0x1.5555555555555p+970')),
432 ]
433
434 for i, (vals, expected) in enumerate(test_values):
435 try:
436 actual = math.fsum(vals)
437 except OverflowError:
438 self.fail("test %d failed: got OverflowError, expected %r "
439 "for math.fsum(%.100r)" % (i, expected, vals))
440 except ValueError:
441 self.fail("test %d failed: got ValueError, expected %r "
442 "for math.fsum(%.100r)" % (i, expected, vals))
443 self.assertEqual(actual, expected)
444
445 from random import random, gauss, shuffle
446 for j in range(1000):
447 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
448 s = 0
449 for i in range(200):
450 v = gauss(0, random()) ** 7 - s
451 s += v
452 vals.append(v)
453 shuffle(vals)
454
455 s = msum(vals)
456 self.assertEqual(msum(vals), math.fsum(vals))
457
Thomas Wouters89f507f2006-12-13 04:49:30 +0000458 def testHypot(self):
459 self.assertRaises(TypeError, math.hypot)
460 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
461 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000462 self.assertEqual(math.hypot(NAN, INF), INF)
463 self.assertEqual(math.hypot(INF, NAN), INF)
464 self.assertEqual(math.hypot(NAN, NINF), INF)
465 self.assertEqual(math.hypot(NINF, NAN), INF)
466 self.assert_(math.isnan(math.hypot(1.0, NAN)))
467 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000468
Thomas Wouters89f507f2006-12-13 04:49:30 +0000469 def testLdexp(self):
470 self.assertRaises(TypeError, math.ldexp)
471 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
472 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
473 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
474 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000475 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
476 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
477 self.assertEquals(math.ldexp(1., -1000000), 0.)
478 self.assertEquals(math.ldexp(-1., -1000000), -0.)
479 self.assertEquals(math.ldexp(INF, 30), INF)
480 self.assertEquals(math.ldexp(NINF, -213), NINF)
481 self.assert_(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000482
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000483 # large second argument
484 for n in [10**5, 10**10, 10**20, 10**40]:
485 self.assertEquals(math.ldexp(INF, -n), INF)
486 self.assertEquals(math.ldexp(NINF, -n), NINF)
487 self.assertEquals(math.ldexp(1., -n), 0.)
488 self.assertEquals(math.ldexp(-1., -n), -0.)
489 self.assertEquals(math.ldexp(0., -n), 0.)
490 self.assertEquals(math.ldexp(-0., -n), -0.)
491 self.assert_(math.isnan(math.ldexp(NAN, -n)))
492
493 self.assertRaises(OverflowError, math.ldexp, 1., n)
494 self.assertRaises(OverflowError, math.ldexp, -1., n)
495 self.assertEquals(math.ldexp(0., n), 0.)
496 self.assertEquals(math.ldexp(-0., n), -0.)
497 self.assertEquals(math.ldexp(INF, n), INF)
498 self.assertEquals(math.ldexp(NINF, n), NINF)
499 self.assert_(math.isnan(math.ldexp(NAN, n)))
500
Thomas Wouters89f507f2006-12-13 04:49:30 +0000501 def testLog(self):
502 self.assertRaises(TypeError, math.log)
503 self.ftest('log(1/e)', math.log(1/math.e), -1)
504 self.ftest('log(1)', math.log(1), 0)
505 self.ftest('log(e)', math.log(math.e), 1)
506 self.ftest('log(32,2)', math.log(32,2), 5)
507 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
508 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000509 self.assertEquals(math.log(INF), INF)
510 self.assertRaises(ValueError, math.log, NINF)
511 self.assert_(math.isnan(math.log(NAN)))
512
513 def testLog1p(self):
514 self.assertRaises(TypeError, math.log1p)
515 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
516 self.ftest('log1p(0)', math.log1p(0), 0)
517 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
518 self.ftest('log1p(1)', math.log1p(1), math.log(2))
519 self.assertEquals(math.log1p(INF), INF)
520 self.assertRaises(ValueError, math.log1p, NINF)
521 self.assert_(math.isnan(math.log1p(NAN)))
522 n= 2**90
523 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
524 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000525
Thomas Wouters89f507f2006-12-13 04:49:30 +0000526 def testLog10(self):
527 self.assertRaises(TypeError, math.log10)
528 self.ftest('log10(0.1)', math.log10(0.1), -1)
529 self.ftest('log10(1)', math.log10(1), 0)
530 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes53876d92008-04-19 00:31:39 +0000531 self.assertEquals(math.log(INF), INF)
532 self.assertRaises(ValueError, math.log10, NINF)
533 self.assert_(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000534
Thomas Wouters89f507f2006-12-13 04:49:30 +0000535 def testModf(self):
536 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000537
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000538 def testmodf(name, result, expected):
539 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000540 if abs(v1-e1) > eps or abs(v2-e2):
541 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000542 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000543
Thomas Wouters89f507f2006-12-13 04:49:30 +0000544 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
545 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000546
Christian Heimes53876d92008-04-19 00:31:39 +0000547 self.assertEquals(math.modf(INF), (0.0, INF))
548 self.assertEquals(math.modf(NINF), (-0.0, NINF))
549
550 modf_nan = math.modf(NAN)
551 self.assert_(math.isnan(modf_nan[0]))
552 self.assert_(math.isnan(modf_nan[1]))
553
Thomas Wouters89f507f2006-12-13 04:49:30 +0000554 def testPow(self):
555 self.assertRaises(TypeError, math.pow)
556 self.ftest('pow(0,1)', math.pow(0,1), 0)
557 self.ftest('pow(1,0)', math.pow(1,0), 1)
558 self.ftest('pow(2,1)', math.pow(2,1), 2)
559 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000560 self.assertEqual(math.pow(INF, 1), INF)
561 self.assertEqual(math.pow(NINF, 1), NINF)
562 self.assertEqual((math.pow(1, INF)), 1.)
563 self.assertEqual((math.pow(1, NINF)), 1.)
564 self.assert_(math.isnan(math.pow(NAN, 1)))
565 self.assert_(math.isnan(math.pow(2, NAN)))
566 self.assert_(math.isnan(math.pow(0, NAN)))
567 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000568
569 # pow(0., x)
570 self.assertEqual(math.pow(0., INF), 0.)
571 self.assertEqual(math.pow(0., 3.), 0.)
572 self.assertEqual(math.pow(0., 2.3), 0.)
573 self.assertEqual(math.pow(0., 2.), 0.)
574 self.assertEqual(math.pow(0., 0.), 1.)
575 self.assertEqual(math.pow(0., -0.), 1.)
576 self.assertRaises(ValueError, math.pow, 0., -2.)
577 self.assertRaises(ValueError, math.pow, 0., -2.3)
578 self.assertRaises(ValueError, math.pow, 0., -3.)
579 self.assertRaises(ValueError, math.pow, 0., NINF)
580 self.assert_(math.isnan(math.pow(0., NAN)))
581
582 # pow(INF, x)
583 self.assertEqual(math.pow(INF, INF), INF)
584 self.assertEqual(math.pow(INF, 3.), INF)
585 self.assertEqual(math.pow(INF, 2.3), INF)
586 self.assertEqual(math.pow(INF, 2.), INF)
587 self.assertEqual(math.pow(INF, 0.), 1.)
588 self.assertEqual(math.pow(INF, -0.), 1.)
589 self.assertEqual(math.pow(INF, -2.), 0.)
590 self.assertEqual(math.pow(INF, -2.3), 0.)
591 self.assertEqual(math.pow(INF, -3.), 0.)
592 self.assertEqual(math.pow(INF, NINF), 0.)
593 self.assert_(math.isnan(math.pow(INF, NAN)))
594
595 # pow(-0., x)
596 self.assertEqual(math.pow(-0., INF), 0.)
597 self.assertEqual(math.pow(-0., 3.), -0.)
598 self.assertEqual(math.pow(-0., 2.3), 0.)
599 self.assertEqual(math.pow(-0., 2.), 0.)
600 self.assertEqual(math.pow(-0., 0.), 1.)
601 self.assertEqual(math.pow(-0., -0.), 1.)
602 self.assertRaises(ValueError, math.pow, -0., -2.)
603 self.assertRaises(ValueError, math.pow, -0., -2.3)
604 self.assertRaises(ValueError, math.pow, -0., -3.)
605 self.assertRaises(ValueError, math.pow, -0., NINF)
606 self.assert_(math.isnan(math.pow(-0., NAN)))
607
608 # pow(NINF, x)
609 self.assertEqual(math.pow(NINF, INF), INF)
610 self.assertEqual(math.pow(NINF, 3.), NINF)
611 self.assertEqual(math.pow(NINF, 2.3), INF)
612 self.assertEqual(math.pow(NINF, 2.), INF)
613 self.assertEqual(math.pow(NINF, 0.), 1.)
614 self.assertEqual(math.pow(NINF, -0.), 1.)
615 self.assertEqual(math.pow(NINF, -2.), 0.)
616 self.assertEqual(math.pow(NINF, -2.3), 0.)
617 self.assertEqual(math.pow(NINF, -3.), -0.)
618 self.assertEqual(math.pow(NINF, NINF), 0.)
619 self.assert_(math.isnan(math.pow(NINF, NAN)))
620
621 # pow(-1, x)
622 self.assertEqual(math.pow(-1., INF), 1.)
623 self.assertEqual(math.pow(-1., 3.), -1.)
624 self.assertRaises(ValueError, math.pow, -1., 2.3)
625 self.assertEqual(math.pow(-1., 2.), 1.)
626 self.assertEqual(math.pow(-1., 0.), 1.)
627 self.assertEqual(math.pow(-1., -0.), 1.)
628 self.assertEqual(math.pow(-1., -2.), 1.)
629 self.assertRaises(ValueError, math.pow, -1., -2.3)
630 self.assertEqual(math.pow(-1., -3.), -1.)
631 self.assertEqual(math.pow(-1., NINF), 1.)
632 self.assert_(math.isnan(math.pow(-1., NAN)))
633
634 # pow(1, x)
635 self.assertEqual(math.pow(1., INF), 1.)
636 self.assertEqual(math.pow(1., 3.), 1.)
637 self.assertEqual(math.pow(1., 2.3), 1.)
638 self.assertEqual(math.pow(1., 2.), 1.)
639 self.assertEqual(math.pow(1., 0.), 1.)
640 self.assertEqual(math.pow(1., -0.), 1.)
641 self.assertEqual(math.pow(1., -2.), 1.)
642 self.assertEqual(math.pow(1., -2.3), 1.)
643 self.assertEqual(math.pow(1., -3.), 1.)
644 self.assertEqual(math.pow(1., NINF), 1.)
645 self.assertEqual(math.pow(1., NAN), 1.)
646
647 # pow(x, 0) should be 1 for any x
648 self.assertEqual(math.pow(2.3, 0.), 1.)
649 self.assertEqual(math.pow(-2.3, 0.), 1.)
650 self.assertEqual(math.pow(NAN, 0.), 1.)
651 self.assertEqual(math.pow(2.3, -0.), 1.)
652 self.assertEqual(math.pow(-2.3, -0.), 1.)
653 self.assertEqual(math.pow(NAN, -0.), 1.)
654
655 # pow(x, y) is invalid if x is negative and y is not integral
656 self.assertRaises(ValueError, math.pow, -1., 2.3)
657 self.assertRaises(ValueError, math.pow, -15., -3.1)
658
659 # pow(x, NINF)
660 self.assertEqual(math.pow(1.9, NINF), 0.)
661 self.assertEqual(math.pow(1.1, NINF), 0.)
662 self.assertEqual(math.pow(0.9, NINF), INF)
663 self.assertEqual(math.pow(0.1, NINF), INF)
664 self.assertEqual(math.pow(-0.1, NINF), INF)
665 self.assertEqual(math.pow(-0.9, NINF), INF)
666 self.assertEqual(math.pow(-1.1, NINF), 0.)
667 self.assertEqual(math.pow(-1.9, NINF), 0.)
668
669 # pow(x, INF)
670 self.assertEqual(math.pow(1.9, INF), INF)
671 self.assertEqual(math.pow(1.1, INF), INF)
672 self.assertEqual(math.pow(0.9, INF), 0.)
673 self.assertEqual(math.pow(0.1, INF), 0.)
674 self.assertEqual(math.pow(-0.1, INF), 0.)
675 self.assertEqual(math.pow(-0.9, INF), 0.)
676 self.assertEqual(math.pow(-1.1, INF), INF)
677 self.assertEqual(math.pow(-1.9, INF), INF)
678
679 # pow(x, y) should work for x negative, y an integer
680 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
681 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
682 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
683 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
684 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
685 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
686 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
687 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
688 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
689 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
690
691 # the following tests have been commented out since they don't
692 # really belong here: the implementation of ** for floats is
693 # independent of the implemention of math.pow
694 #self.assertEqual(1**NAN, 1)
695 #self.assertEqual(1**INF, 1)
696 #self.assertEqual(1**NINF, 1)
697 #self.assertEqual(1**0, 1)
698 #self.assertEqual(1.**NAN, 1)
699 #self.assertEqual(1.**INF, 1)
700 #self.assertEqual(1.**NINF, 1)
701 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000702
Thomas Wouters89f507f2006-12-13 04:49:30 +0000703 def testRadians(self):
704 self.assertRaises(TypeError, math.radians)
705 self.ftest('radians(180)', math.radians(180), math.pi)
706 self.ftest('radians(90)', math.radians(90), math.pi/2)
707 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000708
Thomas Wouters89f507f2006-12-13 04:49:30 +0000709 def testSin(self):
710 self.assertRaises(TypeError, math.sin)
711 self.ftest('sin(0)', math.sin(0), 0)
712 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
713 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000714 try:
715 self.assert_(math.isnan(math.sin(INF)))
716 self.assert_(math.isnan(math.sin(NINF)))
717 except ValueError:
718 self.assertRaises(ValueError, math.sin, INF)
719 self.assertRaises(ValueError, math.sin, NINF)
720 self.assert_(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000721
Thomas Wouters89f507f2006-12-13 04:49:30 +0000722 def testSinh(self):
723 self.assertRaises(TypeError, math.sinh)
724 self.ftest('sinh(0)', math.sinh(0), 0)
725 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
726 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000727 self.assertEquals(math.sinh(INF), INF)
Christian Heimesa342c012008-04-20 21:01:16 +0000728 self.assertEquals(math.sinh(NINF), NINF)
Christian Heimes53876d92008-04-19 00:31:39 +0000729 self.assert_(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000730
Thomas Wouters89f507f2006-12-13 04:49:30 +0000731 def testSqrt(self):
732 self.assertRaises(TypeError, math.sqrt)
733 self.ftest('sqrt(0)', math.sqrt(0), 0)
734 self.ftest('sqrt(1)', math.sqrt(1), 1)
735 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000736 self.assertEquals(math.sqrt(INF), INF)
737 self.assertRaises(ValueError, math.sqrt, NINF)
738 self.assert_(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000739
Thomas Wouters89f507f2006-12-13 04:49:30 +0000740 def testTan(self):
741 self.assertRaises(TypeError, math.tan)
742 self.ftest('tan(0)', math.tan(0), 0)
743 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
744 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000745 try:
746 self.assert_(math.isnan(math.tan(INF)))
747 self.assert_(math.isnan(math.tan(NINF)))
748 except:
749 self.assertRaises(ValueError, math.tan, INF)
750 self.assertRaises(ValueError, math.tan, NINF)
751 self.assert_(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000752
Thomas Wouters89f507f2006-12-13 04:49:30 +0000753 def testTanh(self):
754 self.assertRaises(TypeError, math.tanh)
755 self.ftest('tanh(0)', math.tanh(0), 0)
756 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000757 self.ftest('tanh(inf)', math.tanh(INF), 1)
758 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
759 self.assert_(math.isnan(math.tanh(NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000760 # check that tanh(-0.) == -0. on IEEE 754 systems
761 if float.__getformat__("double").startswith("IEEE"):
762 self.assertEqual(math.tanh(-0.), -0.)
763 self.assertEqual(math.copysign(1., math.tanh(-0.)),
764 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000765
Christian Heimes400adb02008-02-01 08:12:03 +0000766 def test_trunc(self):
767 self.assertEqual(math.trunc(1), 1)
768 self.assertEqual(math.trunc(-1), -1)
769 self.assertEqual(type(math.trunc(1)), int)
770 self.assertEqual(type(math.trunc(1.5)), int)
771 self.assertEqual(math.trunc(1.5), 1)
772 self.assertEqual(math.trunc(-1.5), -1)
773 self.assertEqual(math.trunc(1.999999), 1)
774 self.assertEqual(math.trunc(-1.999999), -1)
775 self.assertEqual(math.trunc(-0.999999), -0)
776 self.assertEqual(math.trunc(-100.999), -100)
777
778 class TestTrunc(object):
779 def __trunc__(self):
780 return 23
781
782 class TestNoTrunc(object):
783 pass
784
785 self.assertEqual(math.trunc(TestTrunc()), 23)
786
787 self.assertRaises(TypeError, math.trunc)
788 self.assertRaises(TypeError, math.trunc, 1, 2)
789 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
790
791 # XXX Doesn't work because the method is looked up on
792 # the type only.
793 #t = TestNoTrunc()
794 #t.__trunc__ = lambda *args: args
795 #self.assertEquals((), math.trunc(t))
796 #self.assertRaises(TypeError, math.trunc, t, 0)
797
Christian Heimes072c0f12008-01-03 23:01:04 +0000798 def testCopysign(self):
799 self.assertEqual(math.copysign(1, 42), 1.0)
800 self.assertEqual(math.copysign(0., 42), 0.0)
801 self.assertEqual(math.copysign(1., -42), -1.0)
802 self.assertEqual(math.copysign(3, 0.), 3.0)
803 self.assertEqual(math.copysign(4., -0.), -4.0)
804
805 def testIsnan(self):
806 self.assert_(math.isnan(float("nan")))
807 self.assert_(math.isnan(float("inf")* 0.))
808 self.failIf(math.isnan(float("inf")))
809 self.failIf(math.isnan(0.))
810 self.failIf(math.isnan(1.))
811
812 def testIsinf(self):
813 self.assert_(math.isinf(float("inf")))
814 self.assert_(math.isinf(float("-inf")))
815 self.assert_(math.isinf(1E400))
816 self.assert_(math.isinf(-1E400))
817 self.failIf(math.isinf(float("nan")))
818 self.failIf(math.isinf(0.))
819 self.failIf(math.isinf(1.))
820
Thomas Wouters89f507f2006-12-13 04:49:30 +0000821 # RED_FLAG 16-Oct-2000 Tim
822 # While 2.0 is more consistent about exceptions than previous releases, it
823 # still fails this part of the test on some platforms. For now, we only
824 # *run* test_exceptions() in verbose mode, so that this isn't normally
825 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000826
Thomas Wouters89f507f2006-12-13 04:49:30 +0000827 if verbose:
828 def test_exceptions(self):
829 try:
830 x = math.exp(-1000000000)
831 except:
832 # mathmodule.c is failing to weed out underflows from libm, or
833 # we've got an fp format with huge dynamic range
834 self.fail("underflowing exp() should not have raised "
835 "an exception")
836 if x != 0:
837 self.fail("underflowing exp() should have returned 0")
838
839 # If this fails, probably using a strict IEEE-754 conforming libm, and x
840 # is +Inf afterwards. But Python wants overflows detected by default.
841 try:
842 x = math.exp(1000000000)
843 except OverflowError:
844 pass
845 else:
846 self.fail("overflowing exp() didn't trigger OverflowError")
847
848 # If this fails, it could be a puzzle. One odd possibility is that
849 # mathmodule.c's macros are getting confused while comparing
850 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
851 # as a result (and so raising OverflowError instead).
852 try:
853 x = math.sqrt(-1.0)
854 except ValueError:
855 pass
856 else:
857 self.fail("sqrt(-1) didn't raise ValueError")
858
Christian Heimes53876d92008-04-19 00:31:39 +0000859 def test_testfile(self):
860 if not float.__getformat__("double").startswith("IEEE"):
861 return
862 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
863 # Skip if either the input or result is complex, or if
864 # flags is nonempty
865 if ai != 0. or ei != 0. or flags:
866 continue
867 if fn in ['rect', 'polar']:
868 # no real versions of rect, polar
869 continue
870 func = getattr(math, fn)
Christian Heimesa342c012008-04-20 21:01:16 +0000871 try:
872 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +0000873 except ValueError as exc:
874 message = (("Unexpected ValueError: %s\n " +
875 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +0000876 self.fail(message)
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000877 except OverflowError:
878 message = ("Unexpected OverflowError in " +
879 "test %s:%s(%r)\n" % (id, fn, ar))
880 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +0000881 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000882
883def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +0000884 from doctest import DocFileSuite
885 suite = unittest.TestSuite()
886 suite.addTest(unittest.makeSuite(MathTests))
887 suite.addTest(DocFileSuite("ieee754.txt"))
888 run_unittest(suite)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000889
890if __name__ == '__main__':
891 test_main()