blob: f29bddd8dec5eda484fda35a8b55113f1009375d [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
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000010import struct
Guido van Rossumfcce6301996-08-08 18:26:25 +000011
Christian Heimes53876d92008-04-19 00:31:39 +000012eps = 1E-05
13NAN = float('nan')
14INF = float('inf')
15NINF = float('-inf')
16
Mark Dickinson63566232009-09-18 21:04:19 +000017# decorator for skipping tests on non-IEEE 754 platforms
18requires_IEEE_754 = unittest.skipUnless(
19 float.__getformat__("double").startswith("IEEE"),
20 "test requires IEEE 754 doubles")
21
Mark Dickinson5c567082009-04-24 16:39:07 +000022# detect evidence of double-rounding: fsum is not always correctly
23# rounded on machines that suffer from double rounding.
24x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
25HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
26
Christian Heimes53876d92008-04-19 00:31:39 +000027# locate file with test values
28if __name__ == '__main__':
29 file = sys.argv[0]
30else:
31 file = __file__
32test_dir = os.path.dirname(file) or os.curdir
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000033math_testcases = os.path.join(test_dir, 'math_testcases.txt')
Christian Heimes53876d92008-04-19 00:31:39 +000034test_file = os.path.join(test_dir, 'cmath_testcases.txt')
35
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000036def to_ulps(x):
37 """Convert a non-NaN float x to an integer, in such a way that
38 adjacent floats are converted to adjacent integers. Then
39 abs(ulps(x) - ulps(y)) gives the difference in ulps between two
40 floats.
41
42 The results from this function will only make sense on platforms
43 where C doubles are represented in IEEE 754 binary64 format.
44
45 """
46 n = struct.unpack('q', struct.pack('<d', x))[0]
47 if n < 0:
48 n = ~(n+2**63)
49 return n
50
51
52def parse_mtestfile(fname):
53 """Parse a file with test values
54
55 -- starts a comment
56 blank lines, or lines containing only a comment, are ignored
57 other lines are expected to have the form
58 id fn arg -> expected [flag]*
59
60 """
61 with open(fname) as fp:
62 for line in fp:
63 # strip comments, and skip blank lines
64 if '--' in line:
65 line = line[:line.index('--')]
66 if not line.strip():
67 continue
68
69 lhs, rhs = line.split('->')
70 id, fn, arg = lhs.split()
71 rhs_pieces = rhs.split()
72 exp = rhs_pieces[0]
73 flags = rhs_pieces[1:]
74
75 yield (id, fn, float(arg), float(exp), flags)
76
Christian Heimes53876d92008-04-19 00:31:39 +000077def parse_testfile(fname):
78 """Parse a file with test values
79
80 Empty lines or lines starting with -- are ignored
81 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
82 """
83 with open(fname) as fp:
84 for line in fp:
85 # skip comment lines and blank lines
86 if line.startswith('--') or not line.strip():
87 continue
88
89 lhs, rhs = line.split('->')
90 id, fn, arg_real, arg_imag = lhs.split()
91 rhs_pieces = rhs.split()
92 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
93 flags = rhs_pieces[2:]
94
95 yield (id, fn,
96 float(arg_real), float(arg_imag),
97 float(exp_real), float(exp_imag),
98 flags
99 )
Guido van Rossumfcce6301996-08-08 18:26:25 +0000100
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +0000102
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103 def ftest(self, name, value, expected):
104 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +0000105 # Use %r instead of %f so the error message
106 # displays full precision. Otherwise discrepancies
107 # in the last few bits will lead to very confusing
108 # error messages
109 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000111
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 def testConstants(self):
113 self.ftest('pi', math.pi, 3.1415926)
114 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000115
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116 def testAcos(self):
117 self.assertRaises(TypeError, math.acos)
118 self.ftest('acos(-1)', math.acos(-1), math.pi)
119 self.ftest('acos(0)', math.acos(0), math.pi/2)
120 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000121 self.assertRaises(ValueError, math.acos, INF)
122 self.assertRaises(ValueError, math.acos, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000123 self.assertTrue(math.isnan(math.acos(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000124
125 def testAcosh(self):
126 self.assertRaises(TypeError, math.acosh)
127 self.ftest('acosh(1)', math.acosh(1), 0)
128 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
129 self.assertRaises(ValueError, math.acosh, 0)
130 self.assertRaises(ValueError, math.acosh, -1)
131 self.assertEquals(math.acosh(INF), INF)
132 self.assertRaises(ValueError, math.acosh, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000133 self.assertTrue(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000134
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135 def testAsin(self):
136 self.assertRaises(TypeError, math.asin)
137 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
138 self.ftest('asin(0)', math.asin(0), 0)
139 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000140 self.assertRaises(ValueError, math.asin, INF)
141 self.assertRaises(ValueError, math.asin, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000142 self.assertTrue(math.isnan(math.asin(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000143
144 def testAsinh(self):
145 self.assertRaises(TypeError, math.asinh)
146 self.ftest('asinh(0)', math.asinh(0), 0)
147 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
148 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
149 self.assertEquals(math.asinh(INF), INF)
150 self.assertEquals(math.asinh(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000151 self.assertTrue(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000152
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153 def testAtan(self):
154 self.assertRaises(TypeError, math.atan)
155 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
156 self.ftest('atan(0)', math.atan(0), 0)
157 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes53876d92008-04-19 00:31:39 +0000158 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Christian Heimesa342c012008-04-20 21:01:16 +0000159 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000160 self.assertTrue(math.isnan(math.atan(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000161
162 def testAtanh(self):
163 self.assertRaises(TypeError, math.atan)
164 self.ftest('atanh(0)', math.atanh(0), 0)
165 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
166 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
167 self.assertRaises(ValueError, math.atanh, 1)
168 self.assertRaises(ValueError, math.atanh, -1)
169 self.assertRaises(ValueError, math.atanh, INF)
170 self.assertRaises(ValueError, math.atanh, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000171 self.assertTrue(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000172
Thomas Wouters89f507f2006-12-13 04:49:30 +0000173 def testAtan2(self):
174 self.assertRaises(TypeError, math.atan2)
175 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
176 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
177 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
178 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
179 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000180
Christian Heimese57950f2008-04-21 13:08:03 +0000181 # math.atan2(0, x)
182 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
183 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
184 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
185 self.assertEqual(math.atan2(0., 0.), 0.)
186 self.assertEqual(math.atan2(0., 2.3), 0.)
187 self.assertEqual(math.atan2(0., INF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000188 self.assertTrue(math.isnan(math.atan2(0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000189 # math.atan2(-0, x)
190 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
191 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
192 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
193 self.assertEqual(math.atan2(-0., 0.), -0.)
194 self.assertEqual(math.atan2(-0., 2.3), -0.)
195 self.assertEqual(math.atan2(-0., INF), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000196 self.assertTrue(math.isnan(math.atan2(-0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000197 # math.atan2(INF, x)
198 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
199 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
200 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
201 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
202 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
203 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000204 self.assertTrue(math.isnan(math.atan2(INF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000205 # math.atan2(NINF, x)
206 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
207 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
208 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
209 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
210 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
211 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000212 self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000213 # math.atan2(+finite, x)
214 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
215 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
216 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
217 self.assertEqual(math.atan2(2.3, INF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000218 self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000219 # math.atan2(-finite, x)
220 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
221 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
222 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
223 self.assertEqual(math.atan2(-2.3, INF), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000224 self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000225 # math.atan2(NAN, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000226 self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
227 self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
228 self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
229 self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
230 self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
231 self.assertTrue(math.isnan(math.atan2(NAN, INF)))
232 self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000233
Thomas Wouters89f507f2006-12-13 04:49:30 +0000234 def testCeil(self):
235 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000236 self.assertEquals(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000237 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
238 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
239 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
240 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
241 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
242 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000243 #self.assertEquals(math.ceil(INF), INF)
244 #self.assertEquals(math.ceil(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000245 #self.assertTrue(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000246
Guido van Rossum13e05de2007-08-23 22:56:55 +0000247 class TestCeil:
248 def __ceil__(self):
249 return 42
250 class TestNoCeil:
251 pass
252 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
253 self.assertRaises(TypeError, math.ceil, TestNoCeil())
254
255 t = TestNoCeil()
256 t.__ceil__ = lambda *args: args
257 self.assertRaises(TypeError, math.ceil, t)
258 self.assertRaises(TypeError, math.ceil, t, 0)
259
Mark Dickinson63566232009-09-18 21:04:19 +0000260 @requires_IEEE_754
261 def testCopysign(self):
262 self.assertRaises(TypeError, math.copysign)
263 # copysign should let us distinguish signs of zeros
264 self.assertEquals(copysign(1., 0.), 1.)
265 self.assertEquals(copysign(1., -0.), -1.)
266 self.assertEquals(copysign(INF, 0.), INF)
267 self.assertEquals(copysign(INF, -0.), NINF)
268 self.assertEquals(copysign(NINF, 0.), INF)
269 self.assertEquals(copysign(NINF, -0.), NINF)
270 # and of infinities
271 self.assertEquals(copysign(1., INF), 1.)
272 self.assertEquals(copysign(1., NINF), -1.)
273 self.assertEquals(copysign(INF, INF), INF)
274 self.assertEquals(copysign(INF, NINF), NINF)
275 self.assertEquals(copysign(NINF, INF), INF)
276 self.assertEquals(copysign(NINF, NINF), NINF)
277 self.assertTrue(math.isnan(copysign(NAN, 1.)))
278 self.assertTrue(math.isnan(copysign(NAN, INF)))
279 self.assertTrue(math.isnan(copysign(NAN, NINF)))
280 self.assertTrue(math.isnan(copysign(NAN, NAN)))
281 # copysign(INF, NAN) may be INF or it may be NINF, since
282 # we don't know whether the sign bit of NAN is set on any
283 # given platform.
284 self.assertTrue(math.isinf(copysign(INF, NAN)))
285 # similarly, copysign(2., NAN) could be 2. or -2.
286 self.assertEquals(abs(copysign(2., NAN)), 2.)
Christian Heimes53876d92008-04-19 00:31:39 +0000287
Thomas Wouters89f507f2006-12-13 04:49:30 +0000288 def testCos(self):
289 self.assertRaises(TypeError, math.cos)
290 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
291 self.ftest('cos(0)', math.cos(0), 1)
292 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
293 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000294 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000295 self.assertTrue(math.isnan(math.cos(INF)))
296 self.assertTrue(math.isnan(math.cos(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000297 except ValueError:
298 self.assertRaises(ValueError, math.cos, INF)
299 self.assertRaises(ValueError, math.cos, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000300 self.assertTrue(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000301
Thomas Wouters89f507f2006-12-13 04:49:30 +0000302 def testCosh(self):
303 self.assertRaises(TypeError, math.cosh)
304 self.ftest('cosh(0)', math.cosh(0), 1)
305 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 +0000306 self.assertEquals(math.cosh(INF), INF)
307 self.assertEquals(math.cosh(NINF), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000308 self.assertTrue(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000309
Thomas Wouters89f507f2006-12-13 04:49:30 +0000310 def testDegrees(self):
311 self.assertRaises(TypeError, math.degrees)
312 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
313 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
314 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000315
Thomas Wouters89f507f2006-12-13 04:49:30 +0000316 def testExp(self):
317 self.assertRaises(TypeError, math.exp)
318 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
319 self.ftest('exp(0)', math.exp(0), 1)
320 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes53876d92008-04-19 00:31:39 +0000321 self.assertEquals(math.exp(INF), INF)
322 self.assertEquals(math.exp(NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000323 self.assertTrue(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000324
Thomas Wouters89f507f2006-12-13 04:49:30 +0000325 def testFabs(self):
326 self.assertRaises(TypeError, math.fabs)
327 self.ftest('fabs(-1)', math.fabs(-1), 1)
328 self.ftest('fabs(0)', math.fabs(0), 0)
329 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000330
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000331 def testFactorial(self):
332 def fact(n):
333 result = 1
334 for i in range(1, int(n)+1):
335 result *= i
336 return result
337 values = list(range(10)) + [50, 100, 500]
338 random.shuffle(values)
339 for x in range(10):
340 for cast in (int, float):
341 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
342 self.assertRaises(ValueError, math.factorial, -1)
343 self.assertRaises(ValueError, math.factorial, math.pi)
344
Thomas Wouters89f507f2006-12-13 04:49:30 +0000345 def testFloor(self):
346 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000347 self.assertEquals(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000348 self.ftest('floor(0.5)', math.floor(0.5), 0)
349 self.ftest('floor(1.0)', math.floor(1.0), 1)
350 self.ftest('floor(1.5)', math.floor(1.5), 1)
351 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
352 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
353 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000354 # pow() relies on floor() to check for integers
355 # This fails on some platforms - so check it here
356 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
357 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes53876d92008-04-19 00:31:39 +0000358 #self.assertEquals(math.ceil(INF), INF)
359 #self.assertEquals(math.ceil(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000360 #self.assertTrue(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000361
Guido van Rossum13e05de2007-08-23 22:56:55 +0000362 class TestFloor:
363 def __floor__(self):
364 return 42
365 class TestNoFloor:
366 pass
367 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
368 self.assertRaises(TypeError, math.floor, TestNoFloor())
369
370 t = TestNoFloor()
371 t.__floor__ = lambda *args: args
372 self.assertRaises(TypeError, math.floor, t)
373 self.assertRaises(TypeError, math.floor, t, 0)
374
Thomas Wouters89f507f2006-12-13 04:49:30 +0000375 def testFmod(self):
376 self.assertRaises(TypeError, math.fmod)
377 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
378 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
379 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
380 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
381 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
382 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000383 self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
384 self.assertTrue(math.isnan(math.fmod(1., NAN)))
385 self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000386 self.assertRaises(ValueError, math.fmod, 1., 0.)
387 self.assertRaises(ValueError, math.fmod, INF, 1.)
388 self.assertRaises(ValueError, math.fmod, NINF, 1.)
389 self.assertRaises(ValueError, math.fmod, INF, 0.)
390 self.assertEquals(math.fmod(3.0, INF), 3.0)
391 self.assertEquals(math.fmod(-3.0, INF), -3.0)
392 self.assertEquals(math.fmod(3.0, NINF), 3.0)
393 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
394 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
395 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000396
Thomas Wouters89f507f2006-12-13 04:49:30 +0000397 def testFrexp(self):
398 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000399
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000400 def testfrexp(name, result, expected):
401 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000402 if abs(mant-emant) > eps or exp != eexp:
403 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000404 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000405
Thomas Wouters89f507f2006-12-13 04:49:30 +0000406 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
407 testfrexp('frexp(0)', math.frexp(0), (0, 0))
408 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
409 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000410
Christian Heimes53876d92008-04-19 00:31:39 +0000411 self.assertEquals(math.frexp(INF)[0], INF)
412 self.assertEquals(math.frexp(NINF)[0], NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000413 self.assertTrue(math.isnan(math.frexp(NAN)[0]))
Christian Heimes53876d92008-04-19 00:31:39 +0000414
Mark Dickinson63566232009-09-18 21:04:19 +0000415 @requires_IEEE_754
Mark Dickinson5c567082009-04-24 16:39:07 +0000416 @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
417 "fsum is not exact on machines with double rounding")
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000418 def testFsum(self):
419 # math.fsum relies on exact rounding for correct operation.
420 # There's a known problem with IA32 floating-point that causes
421 # inexact rounding in some situations, and will cause the
422 # math.fsum tests below to fail; see issue #2937. On non IEEE
423 # 754 platforms, and on IEEE 754 platforms that exhibit the
424 # problem described in issue #2937, we simply skip the whole
425 # test.
426
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000427 # Python version of math.fsum, for comparison. Uses a
428 # different algorithm based on frexp, ldexp and integer
429 # arithmetic.
430 from sys import float_info
431 mant_dig = float_info.mant_dig
432 etiny = float_info.min_exp - mant_dig
433
434 def msum(iterable):
435 """Full precision summation. Compute sum(iterable) without any
436 intermediate accumulation of error. Based on the 'lsum' function
437 at http://code.activestate.com/recipes/393090/
438
439 """
440 tmant, texp = 0, 0
441 for x in iterable:
442 mant, exp = math.frexp(x)
443 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
444 if texp > exp:
445 tmant <<= texp-exp
446 texp = exp
447 else:
448 mant <<= exp-texp
449 tmant += mant
450 # Round tmant * 2**texp to a float. The original recipe
451 # used float(str(tmant)) * 2.0**texp for this, but that's
452 # a little unsafe because str -> float conversion can't be
453 # relied upon to do correct rounding on all platforms.
454 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
455 if tail > 0:
456 h = 1 << (tail-1)
457 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
458 texp += tail
459 return math.ldexp(tmant, texp)
460
461 test_values = [
462 ([], 0.0),
463 ([0.0], 0.0),
464 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
465 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
466 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
467 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
468 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
469 ([1./n for n in range(1, 1001)],
470 float.fromhex('0x1.df11f45f4e61ap+2')),
471 ([(-1.)**n/n for n in range(1, 1001)],
472 float.fromhex('-0x1.62a2af1bd3624p-1')),
473 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
474 ([1e16, 1., 1e-16], 10000000000000002.0),
475 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
476 # exercise code for resizing partials array
477 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
478 [-2.**1022],
479 float.fromhex('0x1.5555555555555p+970')),
480 ]
481
482 for i, (vals, expected) in enumerate(test_values):
483 try:
484 actual = math.fsum(vals)
485 except OverflowError:
486 self.fail("test %d failed: got OverflowError, expected %r "
487 "for math.fsum(%.100r)" % (i, expected, vals))
488 except ValueError:
489 self.fail("test %d failed: got ValueError, expected %r "
490 "for math.fsum(%.100r)" % (i, expected, vals))
491 self.assertEqual(actual, expected)
492
493 from random import random, gauss, shuffle
494 for j in range(1000):
495 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
496 s = 0
497 for i in range(200):
498 v = gauss(0, random()) ** 7 - s
499 s += v
500 vals.append(v)
501 shuffle(vals)
502
503 s = msum(vals)
504 self.assertEqual(msum(vals), math.fsum(vals))
505
Thomas Wouters89f507f2006-12-13 04:49:30 +0000506 def testHypot(self):
507 self.assertRaises(TypeError, math.hypot)
508 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
509 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000510 self.assertEqual(math.hypot(NAN, INF), INF)
511 self.assertEqual(math.hypot(INF, NAN), INF)
512 self.assertEqual(math.hypot(NAN, NINF), INF)
513 self.assertEqual(math.hypot(NINF, NAN), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000514 self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
515 self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000516
Thomas Wouters89f507f2006-12-13 04:49:30 +0000517 def testLdexp(self):
518 self.assertRaises(TypeError, math.ldexp)
519 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
520 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
521 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
522 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000523 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
524 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
525 self.assertEquals(math.ldexp(1., -1000000), 0.)
526 self.assertEquals(math.ldexp(-1., -1000000), -0.)
527 self.assertEquals(math.ldexp(INF, 30), INF)
528 self.assertEquals(math.ldexp(NINF, -213), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000529 self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000530
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000531 # large second argument
532 for n in [10**5, 10**10, 10**20, 10**40]:
533 self.assertEquals(math.ldexp(INF, -n), INF)
534 self.assertEquals(math.ldexp(NINF, -n), NINF)
535 self.assertEquals(math.ldexp(1., -n), 0.)
536 self.assertEquals(math.ldexp(-1., -n), -0.)
537 self.assertEquals(math.ldexp(0., -n), 0.)
538 self.assertEquals(math.ldexp(-0., -n), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000539 self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000540
541 self.assertRaises(OverflowError, math.ldexp, 1., n)
542 self.assertRaises(OverflowError, math.ldexp, -1., n)
543 self.assertEquals(math.ldexp(0., n), 0.)
544 self.assertEquals(math.ldexp(-0., n), -0.)
545 self.assertEquals(math.ldexp(INF, n), INF)
546 self.assertEquals(math.ldexp(NINF, n), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000547 self.assertTrue(math.isnan(math.ldexp(NAN, n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000548
Thomas Wouters89f507f2006-12-13 04:49:30 +0000549 def testLog(self):
550 self.assertRaises(TypeError, math.log)
551 self.ftest('log(1/e)', math.log(1/math.e), -1)
552 self.ftest('log(1)', math.log(1), 0)
553 self.ftest('log(e)', math.log(math.e), 1)
554 self.ftest('log(32,2)', math.log(32,2), 5)
555 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
556 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000557 self.assertEquals(math.log(INF), INF)
558 self.assertRaises(ValueError, math.log, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000559 self.assertTrue(math.isnan(math.log(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000560
561 def testLog1p(self):
562 self.assertRaises(TypeError, math.log1p)
563 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
564 self.ftest('log1p(0)', math.log1p(0), 0)
565 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
566 self.ftest('log1p(1)', math.log1p(1), math.log(2))
567 self.assertEquals(math.log1p(INF), INF)
568 self.assertRaises(ValueError, math.log1p, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000569 self.assertTrue(math.isnan(math.log1p(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000570 n= 2**90
571 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
572 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000573
Thomas Wouters89f507f2006-12-13 04:49:30 +0000574 def testLog10(self):
575 self.assertRaises(TypeError, math.log10)
576 self.ftest('log10(0.1)', math.log10(0.1), -1)
577 self.ftest('log10(1)', math.log10(1), 0)
578 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes53876d92008-04-19 00:31:39 +0000579 self.assertEquals(math.log(INF), INF)
580 self.assertRaises(ValueError, math.log10, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000581 self.assertTrue(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000582
Thomas Wouters89f507f2006-12-13 04:49:30 +0000583 def testModf(self):
584 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000585
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000586 def testmodf(name, result, expected):
587 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000588 if abs(v1-e1) > eps or abs(v2-e2):
589 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000590 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000591
Thomas Wouters89f507f2006-12-13 04:49:30 +0000592 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
593 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000594
Christian Heimes53876d92008-04-19 00:31:39 +0000595 self.assertEquals(math.modf(INF), (0.0, INF))
596 self.assertEquals(math.modf(NINF), (-0.0, NINF))
597
598 modf_nan = math.modf(NAN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000599 self.assertTrue(math.isnan(modf_nan[0]))
600 self.assertTrue(math.isnan(modf_nan[1]))
Christian Heimes53876d92008-04-19 00:31:39 +0000601
Thomas Wouters89f507f2006-12-13 04:49:30 +0000602 def testPow(self):
603 self.assertRaises(TypeError, math.pow)
604 self.ftest('pow(0,1)', math.pow(0,1), 0)
605 self.ftest('pow(1,0)', math.pow(1,0), 1)
606 self.ftest('pow(2,1)', math.pow(2,1), 2)
607 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000608 self.assertEqual(math.pow(INF, 1), INF)
609 self.assertEqual(math.pow(NINF, 1), NINF)
610 self.assertEqual((math.pow(1, INF)), 1.)
611 self.assertEqual((math.pow(1, NINF)), 1.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000612 self.assertTrue(math.isnan(math.pow(NAN, 1)))
613 self.assertTrue(math.isnan(math.pow(2, NAN)))
614 self.assertTrue(math.isnan(math.pow(0, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000615 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000616
617 # pow(0., x)
618 self.assertEqual(math.pow(0., INF), 0.)
619 self.assertEqual(math.pow(0., 3.), 0.)
620 self.assertEqual(math.pow(0., 2.3), 0.)
621 self.assertEqual(math.pow(0., 2.), 0.)
622 self.assertEqual(math.pow(0., 0.), 1.)
623 self.assertEqual(math.pow(0., -0.), 1.)
624 self.assertRaises(ValueError, math.pow, 0., -2.)
625 self.assertRaises(ValueError, math.pow, 0., -2.3)
626 self.assertRaises(ValueError, math.pow, 0., -3.)
627 self.assertRaises(ValueError, math.pow, 0., NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000628 self.assertTrue(math.isnan(math.pow(0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000629
630 # pow(INF, x)
631 self.assertEqual(math.pow(INF, INF), INF)
632 self.assertEqual(math.pow(INF, 3.), INF)
633 self.assertEqual(math.pow(INF, 2.3), INF)
634 self.assertEqual(math.pow(INF, 2.), INF)
635 self.assertEqual(math.pow(INF, 0.), 1.)
636 self.assertEqual(math.pow(INF, -0.), 1.)
637 self.assertEqual(math.pow(INF, -2.), 0.)
638 self.assertEqual(math.pow(INF, -2.3), 0.)
639 self.assertEqual(math.pow(INF, -3.), 0.)
640 self.assertEqual(math.pow(INF, NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000641 self.assertTrue(math.isnan(math.pow(INF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000642
643 # pow(-0., x)
644 self.assertEqual(math.pow(-0., INF), 0.)
645 self.assertEqual(math.pow(-0., 3.), -0.)
646 self.assertEqual(math.pow(-0., 2.3), 0.)
647 self.assertEqual(math.pow(-0., 2.), 0.)
648 self.assertEqual(math.pow(-0., 0.), 1.)
649 self.assertEqual(math.pow(-0., -0.), 1.)
650 self.assertRaises(ValueError, math.pow, -0., -2.)
651 self.assertRaises(ValueError, math.pow, -0., -2.3)
652 self.assertRaises(ValueError, math.pow, -0., -3.)
653 self.assertRaises(ValueError, math.pow, -0., NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000654 self.assertTrue(math.isnan(math.pow(-0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000655
656 # pow(NINF, x)
657 self.assertEqual(math.pow(NINF, INF), INF)
658 self.assertEqual(math.pow(NINF, 3.), NINF)
659 self.assertEqual(math.pow(NINF, 2.3), INF)
660 self.assertEqual(math.pow(NINF, 2.), INF)
661 self.assertEqual(math.pow(NINF, 0.), 1.)
662 self.assertEqual(math.pow(NINF, -0.), 1.)
663 self.assertEqual(math.pow(NINF, -2.), 0.)
664 self.assertEqual(math.pow(NINF, -2.3), 0.)
665 self.assertEqual(math.pow(NINF, -3.), -0.)
666 self.assertEqual(math.pow(NINF, NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000667 self.assertTrue(math.isnan(math.pow(NINF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000668
669 # pow(-1, x)
670 self.assertEqual(math.pow(-1., INF), 1.)
671 self.assertEqual(math.pow(-1., 3.), -1.)
672 self.assertRaises(ValueError, math.pow, -1., 2.3)
673 self.assertEqual(math.pow(-1., 2.), 1.)
674 self.assertEqual(math.pow(-1., 0.), 1.)
675 self.assertEqual(math.pow(-1., -0.), 1.)
676 self.assertEqual(math.pow(-1., -2.), 1.)
677 self.assertRaises(ValueError, math.pow, -1., -2.3)
678 self.assertEqual(math.pow(-1., -3.), -1.)
679 self.assertEqual(math.pow(-1., NINF), 1.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000680 self.assertTrue(math.isnan(math.pow(-1., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000681
682 # pow(1, x)
683 self.assertEqual(math.pow(1., INF), 1.)
684 self.assertEqual(math.pow(1., 3.), 1.)
685 self.assertEqual(math.pow(1., 2.3), 1.)
686 self.assertEqual(math.pow(1., 2.), 1.)
687 self.assertEqual(math.pow(1., 0.), 1.)
688 self.assertEqual(math.pow(1., -0.), 1.)
689 self.assertEqual(math.pow(1., -2.), 1.)
690 self.assertEqual(math.pow(1., -2.3), 1.)
691 self.assertEqual(math.pow(1., -3.), 1.)
692 self.assertEqual(math.pow(1., NINF), 1.)
693 self.assertEqual(math.pow(1., NAN), 1.)
694
695 # pow(x, 0) should be 1 for any x
696 self.assertEqual(math.pow(2.3, 0.), 1.)
697 self.assertEqual(math.pow(-2.3, 0.), 1.)
698 self.assertEqual(math.pow(NAN, 0.), 1.)
699 self.assertEqual(math.pow(2.3, -0.), 1.)
700 self.assertEqual(math.pow(-2.3, -0.), 1.)
701 self.assertEqual(math.pow(NAN, -0.), 1.)
702
703 # pow(x, y) is invalid if x is negative and y is not integral
704 self.assertRaises(ValueError, math.pow, -1., 2.3)
705 self.assertRaises(ValueError, math.pow, -15., -3.1)
706
707 # pow(x, NINF)
708 self.assertEqual(math.pow(1.9, NINF), 0.)
709 self.assertEqual(math.pow(1.1, NINF), 0.)
710 self.assertEqual(math.pow(0.9, NINF), INF)
711 self.assertEqual(math.pow(0.1, NINF), INF)
712 self.assertEqual(math.pow(-0.1, NINF), INF)
713 self.assertEqual(math.pow(-0.9, NINF), INF)
714 self.assertEqual(math.pow(-1.1, NINF), 0.)
715 self.assertEqual(math.pow(-1.9, NINF), 0.)
716
717 # pow(x, INF)
718 self.assertEqual(math.pow(1.9, INF), INF)
719 self.assertEqual(math.pow(1.1, INF), INF)
720 self.assertEqual(math.pow(0.9, INF), 0.)
721 self.assertEqual(math.pow(0.1, INF), 0.)
722 self.assertEqual(math.pow(-0.1, INF), 0.)
723 self.assertEqual(math.pow(-0.9, INF), 0.)
724 self.assertEqual(math.pow(-1.1, INF), INF)
725 self.assertEqual(math.pow(-1.9, INF), INF)
726
727 # pow(x, y) should work for x negative, y an integer
728 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
729 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
730 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
731 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
732 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
733 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
734 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
735 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
736 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
737 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
738
739 # the following tests have been commented out since they don't
740 # really belong here: the implementation of ** for floats is
741 # independent of the implemention of math.pow
742 #self.assertEqual(1**NAN, 1)
743 #self.assertEqual(1**INF, 1)
744 #self.assertEqual(1**NINF, 1)
745 #self.assertEqual(1**0, 1)
746 #self.assertEqual(1.**NAN, 1)
747 #self.assertEqual(1.**INF, 1)
748 #self.assertEqual(1.**NINF, 1)
749 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000750
Thomas Wouters89f507f2006-12-13 04:49:30 +0000751 def testRadians(self):
752 self.assertRaises(TypeError, math.radians)
753 self.ftest('radians(180)', math.radians(180), math.pi)
754 self.ftest('radians(90)', math.radians(90), math.pi/2)
755 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000756
Thomas Wouters89f507f2006-12-13 04:49:30 +0000757 def testSin(self):
758 self.assertRaises(TypeError, math.sin)
759 self.ftest('sin(0)', math.sin(0), 0)
760 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
761 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000762 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000763 self.assertTrue(math.isnan(math.sin(INF)))
764 self.assertTrue(math.isnan(math.sin(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000765 except ValueError:
766 self.assertRaises(ValueError, math.sin, INF)
767 self.assertRaises(ValueError, math.sin, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000768 self.assertTrue(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000769
Thomas Wouters89f507f2006-12-13 04:49:30 +0000770 def testSinh(self):
771 self.assertRaises(TypeError, math.sinh)
772 self.ftest('sinh(0)', math.sinh(0), 0)
773 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
774 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000775 self.assertEquals(math.sinh(INF), INF)
Christian Heimesa342c012008-04-20 21:01:16 +0000776 self.assertEquals(math.sinh(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000777 self.assertTrue(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000778
Thomas Wouters89f507f2006-12-13 04:49:30 +0000779 def testSqrt(self):
780 self.assertRaises(TypeError, math.sqrt)
781 self.ftest('sqrt(0)', math.sqrt(0), 0)
782 self.ftest('sqrt(1)', math.sqrt(1), 1)
783 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000784 self.assertEquals(math.sqrt(INF), INF)
785 self.assertRaises(ValueError, math.sqrt, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000786 self.assertTrue(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000787
Thomas Wouters89f507f2006-12-13 04:49:30 +0000788 def testTan(self):
789 self.assertRaises(TypeError, math.tan)
790 self.ftest('tan(0)', math.tan(0), 0)
791 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
792 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000793 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000794 self.assertTrue(math.isnan(math.tan(INF)))
795 self.assertTrue(math.isnan(math.tan(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000796 except:
797 self.assertRaises(ValueError, math.tan, INF)
798 self.assertRaises(ValueError, math.tan, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000799 self.assertTrue(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000800
Thomas Wouters89f507f2006-12-13 04:49:30 +0000801 def testTanh(self):
802 self.assertRaises(TypeError, math.tanh)
803 self.ftest('tanh(0)', math.tanh(0), 0)
804 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000805 self.ftest('tanh(inf)', math.tanh(INF), 1)
806 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000807 self.assertTrue(math.isnan(math.tanh(NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000808 # check that tanh(-0.) == -0. on IEEE 754 systems
809 if float.__getformat__("double").startswith("IEEE"):
810 self.assertEqual(math.tanh(-0.), -0.)
811 self.assertEqual(math.copysign(1., math.tanh(-0.)),
812 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000813
Christian Heimes400adb02008-02-01 08:12:03 +0000814 def test_trunc(self):
815 self.assertEqual(math.trunc(1), 1)
816 self.assertEqual(math.trunc(-1), -1)
817 self.assertEqual(type(math.trunc(1)), int)
818 self.assertEqual(type(math.trunc(1.5)), int)
819 self.assertEqual(math.trunc(1.5), 1)
820 self.assertEqual(math.trunc(-1.5), -1)
821 self.assertEqual(math.trunc(1.999999), 1)
822 self.assertEqual(math.trunc(-1.999999), -1)
823 self.assertEqual(math.trunc(-0.999999), -0)
824 self.assertEqual(math.trunc(-100.999), -100)
825
826 class TestTrunc(object):
827 def __trunc__(self):
828 return 23
829
830 class TestNoTrunc(object):
831 pass
832
833 self.assertEqual(math.trunc(TestTrunc()), 23)
834
835 self.assertRaises(TypeError, math.trunc)
836 self.assertRaises(TypeError, math.trunc, 1, 2)
837 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
838
839 # XXX Doesn't work because the method is looked up on
840 # the type only.
841 #t = TestNoTrunc()
842 #t.__trunc__ = lambda *args: args
843 #self.assertEquals((), math.trunc(t))
844 #self.assertRaises(TypeError, math.trunc, t, 0)
845
Christian Heimes072c0f12008-01-03 23:01:04 +0000846 def testCopysign(self):
847 self.assertEqual(math.copysign(1, 42), 1.0)
848 self.assertEqual(math.copysign(0., 42), 0.0)
849 self.assertEqual(math.copysign(1., -42), -1.0)
850 self.assertEqual(math.copysign(3, 0.), 3.0)
851 self.assertEqual(math.copysign(4., -0.), -4.0)
852
853 def testIsnan(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000854 self.assertTrue(math.isnan(float("nan")))
855 self.assertTrue(math.isnan(float("inf")* 0.))
856 self.assertFalse(math.isnan(float("inf")))
857 self.assertFalse(math.isnan(0.))
858 self.assertFalse(math.isnan(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000859
860 def testIsinf(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000861 self.assertTrue(math.isinf(float("inf")))
862 self.assertTrue(math.isinf(float("-inf")))
863 self.assertTrue(math.isinf(1E400))
864 self.assertTrue(math.isinf(-1E400))
865 self.assertFalse(math.isinf(float("nan")))
866 self.assertFalse(math.isinf(0.))
867 self.assertFalse(math.isinf(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000868
Thomas Wouters89f507f2006-12-13 04:49:30 +0000869 # RED_FLAG 16-Oct-2000 Tim
870 # While 2.0 is more consistent about exceptions than previous releases, it
871 # still fails this part of the test on some platforms. For now, we only
872 # *run* test_exceptions() in verbose mode, so that this isn't normally
873 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000874
Thomas Wouters89f507f2006-12-13 04:49:30 +0000875 if verbose:
876 def test_exceptions(self):
877 try:
878 x = math.exp(-1000000000)
879 except:
880 # mathmodule.c is failing to weed out underflows from libm, or
881 # we've got an fp format with huge dynamic range
882 self.fail("underflowing exp() should not have raised "
883 "an exception")
884 if x != 0:
885 self.fail("underflowing exp() should have returned 0")
886
887 # If this fails, probably using a strict IEEE-754 conforming libm, and x
888 # is +Inf afterwards. But Python wants overflows detected by default.
889 try:
890 x = math.exp(1000000000)
891 except OverflowError:
892 pass
893 else:
894 self.fail("overflowing exp() didn't trigger OverflowError")
895
896 # If this fails, it could be a puzzle. One odd possibility is that
897 # mathmodule.c's macros are getting confused while comparing
898 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
899 # as a result (and so raising OverflowError instead).
900 try:
901 x = math.sqrt(-1.0)
902 except ValueError:
903 pass
904 else:
905 self.fail("sqrt(-1) didn't raise ValueError")
906
Mark Dickinson63566232009-09-18 21:04:19 +0000907 @requires_IEEE_754
Christian Heimes53876d92008-04-19 00:31:39 +0000908 def test_testfile(self):
Christian Heimes53876d92008-04-19 00:31:39 +0000909 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
910 # Skip if either the input or result is complex, or if
911 # flags is nonempty
912 if ai != 0. or ei != 0. or flags:
913 continue
914 if fn in ['rect', 'polar']:
915 # no real versions of rect, polar
916 continue
917 func = getattr(math, fn)
Christian Heimesa342c012008-04-20 21:01:16 +0000918 try:
919 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +0000920 except ValueError as exc:
921 message = (("Unexpected ValueError: %s\n " +
922 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +0000923 self.fail(message)
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000924 except OverflowError:
925 message = ("Unexpected OverflowError in " +
926 "test %s:%s(%r)\n" % (id, fn, ar))
927 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +0000928 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000929
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000930 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
931 "test requires IEEE 754 doubles")
932 def test_mtestfile(self):
933 ALLOWED_ERROR = 20 # permitted error, in ulps
934 fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}"
935
936 failures = []
937 for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
938 func = getattr(math, fn)
939
940 if 'invalid' in flags or 'divide-by-zero' in flags:
941 expected = 'ValueError'
942 elif 'overflow' in flags:
943 expected = 'OverflowError'
944
945 try:
946 got = func(arg)
947 except ValueError:
948 got = 'ValueError'
949 except OverflowError:
950 got = 'OverflowError'
951
952 diff_ulps = None
953 if isinstance(got, float) and isinstance(expected, float):
954 if math.isnan(expected) and math.isnan(got):
955 continue
956 if not math.isnan(expected) and not math.isnan(got):
957 diff_ulps = to_ulps(expected) - to_ulps(got)
958 if diff_ulps <= ALLOWED_ERROR:
959 continue
960
961 if isinstance(got, str) and isinstance(expected, str):
962 if got == expected:
963 continue
964
965 fail_msg = fail_fmt.format(id, fn, arg, expected, got)
966 if diff_ulps is not None:
967 fail_msg += ' ({} ulps)'.format(diff_ulps)
968 failures.append(fail_msg)
969
970 if failures:
971 self.fail('Failures in test_mtestfile:\n ' +
972 '\n '.join(failures))
973
974
Thomas Wouters89f507f2006-12-13 04:49:30 +0000975def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +0000976 from doctest import DocFileSuite
977 suite = unittest.TestSuite()
978 suite.addTest(unittest.makeSuite(MathTests))
979 suite.addTest(DocFileSuite("ieee754.txt"))
980 run_unittest(suite)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000981
982if __name__ == '__main__':
983 test_main()