blob: d74f01fb708f9b4dd04203e0d7b43026f7dc247e [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
Mark Dickinson5c567082009-04-24 16:39:07 +000016# detect evidence of double-rounding: fsum is not always correctly
17# rounded on machines that suffer from double rounding.
18x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
19HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
20
Christian Heimes53876d92008-04-19 00:31:39 +000021# locate file with test values
22if __name__ == '__main__':
23 file = sys.argv[0]
24else:
25 file = __file__
26test_dir = os.path.dirname(file) or os.curdir
27test_file = os.path.join(test_dir, 'cmath_testcases.txt')
28
29def parse_testfile(fname):
30 """Parse a file with test values
31
32 Empty lines or lines starting with -- are ignored
33 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
34 """
35 with open(fname) as fp:
36 for line in fp:
37 # skip comment lines and blank lines
38 if line.startswith('--') or not line.strip():
39 continue
40
41 lhs, rhs = line.split('->')
42 id, fn, arg_real, arg_imag = lhs.split()
43 rhs_pieces = rhs.split()
44 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
45 flags = rhs_pieces[2:]
46
47 yield (id, fn,
48 float(arg_real), float(arg_imag),
49 float(exp_real), float(exp_imag),
50 flags
51 )
Guido van Rossumfcce6301996-08-08 18:26:25 +000052
Thomas Wouters89f507f2006-12-13 04:49:30 +000053class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000054
Thomas Wouters89f507f2006-12-13 04:49:30 +000055 def ftest(self, name, value, expected):
56 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +000057 # Use %r instead of %f so the error message
58 # displays full precision. Otherwise discrepancies
59 # in the last few bits will lead to very confusing
60 # error messages
61 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +000062 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000063
Thomas Wouters89f507f2006-12-13 04:49:30 +000064 def testConstants(self):
65 self.ftest('pi', math.pi, 3.1415926)
66 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000067
Thomas Wouters89f507f2006-12-13 04:49:30 +000068 def testAcos(self):
69 self.assertRaises(TypeError, math.acos)
70 self.ftest('acos(-1)', math.acos(-1), math.pi)
71 self.ftest('acos(0)', math.acos(0), math.pi/2)
72 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +000073 self.assertRaises(ValueError, math.acos, INF)
74 self.assertRaises(ValueError, math.acos, NINF)
75 self.assert_(math.isnan(math.acos(NAN)))
76
77 def testAcosh(self):
78 self.assertRaises(TypeError, math.acosh)
79 self.ftest('acosh(1)', math.acosh(1), 0)
80 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
81 self.assertRaises(ValueError, math.acosh, 0)
82 self.assertRaises(ValueError, math.acosh, -1)
83 self.assertEquals(math.acosh(INF), INF)
84 self.assertRaises(ValueError, math.acosh, NINF)
85 self.assert_(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000086
Thomas Wouters89f507f2006-12-13 04:49:30 +000087 def testAsin(self):
88 self.assertRaises(TypeError, math.asin)
89 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
90 self.ftest('asin(0)', math.asin(0), 0)
91 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +000092 self.assertRaises(ValueError, math.asin, INF)
93 self.assertRaises(ValueError, math.asin, NINF)
94 self.assert_(math.isnan(math.asin(NAN)))
95
96 def testAsinh(self):
97 self.assertRaises(TypeError, math.asinh)
98 self.ftest('asinh(0)', math.asinh(0), 0)
99 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
100 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
101 self.assertEquals(math.asinh(INF), INF)
102 self.assertEquals(math.asinh(NINF), NINF)
103 self.assert_(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000104
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 def testAtan(self):
106 self.assertRaises(TypeError, math.atan)
107 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
108 self.ftest('atan(0)', math.atan(0), 0)
109 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes53876d92008-04-19 00:31:39 +0000110 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Christian Heimesa342c012008-04-20 21:01:16 +0000111 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000112 self.assert_(math.isnan(math.atan(NAN)))
113
114 def testAtanh(self):
115 self.assertRaises(TypeError, math.atan)
116 self.ftest('atanh(0)', math.atanh(0), 0)
117 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
118 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
119 self.assertRaises(ValueError, math.atanh, 1)
120 self.assertRaises(ValueError, math.atanh, -1)
121 self.assertRaises(ValueError, math.atanh, INF)
122 self.assertRaises(ValueError, math.atanh, NINF)
123 self.assert_(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000124
Thomas Wouters89f507f2006-12-13 04:49:30 +0000125 def testAtan2(self):
126 self.assertRaises(TypeError, math.atan2)
127 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
128 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
129 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
130 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
131 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000132
Christian Heimese57950f2008-04-21 13:08:03 +0000133 # math.atan2(0, x)
134 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
135 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
136 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
137 self.assertEqual(math.atan2(0., 0.), 0.)
138 self.assertEqual(math.atan2(0., 2.3), 0.)
139 self.assertEqual(math.atan2(0., INF), 0.)
140 self.assert_(math.isnan(math.atan2(0., NAN)))
141 # math.atan2(-0, x)
142 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
143 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
144 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
145 self.assertEqual(math.atan2(-0., 0.), -0.)
146 self.assertEqual(math.atan2(-0., 2.3), -0.)
147 self.assertEqual(math.atan2(-0., INF), -0.)
148 self.assert_(math.isnan(math.atan2(-0., NAN)))
149 # math.atan2(INF, x)
150 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
151 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
152 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
153 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
154 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
155 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
156 self.assert_(math.isnan(math.atan2(INF, NAN)))
157 # math.atan2(NINF, x)
158 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
159 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
160 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
161 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
162 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
163 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
164 self.assert_(math.isnan(math.atan2(NINF, 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(-finite, x)
172 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
173 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
174 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
175 self.assertEqual(math.atan2(-2.3, INF), -0.)
176 self.assert_(math.isnan(math.atan2(-2.3, NAN)))
177 # math.atan2(NAN, x)
178 self.assert_(math.isnan(math.atan2(NAN, NINF)))
179 self.assert_(math.isnan(math.atan2(NAN, -2.3)))
180 self.assert_(math.isnan(math.atan2(NAN, -0.)))
181 self.assert_(math.isnan(math.atan2(NAN, 0.)))
182 self.assert_(math.isnan(math.atan2(NAN, 2.3)))
183 self.assert_(math.isnan(math.atan2(NAN, INF)))
184 self.assert_(math.isnan(math.atan2(NAN, NAN)))
185
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 def testCeil(self):
187 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000188 self.assertEquals(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
190 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
191 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
192 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
193 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
194 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000195 #self.assertEquals(math.ceil(INF), INF)
196 #self.assertEquals(math.ceil(NINF), NINF)
197 #self.assert_(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000198
Guido van Rossum13e05de2007-08-23 22:56:55 +0000199 class TestCeil:
200 def __ceil__(self):
201 return 42
202 class TestNoCeil:
203 pass
204 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
205 self.assertRaises(TypeError, math.ceil, TestNoCeil())
206
207 t = TestNoCeil()
208 t.__ceil__ = lambda *args: args
209 self.assertRaises(TypeError, math.ceil, t)
210 self.assertRaises(TypeError, math.ceil, t, 0)
211
Christian Heimes53876d92008-04-19 00:31:39 +0000212 if float.__getformat__("double").startswith("IEEE"):
213 def testCopysign(self):
214 self.assertRaises(TypeError, math.copysign)
215 # copysign should let us distinguish signs of zeros
216 self.assertEquals(copysign(1., 0.), 1.)
217 self.assertEquals(copysign(1., -0.), -1.)
218 self.assertEquals(copysign(INF, 0.), INF)
219 self.assertEquals(copysign(INF, -0.), NINF)
220 self.assertEquals(copysign(NINF, 0.), INF)
221 self.assertEquals(copysign(NINF, -0.), NINF)
222 # and of infinities
223 self.assertEquals(copysign(1., INF), 1.)
224 self.assertEquals(copysign(1., NINF), -1.)
225 self.assertEquals(copysign(INF, INF), INF)
226 self.assertEquals(copysign(INF, NINF), NINF)
227 self.assertEquals(copysign(NINF, INF), INF)
228 self.assertEquals(copysign(NINF, NINF), NINF)
229 self.assert_(math.isnan(copysign(NAN, 1.)))
230 self.assert_(math.isnan(copysign(NAN, INF)))
231 self.assert_(math.isnan(copysign(NAN, NINF)))
232 self.assert_(math.isnan(copysign(NAN, NAN)))
233 # copysign(INF, NAN) may be INF or it may be NINF, since
234 # we don't know whether the sign bit of NAN is set on any
235 # given platform.
236 self.assert_(math.isinf(copysign(INF, NAN)))
237 # similarly, copysign(2., NAN) could be 2. or -2.
238 self.assertEquals(abs(copysign(2., NAN)), 2.)
239
Thomas Wouters89f507f2006-12-13 04:49:30 +0000240 def testCos(self):
241 self.assertRaises(TypeError, math.cos)
242 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
243 self.ftest('cos(0)', math.cos(0), 1)
244 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
245 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000246 try:
247 self.assert_(math.isnan(math.cos(INF)))
248 self.assert_(math.isnan(math.cos(NINF)))
249 except ValueError:
250 self.assertRaises(ValueError, math.cos, INF)
251 self.assertRaises(ValueError, math.cos, NINF)
252 self.assert_(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000253
Thomas Wouters89f507f2006-12-13 04:49:30 +0000254 def testCosh(self):
255 self.assertRaises(TypeError, math.cosh)
256 self.ftest('cosh(0)', math.cosh(0), 1)
257 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 +0000258 self.assertEquals(math.cosh(INF), INF)
259 self.assertEquals(math.cosh(NINF), INF)
260 self.assert_(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000261
Thomas Wouters89f507f2006-12-13 04:49:30 +0000262 def testDegrees(self):
263 self.assertRaises(TypeError, math.degrees)
264 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
265 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
266 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000267
Thomas Wouters89f507f2006-12-13 04:49:30 +0000268 def testExp(self):
269 self.assertRaises(TypeError, math.exp)
270 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
271 self.ftest('exp(0)', math.exp(0), 1)
272 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes53876d92008-04-19 00:31:39 +0000273 self.assertEquals(math.exp(INF), INF)
274 self.assertEquals(math.exp(NINF), 0.)
275 self.assert_(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000276
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277 def testFabs(self):
278 self.assertRaises(TypeError, math.fabs)
279 self.ftest('fabs(-1)', math.fabs(-1), 1)
280 self.ftest('fabs(0)', math.fabs(0), 0)
281 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000282
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000283 def testFactorial(self):
284 def fact(n):
285 result = 1
286 for i in range(1, int(n)+1):
287 result *= i
288 return result
289 values = list(range(10)) + [50, 100, 500]
290 random.shuffle(values)
291 for x in range(10):
292 for cast in (int, float):
293 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
294 self.assertRaises(ValueError, math.factorial, -1)
295 self.assertRaises(ValueError, math.factorial, math.pi)
296
Thomas Wouters89f507f2006-12-13 04:49:30 +0000297 def testFloor(self):
298 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000299 self.assertEquals(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000300 self.ftest('floor(0.5)', math.floor(0.5), 0)
301 self.ftest('floor(1.0)', math.floor(1.0), 1)
302 self.ftest('floor(1.5)', math.floor(1.5), 1)
303 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
304 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
305 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000306 # pow() relies on floor() to check for integers
307 # This fails on some platforms - so check it here
308 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
309 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes53876d92008-04-19 00:31:39 +0000310 #self.assertEquals(math.ceil(INF), INF)
311 #self.assertEquals(math.ceil(NINF), NINF)
312 #self.assert_(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000313
Guido van Rossum13e05de2007-08-23 22:56:55 +0000314 class TestFloor:
315 def __floor__(self):
316 return 42
317 class TestNoFloor:
318 pass
319 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
320 self.assertRaises(TypeError, math.floor, TestNoFloor())
321
322 t = TestNoFloor()
323 t.__floor__ = lambda *args: args
324 self.assertRaises(TypeError, math.floor, t)
325 self.assertRaises(TypeError, math.floor, t, 0)
326
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327 def testFmod(self):
328 self.assertRaises(TypeError, math.fmod)
329 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
330 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
331 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
332 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
333 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
334 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000335 self.assert_(math.isnan(math.fmod(NAN, 1.)))
336 self.assert_(math.isnan(math.fmod(1., NAN)))
337 self.assert_(math.isnan(math.fmod(NAN, NAN)))
338 self.assertRaises(ValueError, math.fmod, 1., 0.)
339 self.assertRaises(ValueError, math.fmod, INF, 1.)
340 self.assertRaises(ValueError, math.fmod, NINF, 1.)
341 self.assertRaises(ValueError, math.fmod, INF, 0.)
342 self.assertEquals(math.fmod(3.0, INF), 3.0)
343 self.assertEquals(math.fmod(-3.0, INF), -3.0)
344 self.assertEquals(math.fmod(3.0, NINF), 3.0)
345 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
346 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
347 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000348
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349 def testFrexp(self):
350 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000351
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000352 def testfrexp(name, result, expected):
353 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 if abs(mant-emant) > eps or exp != eexp:
355 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000356 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000357
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
359 testfrexp('frexp(0)', math.frexp(0), (0, 0))
360 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
361 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000362
Christian Heimes53876d92008-04-19 00:31:39 +0000363 self.assertEquals(math.frexp(INF)[0], INF)
364 self.assertEquals(math.frexp(NINF)[0], NINF)
365 self.assert_(math.isnan(math.frexp(NAN)[0]))
366
Mark Dickinson5c567082009-04-24 16:39:07 +0000367 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
368 "test requires IEEE 754 doubles")
369 @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
370 "fsum is not exact on machines with double rounding")
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000371 def testFsum(self):
372 # math.fsum relies on exact rounding for correct operation.
373 # There's a known problem with IA32 floating-point that causes
374 # inexact rounding in some situations, and will cause the
375 # math.fsum tests below to fail; see issue #2937. On non IEEE
376 # 754 platforms, and on IEEE 754 platforms that exhibit the
377 # problem described in issue #2937, we simply skip the whole
378 # test.
379
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000380 # Python version of math.fsum, for comparison. Uses a
381 # different algorithm based on frexp, ldexp and integer
382 # arithmetic.
383 from sys import float_info
384 mant_dig = float_info.mant_dig
385 etiny = float_info.min_exp - mant_dig
386
387 def msum(iterable):
388 """Full precision summation. Compute sum(iterable) without any
389 intermediate accumulation of error. Based on the 'lsum' function
390 at http://code.activestate.com/recipes/393090/
391
392 """
393 tmant, texp = 0, 0
394 for x in iterable:
395 mant, exp = math.frexp(x)
396 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
397 if texp > exp:
398 tmant <<= texp-exp
399 texp = exp
400 else:
401 mant <<= exp-texp
402 tmant += mant
403 # Round tmant * 2**texp to a float. The original recipe
404 # used float(str(tmant)) * 2.0**texp for this, but that's
405 # a little unsafe because str -> float conversion can't be
406 # relied upon to do correct rounding on all platforms.
407 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
408 if tail > 0:
409 h = 1 << (tail-1)
410 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
411 texp += tail
412 return math.ldexp(tmant, texp)
413
414 test_values = [
415 ([], 0.0),
416 ([0.0], 0.0),
417 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
418 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
419 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
420 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
421 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
422 ([1./n for n in range(1, 1001)],
423 float.fromhex('0x1.df11f45f4e61ap+2')),
424 ([(-1.)**n/n for n in range(1, 1001)],
425 float.fromhex('-0x1.62a2af1bd3624p-1')),
426 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
427 ([1e16, 1., 1e-16], 10000000000000002.0),
428 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
429 # exercise code for resizing partials array
430 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
431 [-2.**1022],
432 float.fromhex('0x1.5555555555555p+970')),
433 ]
434
435 for i, (vals, expected) in enumerate(test_values):
436 try:
437 actual = math.fsum(vals)
438 except OverflowError:
439 self.fail("test %d failed: got OverflowError, expected %r "
440 "for math.fsum(%.100r)" % (i, expected, vals))
441 except ValueError:
442 self.fail("test %d failed: got ValueError, expected %r "
443 "for math.fsum(%.100r)" % (i, expected, vals))
444 self.assertEqual(actual, expected)
445
446 from random import random, gauss, shuffle
447 for j in range(1000):
448 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
449 s = 0
450 for i in range(200):
451 v = gauss(0, random()) ** 7 - s
452 s += v
453 vals.append(v)
454 shuffle(vals)
455
456 s = msum(vals)
457 self.assertEqual(msum(vals), math.fsum(vals))
458
Thomas Wouters89f507f2006-12-13 04:49:30 +0000459 def testHypot(self):
460 self.assertRaises(TypeError, math.hypot)
461 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
462 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000463 self.assertEqual(math.hypot(NAN, INF), INF)
464 self.assertEqual(math.hypot(INF, NAN), INF)
465 self.assertEqual(math.hypot(NAN, NINF), INF)
466 self.assertEqual(math.hypot(NINF, NAN), INF)
467 self.assert_(math.isnan(math.hypot(1.0, NAN)))
468 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000469
Thomas Wouters89f507f2006-12-13 04:49:30 +0000470 def testLdexp(self):
471 self.assertRaises(TypeError, math.ldexp)
472 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
473 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
474 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
475 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000476 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
477 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
478 self.assertEquals(math.ldexp(1., -1000000), 0.)
479 self.assertEquals(math.ldexp(-1., -1000000), -0.)
480 self.assertEquals(math.ldexp(INF, 30), INF)
481 self.assertEquals(math.ldexp(NINF, -213), NINF)
482 self.assert_(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000483
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000484 # large second argument
485 for n in [10**5, 10**10, 10**20, 10**40]:
486 self.assertEquals(math.ldexp(INF, -n), INF)
487 self.assertEquals(math.ldexp(NINF, -n), NINF)
488 self.assertEquals(math.ldexp(1., -n), 0.)
489 self.assertEquals(math.ldexp(-1., -n), -0.)
490 self.assertEquals(math.ldexp(0., -n), 0.)
491 self.assertEquals(math.ldexp(-0., -n), -0.)
492 self.assert_(math.isnan(math.ldexp(NAN, -n)))
493
494 self.assertRaises(OverflowError, math.ldexp, 1., n)
495 self.assertRaises(OverflowError, math.ldexp, -1., n)
496 self.assertEquals(math.ldexp(0., n), 0.)
497 self.assertEquals(math.ldexp(-0., n), -0.)
498 self.assertEquals(math.ldexp(INF, n), INF)
499 self.assertEquals(math.ldexp(NINF, n), NINF)
500 self.assert_(math.isnan(math.ldexp(NAN, n)))
501
Thomas Wouters89f507f2006-12-13 04:49:30 +0000502 def testLog(self):
503 self.assertRaises(TypeError, math.log)
504 self.ftest('log(1/e)', math.log(1/math.e), -1)
505 self.ftest('log(1)', math.log(1), 0)
506 self.ftest('log(e)', math.log(math.e), 1)
507 self.ftest('log(32,2)', math.log(32,2), 5)
508 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
509 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000510 self.assertEquals(math.log(INF), INF)
511 self.assertRaises(ValueError, math.log, NINF)
512 self.assert_(math.isnan(math.log(NAN)))
513
514 def testLog1p(self):
515 self.assertRaises(TypeError, math.log1p)
516 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
517 self.ftest('log1p(0)', math.log1p(0), 0)
518 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
519 self.ftest('log1p(1)', math.log1p(1), math.log(2))
520 self.assertEquals(math.log1p(INF), INF)
521 self.assertRaises(ValueError, math.log1p, NINF)
522 self.assert_(math.isnan(math.log1p(NAN)))
523 n= 2**90
524 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
525 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000526
Thomas Wouters89f507f2006-12-13 04:49:30 +0000527 def testLog10(self):
528 self.assertRaises(TypeError, math.log10)
529 self.ftest('log10(0.1)', math.log10(0.1), -1)
530 self.ftest('log10(1)', math.log10(1), 0)
531 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes53876d92008-04-19 00:31:39 +0000532 self.assertEquals(math.log(INF), INF)
533 self.assertRaises(ValueError, math.log10, NINF)
534 self.assert_(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000535
Thomas Wouters89f507f2006-12-13 04:49:30 +0000536 def testModf(self):
537 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000538
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000539 def testmodf(name, result, expected):
540 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000541 if abs(v1-e1) > eps or abs(v2-e2):
542 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000543 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000544
Thomas Wouters89f507f2006-12-13 04:49:30 +0000545 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
546 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000547
Christian Heimes53876d92008-04-19 00:31:39 +0000548 self.assertEquals(math.modf(INF), (0.0, INF))
549 self.assertEquals(math.modf(NINF), (-0.0, NINF))
550
551 modf_nan = math.modf(NAN)
552 self.assert_(math.isnan(modf_nan[0]))
553 self.assert_(math.isnan(modf_nan[1]))
554
Thomas Wouters89f507f2006-12-13 04:49:30 +0000555 def testPow(self):
556 self.assertRaises(TypeError, math.pow)
557 self.ftest('pow(0,1)', math.pow(0,1), 0)
558 self.ftest('pow(1,0)', math.pow(1,0), 1)
559 self.ftest('pow(2,1)', math.pow(2,1), 2)
560 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000561 self.assertEqual(math.pow(INF, 1), INF)
562 self.assertEqual(math.pow(NINF, 1), NINF)
563 self.assertEqual((math.pow(1, INF)), 1.)
564 self.assertEqual((math.pow(1, NINF)), 1.)
565 self.assert_(math.isnan(math.pow(NAN, 1)))
566 self.assert_(math.isnan(math.pow(2, NAN)))
567 self.assert_(math.isnan(math.pow(0, NAN)))
568 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000569
570 # pow(0., x)
571 self.assertEqual(math.pow(0., INF), 0.)
572 self.assertEqual(math.pow(0., 3.), 0.)
573 self.assertEqual(math.pow(0., 2.3), 0.)
574 self.assertEqual(math.pow(0., 2.), 0.)
575 self.assertEqual(math.pow(0., 0.), 1.)
576 self.assertEqual(math.pow(0., -0.), 1.)
577 self.assertRaises(ValueError, math.pow, 0., -2.)
578 self.assertRaises(ValueError, math.pow, 0., -2.3)
579 self.assertRaises(ValueError, math.pow, 0., -3.)
580 self.assertRaises(ValueError, math.pow, 0., NINF)
581 self.assert_(math.isnan(math.pow(0., NAN)))
582
583 # pow(INF, x)
584 self.assertEqual(math.pow(INF, INF), INF)
585 self.assertEqual(math.pow(INF, 3.), INF)
586 self.assertEqual(math.pow(INF, 2.3), INF)
587 self.assertEqual(math.pow(INF, 2.), INF)
588 self.assertEqual(math.pow(INF, 0.), 1.)
589 self.assertEqual(math.pow(INF, -0.), 1.)
590 self.assertEqual(math.pow(INF, -2.), 0.)
591 self.assertEqual(math.pow(INF, -2.3), 0.)
592 self.assertEqual(math.pow(INF, -3.), 0.)
593 self.assertEqual(math.pow(INF, NINF), 0.)
594 self.assert_(math.isnan(math.pow(INF, NAN)))
595
596 # pow(-0., x)
597 self.assertEqual(math.pow(-0., INF), 0.)
598 self.assertEqual(math.pow(-0., 3.), -0.)
599 self.assertEqual(math.pow(-0., 2.3), 0.)
600 self.assertEqual(math.pow(-0., 2.), 0.)
601 self.assertEqual(math.pow(-0., 0.), 1.)
602 self.assertEqual(math.pow(-0., -0.), 1.)
603 self.assertRaises(ValueError, math.pow, -0., -2.)
604 self.assertRaises(ValueError, math.pow, -0., -2.3)
605 self.assertRaises(ValueError, math.pow, -0., -3.)
606 self.assertRaises(ValueError, math.pow, -0., NINF)
607 self.assert_(math.isnan(math.pow(-0., NAN)))
608
609 # pow(NINF, x)
610 self.assertEqual(math.pow(NINF, INF), INF)
611 self.assertEqual(math.pow(NINF, 3.), NINF)
612 self.assertEqual(math.pow(NINF, 2.3), INF)
613 self.assertEqual(math.pow(NINF, 2.), INF)
614 self.assertEqual(math.pow(NINF, 0.), 1.)
615 self.assertEqual(math.pow(NINF, -0.), 1.)
616 self.assertEqual(math.pow(NINF, -2.), 0.)
617 self.assertEqual(math.pow(NINF, -2.3), 0.)
618 self.assertEqual(math.pow(NINF, -3.), -0.)
619 self.assertEqual(math.pow(NINF, NINF), 0.)
620 self.assert_(math.isnan(math.pow(NINF, NAN)))
621
622 # pow(-1, x)
623 self.assertEqual(math.pow(-1., INF), 1.)
624 self.assertEqual(math.pow(-1., 3.), -1.)
625 self.assertRaises(ValueError, math.pow, -1., 2.3)
626 self.assertEqual(math.pow(-1., 2.), 1.)
627 self.assertEqual(math.pow(-1., 0.), 1.)
628 self.assertEqual(math.pow(-1., -0.), 1.)
629 self.assertEqual(math.pow(-1., -2.), 1.)
630 self.assertRaises(ValueError, math.pow, -1., -2.3)
631 self.assertEqual(math.pow(-1., -3.), -1.)
632 self.assertEqual(math.pow(-1., NINF), 1.)
633 self.assert_(math.isnan(math.pow(-1., NAN)))
634
635 # pow(1, x)
636 self.assertEqual(math.pow(1., INF), 1.)
637 self.assertEqual(math.pow(1., 3.), 1.)
638 self.assertEqual(math.pow(1., 2.3), 1.)
639 self.assertEqual(math.pow(1., 2.), 1.)
640 self.assertEqual(math.pow(1., 0.), 1.)
641 self.assertEqual(math.pow(1., -0.), 1.)
642 self.assertEqual(math.pow(1., -2.), 1.)
643 self.assertEqual(math.pow(1., -2.3), 1.)
644 self.assertEqual(math.pow(1., -3.), 1.)
645 self.assertEqual(math.pow(1., NINF), 1.)
646 self.assertEqual(math.pow(1., NAN), 1.)
647
648 # pow(x, 0) should be 1 for any x
649 self.assertEqual(math.pow(2.3, 0.), 1.)
650 self.assertEqual(math.pow(-2.3, 0.), 1.)
651 self.assertEqual(math.pow(NAN, 0.), 1.)
652 self.assertEqual(math.pow(2.3, -0.), 1.)
653 self.assertEqual(math.pow(-2.3, -0.), 1.)
654 self.assertEqual(math.pow(NAN, -0.), 1.)
655
656 # pow(x, y) is invalid if x is negative and y is not integral
657 self.assertRaises(ValueError, math.pow, -1., 2.3)
658 self.assertRaises(ValueError, math.pow, -15., -3.1)
659
660 # pow(x, NINF)
661 self.assertEqual(math.pow(1.9, NINF), 0.)
662 self.assertEqual(math.pow(1.1, NINF), 0.)
663 self.assertEqual(math.pow(0.9, NINF), INF)
664 self.assertEqual(math.pow(0.1, NINF), INF)
665 self.assertEqual(math.pow(-0.1, NINF), INF)
666 self.assertEqual(math.pow(-0.9, NINF), INF)
667 self.assertEqual(math.pow(-1.1, NINF), 0.)
668 self.assertEqual(math.pow(-1.9, NINF), 0.)
669
670 # pow(x, INF)
671 self.assertEqual(math.pow(1.9, INF), INF)
672 self.assertEqual(math.pow(1.1, INF), INF)
673 self.assertEqual(math.pow(0.9, INF), 0.)
674 self.assertEqual(math.pow(0.1, INF), 0.)
675 self.assertEqual(math.pow(-0.1, INF), 0.)
676 self.assertEqual(math.pow(-0.9, INF), 0.)
677 self.assertEqual(math.pow(-1.1, INF), INF)
678 self.assertEqual(math.pow(-1.9, INF), INF)
679
680 # pow(x, y) should work for x negative, y an integer
681 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
682 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
683 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
684 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
685 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
686 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
687 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
688 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
689 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
690 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
691
692 # the following tests have been commented out since they don't
693 # really belong here: the implementation of ** for floats is
694 # independent of the implemention of math.pow
695 #self.assertEqual(1**NAN, 1)
696 #self.assertEqual(1**INF, 1)
697 #self.assertEqual(1**NINF, 1)
698 #self.assertEqual(1**0, 1)
699 #self.assertEqual(1.**NAN, 1)
700 #self.assertEqual(1.**INF, 1)
701 #self.assertEqual(1.**NINF, 1)
702 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000703
Thomas Wouters89f507f2006-12-13 04:49:30 +0000704 def testRadians(self):
705 self.assertRaises(TypeError, math.radians)
706 self.ftest('radians(180)', math.radians(180), math.pi)
707 self.ftest('radians(90)', math.radians(90), math.pi/2)
708 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000709
Thomas Wouters89f507f2006-12-13 04:49:30 +0000710 def testSin(self):
711 self.assertRaises(TypeError, math.sin)
712 self.ftest('sin(0)', math.sin(0), 0)
713 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
714 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000715 try:
716 self.assert_(math.isnan(math.sin(INF)))
717 self.assert_(math.isnan(math.sin(NINF)))
718 except ValueError:
719 self.assertRaises(ValueError, math.sin, INF)
720 self.assertRaises(ValueError, math.sin, NINF)
721 self.assert_(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000722
Thomas Wouters89f507f2006-12-13 04:49:30 +0000723 def testSinh(self):
724 self.assertRaises(TypeError, math.sinh)
725 self.ftest('sinh(0)', math.sinh(0), 0)
726 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
727 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000728 self.assertEquals(math.sinh(INF), INF)
Christian Heimesa342c012008-04-20 21:01:16 +0000729 self.assertEquals(math.sinh(NINF), NINF)
Christian Heimes53876d92008-04-19 00:31:39 +0000730 self.assert_(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000731
Thomas Wouters89f507f2006-12-13 04:49:30 +0000732 def testSqrt(self):
733 self.assertRaises(TypeError, math.sqrt)
734 self.ftest('sqrt(0)', math.sqrt(0), 0)
735 self.ftest('sqrt(1)', math.sqrt(1), 1)
736 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000737 self.assertEquals(math.sqrt(INF), INF)
738 self.assertRaises(ValueError, math.sqrt, NINF)
739 self.assert_(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000740
Thomas Wouters89f507f2006-12-13 04:49:30 +0000741 def testTan(self):
742 self.assertRaises(TypeError, math.tan)
743 self.ftest('tan(0)', math.tan(0), 0)
744 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
745 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000746 try:
747 self.assert_(math.isnan(math.tan(INF)))
748 self.assert_(math.isnan(math.tan(NINF)))
749 except:
750 self.assertRaises(ValueError, math.tan, INF)
751 self.assertRaises(ValueError, math.tan, NINF)
752 self.assert_(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000753
Thomas Wouters89f507f2006-12-13 04:49:30 +0000754 def testTanh(self):
755 self.assertRaises(TypeError, math.tanh)
756 self.ftest('tanh(0)', math.tanh(0), 0)
757 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000758 self.ftest('tanh(inf)', math.tanh(INF), 1)
759 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
760 self.assert_(math.isnan(math.tanh(NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000761 # check that tanh(-0.) == -0. on IEEE 754 systems
762 if float.__getformat__("double").startswith("IEEE"):
763 self.assertEqual(math.tanh(-0.), -0.)
764 self.assertEqual(math.copysign(1., math.tanh(-0.)),
765 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000766
Christian Heimes400adb02008-02-01 08:12:03 +0000767 def test_trunc(self):
768 self.assertEqual(math.trunc(1), 1)
769 self.assertEqual(math.trunc(-1), -1)
770 self.assertEqual(type(math.trunc(1)), int)
771 self.assertEqual(type(math.trunc(1.5)), int)
772 self.assertEqual(math.trunc(1.5), 1)
773 self.assertEqual(math.trunc(-1.5), -1)
774 self.assertEqual(math.trunc(1.999999), 1)
775 self.assertEqual(math.trunc(-1.999999), -1)
776 self.assertEqual(math.trunc(-0.999999), -0)
777 self.assertEqual(math.trunc(-100.999), -100)
778
779 class TestTrunc(object):
780 def __trunc__(self):
781 return 23
782
783 class TestNoTrunc(object):
784 pass
785
786 self.assertEqual(math.trunc(TestTrunc()), 23)
787
788 self.assertRaises(TypeError, math.trunc)
789 self.assertRaises(TypeError, math.trunc, 1, 2)
790 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
791
792 # XXX Doesn't work because the method is looked up on
793 # the type only.
794 #t = TestNoTrunc()
795 #t.__trunc__ = lambda *args: args
796 #self.assertEquals((), math.trunc(t))
797 #self.assertRaises(TypeError, math.trunc, t, 0)
798
Christian Heimes072c0f12008-01-03 23:01:04 +0000799 def testCopysign(self):
800 self.assertEqual(math.copysign(1, 42), 1.0)
801 self.assertEqual(math.copysign(0., 42), 0.0)
802 self.assertEqual(math.copysign(1., -42), -1.0)
803 self.assertEqual(math.copysign(3, 0.), 3.0)
804 self.assertEqual(math.copysign(4., -0.), -4.0)
805
806 def testIsnan(self):
807 self.assert_(math.isnan(float("nan")))
808 self.assert_(math.isnan(float("inf")* 0.))
809 self.failIf(math.isnan(float("inf")))
810 self.failIf(math.isnan(0.))
811 self.failIf(math.isnan(1.))
812
813 def testIsinf(self):
814 self.assert_(math.isinf(float("inf")))
815 self.assert_(math.isinf(float("-inf")))
816 self.assert_(math.isinf(1E400))
817 self.assert_(math.isinf(-1E400))
818 self.failIf(math.isinf(float("nan")))
819 self.failIf(math.isinf(0.))
820 self.failIf(math.isinf(1.))
821
Thomas Wouters89f507f2006-12-13 04:49:30 +0000822 # RED_FLAG 16-Oct-2000 Tim
823 # While 2.0 is more consistent about exceptions than previous releases, it
824 # still fails this part of the test on some platforms. For now, we only
825 # *run* test_exceptions() in verbose mode, so that this isn't normally
826 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000827
Thomas Wouters89f507f2006-12-13 04:49:30 +0000828 if verbose:
829 def test_exceptions(self):
830 try:
831 x = math.exp(-1000000000)
832 except:
833 # mathmodule.c is failing to weed out underflows from libm, or
834 # we've got an fp format with huge dynamic range
835 self.fail("underflowing exp() should not have raised "
836 "an exception")
837 if x != 0:
838 self.fail("underflowing exp() should have returned 0")
839
840 # If this fails, probably using a strict IEEE-754 conforming libm, and x
841 # is +Inf afterwards. But Python wants overflows detected by default.
842 try:
843 x = math.exp(1000000000)
844 except OverflowError:
845 pass
846 else:
847 self.fail("overflowing exp() didn't trigger OverflowError")
848
849 # If this fails, it could be a puzzle. One odd possibility is that
850 # mathmodule.c's macros are getting confused while comparing
851 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
852 # as a result (and so raising OverflowError instead).
853 try:
854 x = math.sqrt(-1.0)
855 except ValueError:
856 pass
857 else:
858 self.fail("sqrt(-1) didn't raise ValueError")
859
Christian Heimes53876d92008-04-19 00:31:39 +0000860 def test_testfile(self):
861 if not float.__getformat__("double").startswith("IEEE"):
862 return
863 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
864 # Skip if either the input or result is complex, or if
865 # flags is nonempty
866 if ai != 0. or ei != 0. or flags:
867 continue
868 if fn in ['rect', 'polar']:
869 # no real versions of rect, polar
870 continue
871 func = getattr(math, fn)
Christian Heimesa342c012008-04-20 21:01:16 +0000872 try:
873 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +0000874 except ValueError as exc:
875 message = (("Unexpected ValueError: %s\n " +
876 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +0000877 self.fail(message)
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000878 except OverflowError:
879 message = ("Unexpected OverflowError in " +
880 "test %s:%s(%r)\n" % (id, fn, ar))
881 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +0000882 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000883
884def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +0000885 from doctest import DocFileSuite
886 suite = unittest.TestSuite()
887 suite.addTest(unittest.makeSuite(MathTests))
888 suite.addTest(DocFileSuite("ieee754.txt"))
889 run_unittest(suite)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000890
891if __name__ == '__main__':
892 test_main()