blob: 023dea94654a769dce38205b2cab1a2424cc6427 [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
Eric Smithf24a0d92010-12-04 13:32:18 +00004from test.support import run_unittest, verbose, requires_IEEE_754
Victor Stinnerfce92332011-06-01 12:28:04 +02005from test import support
Thomas Wouters89f507f2006-12-13 04:49:30 +00006import unittest
7import math
Christian Heimes53876d92008-04-19 00:31:39 +00008import os
Victor Stinnercd9dd372011-05-10 23:40:17 +02009import platform
Christian Heimes53876d92008-04-19 00:31:39 +000010import sys
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000011import struct
Victor Stinnerbe3da382010-11-07 14:14:27 +000012import sysconfig
Guido van Rossumfcce6301996-08-08 18:26:25 +000013
Christian Heimes53876d92008-04-19 00:31:39 +000014eps = 1E-05
15NAN = float('nan')
16INF = float('inf')
17NINF = float('-inf')
18
Mark Dickinson5c567082009-04-24 16:39:07 +000019# detect evidence of double-rounding: fsum is not always correctly
20# rounded on machines that suffer from double rounding.
21x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
22HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
23
Christian Heimes53876d92008-04-19 00:31:39 +000024# locate file with test values
25if __name__ == '__main__':
26 file = sys.argv[0]
27else:
28 file = __file__
29test_dir = os.path.dirname(file) or os.curdir
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000030math_testcases = os.path.join(test_dir, 'math_testcases.txt')
Christian Heimes53876d92008-04-19 00:31:39 +000031test_file = os.path.join(test_dir, 'cmath_testcases.txt')
32
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000033def to_ulps(x):
34 """Convert a non-NaN float x to an integer, in such a way that
35 adjacent floats are converted to adjacent integers. Then
36 abs(ulps(x) - ulps(y)) gives the difference in ulps between two
37 floats.
38
39 The results from this function will only make sense on platforms
40 where C doubles are represented in IEEE 754 binary64 format.
41
42 """
Mark Dickinsond412ab52009-10-17 07:10:00 +000043 n = struct.unpack('<q', struct.pack('<d', x))[0]
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000044 if n < 0:
45 n = ~(n+2**63)
46 return n
47
Mark Dickinson05d2e082009-12-11 20:17:17 +000048def ulps_check(expected, got, ulps=20):
49 """Given non-NaN floats `expected` and `got`,
50 check that they're equal to within the given number of ulps.
51
52 Returns None on success and an error message on failure."""
53
54 ulps_error = to_ulps(got) - to_ulps(expected)
55 if abs(ulps_error) <= ulps:
56 return None
57 return "error = {} ulps; permitted error = {} ulps".format(ulps_error,
58 ulps)
59
Mark Dickinson4c8a9a22010-05-15 17:02:38 +000060# Here's a pure Python version of the math.factorial algorithm, for
61# documentation and comparison purposes.
62#
63# Formula:
64#
65# factorial(n) = factorial_odd_part(n) << (n - count_set_bits(n))
66#
67# where
68#
69# factorial_odd_part(n) = product_{i >= 0} product_{0 < j <= n >> i; j odd} j
70#
71# The outer product above is an infinite product, but once i >= n.bit_length,
72# (n >> i) < 1 and the corresponding term of the product is empty. So only the
73# finitely many terms for 0 <= i < n.bit_length() contribute anything.
74#
75# We iterate downwards from i == n.bit_length() - 1 to i == 0. The inner
76# product in the formula above starts at 1 for i == n.bit_length(); for each i
77# < n.bit_length() we get the inner product for i from that for i + 1 by
78# multiplying by all j in {n >> i+1 < j <= n >> i; j odd}. In Python terms,
79# this set is range((n >> i+1) + 1 | 1, (n >> i) + 1 | 1, 2).
80
81def count_set_bits(n):
82 """Number of '1' bits in binary expansion of a nonnnegative integer."""
83 return 1 + count_set_bits(n & n - 1) if n else 0
84
85def partial_product(start, stop):
86 """Product of integers in range(start, stop, 2), computed recursively.
87 start and stop should both be odd, with start <= stop.
88
89 """
90 numfactors = (stop - start) >> 1
91 if not numfactors:
92 return 1
93 elif numfactors == 1:
94 return start
95 else:
96 mid = (start + numfactors) | 1
97 return partial_product(start, mid) * partial_product(mid, stop)
98
99def py_factorial(n):
100 """Factorial of nonnegative integer n, via "Binary Split Factorial Formula"
101 described at http://www.luschny.de/math/factorial/binarysplitfact.html
102
103 """
104 inner = outer = 1
105 for i in reversed(range(n.bit_length())):
106 inner *= partial_product((n >> i + 1) + 1 | 1, (n >> i) + 1 | 1)
107 outer *= inner
108 return outer << (n - count_set_bits(n))
109
Mark Dickinson05d2e082009-12-11 20:17:17 +0000110def acc_check(expected, got, rel_err=2e-15, abs_err = 5e-323):
111 """Determine whether non-NaN floats a and b are equal to within a
112 (small) rounding error. The default values for rel_err and
113 abs_err are chosen to be suitable for platforms where a float is
114 represented by an IEEE 754 double. They allow an error of between
115 9 and 19 ulps."""
116
117 # need to special case infinities, since inf - inf gives nan
118 if math.isinf(expected) and got == expected:
119 return None
120
121 error = got - expected
122
123 permitted_error = max(abs_err, rel_err * abs(expected))
124 if abs(error) < permitted_error:
125 return None
126 return "error = {}; permitted error = {}".format(error,
127 permitted_error)
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000128
129def parse_mtestfile(fname):
130 """Parse a file with test values
131
132 -- starts a comment
133 blank lines, or lines containing only a comment, are ignored
134 other lines are expected to have the form
135 id fn arg -> expected [flag]*
136
137 """
138 with open(fname) as fp:
139 for line in fp:
140 # strip comments, and skip blank lines
141 if '--' in line:
142 line = line[:line.index('--')]
143 if not line.strip():
144 continue
145
146 lhs, rhs = line.split('->')
147 id, fn, arg = lhs.split()
148 rhs_pieces = rhs.split()
149 exp = rhs_pieces[0]
150 flags = rhs_pieces[1:]
151
152 yield (id, fn, float(arg), float(exp), flags)
153
Christian Heimes53876d92008-04-19 00:31:39 +0000154def parse_testfile(fname):
155 """Parse a file with test values
156
157 Empty lines or lines starting with -- are ignored
158 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
159 """
160 with open(fname) as fp:
161 for line in fp:
162 # skip comment lines and blank lines
163 if line.startswith('--') or not line.strip():
164 continue
165
166 lhs, rhs = line.split('->')
167 id, fn, arg_real, arg_imag = lhs.split()
168 rhs_pieces = rhs.split()
169 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
170 flags = rhs_pieces[2:]
171
172 yield (id, fn,
173 float(arg_real), float(arg_imag),
174 float(exp_real), float(exp_imag),
175 flags
176 )
Guido van Rossumfcce6301996-08-08 18:26:25 +0000177
Thomas Wouters89f507f2006-12-13 04:49:30 +0000178class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +0000179
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180 def ftest(self, name, value, expected):
181 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +0000182 # Use %r instead of %f so the error message
183 # displays full precision. Otherwise discrepancies
184 # in the last few bits will lead to very confusing
185 # error messages
186 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000188
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189 def testConstants(self):
190 self.ftest('pi', math.pi, 3.1415926)
191 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000192
Thomas Wouters89f507f2006-12-13 04:49:30 +0000193 def testAcos(self):
194 self.assertRaises(TypeError, math.acos)
195 self.ftest('acos(-1)', math.acos(-1), math.pi)
196 self.ftest('acos(0)', math.acos(0), math.pi/2)
197 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000198 self.assertRaises(ValueError, math.acos, INF)
199 self.assertRaises(ValueError, math.acos, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000200 self.assertTrue(math.isnan(math.acos(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000201
202 def testAcosh(self):
203 self.assertRaises(TypeError, math.acosh)
204 self.ftest('acosh(1)', math.acosh(1), 0)
205 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
206 self.assertRaises(ValueError, math.acosh, 0)
207 self.assertRaises(ValueError, math.acosh, -1)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000208 self.assertEqual(math.acosh(INF), INF)
Christian Heimes53876d92008-04-19 00:31:39 +0000209 self.assertRaises(ValueError, math.acosh, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000210 self.assertTrue(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000211
Thomas Wouters89f507f2006-12-13 04:49:30 +0000212 def testAsin(self):
213 self.assertRaises(TypeError, math.asin)
214 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
215 self.ftest('asin(0)', math.asin(0), 0)
216 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000217 self.assertRaises(ValueError, math.asin, INF)
218 self.assertRaises(ValueError, math.asin, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000219 self.assertTrue(math.isnan(math.asin(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000220
221 def testAsinh(self):
222 self.assertRaises(TypeError, math.asinh)
223 self.ftest('asinh(0)', math.asinh(0), 0)
224 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
225 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000226 self.assertEqual(math.asinh(INF), INF)
227 self.assertEqual(math.asinh(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000228 self.assertTrue(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000229
Thomas Wouters89f507f2006-12-13 04:49:30 +0000230 def testAtan(self):
231 self.assertRaises(TypeError, math.atan)
232 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
233 self.ftest('atan(0)', math.atan(0), 0)
234 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes53876d92008-04-19 00:31:39 +0000235 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Christian Heimesa342c012008-04-20 21:01:16 +0000236 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000237 self.assertTrue(math.isnan(math.atan(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000238
239 def testAtanh(self):
240 self.assertRaises(TypeError, math.atan)
241 self.ftest('atanh(0)', math.atanh(0), 0)
242 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
243 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
244 self.assertRaises(ValueError, math.atanh, 1)
245 self.assertRaises(ValueError, math.atanh, -1)
246 self.assertRaises(ValueError, math.atanh, INF)
247 self.assertRaises(ValueError, math.atanh, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000248 self.assertTrue(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000249
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250 def testAtan2(self):
251 self.assertRaises(TypeError, math.atan2)
252 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
253 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
254 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
255 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
256 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000257
Christian Heimese57950f2008-04-21 13:08:03 +0000258 # math.atan2(0, x)
259 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
260 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
261 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
262 self.assertEqual(math.atan2(0., 0.), 0.)
263 self.assertEqual(math.atan2(0., 2.3), 0.)
264 self.assertEqual(math.atan2(0., INF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000265 self.assertTrue(math.isnan(math.atan2(0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000266 # math.atan2(-0, x)
267 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
268 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
269 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
270 self.assertEqual(math.atan2(-0., 0.), -0.)
271 self.assertEqual(math.atan2(-0., 2.3), -0.)
272 self.assertEqual(math.atan2(-0., INF), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000273 self.assertTrue(math.isnan(math.atan2(-0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000274 # math.atan2(INF, x)
275 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
276 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
277 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
278 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
279 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
280 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000281 self.assertTrue(math.isnan(math.atan2(INF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000282 # math.atan2(NINF, x)
283 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
284 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
285 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
286 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
287 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
288 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000289 self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000290 # math.atan2(+finite, x)
291 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
292 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
293 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
294 self.assertEqual(math.atan2(2.3, INF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000295 self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000296 # math.atan2(-finite, x)
297 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
298 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
299 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
300 self.assertEqual(math.atan2(-2.3, INF), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000301 self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000302 # math.atan2(NAN, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000303 self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
304 self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
305 self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
306 self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
307 self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
308 self.assertTrue(math.isnan(math.atan2(NAN, INF)))
309 self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000310
Thomas Wouters89f507f2006-12-13 04:49:30 +0000311 def testCeil(self):
312 self.assertRaises(TypeError, math.ceil)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000313 self.assertEqual(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000314 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
315 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
316 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
317 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
318 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
319 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000320 #self.assertEqual(math.ceil(INF), INF)
321 #self.assertEqual(math.ceil(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000322 #self.assertTrue(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000323
Guido van Rossum13e05de2007-08-23 22:56:55 +0000324 class TestCeil:
325 def __ceil__(self):
326 return 42
327 class TestNoCeil:
328 pass
329 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
330 self.assertRaises(TypeError, math.ceil, TestNoCeil())
331
332 t = TestNoCeil()
333 t.__ceil__ = lambda *args: args
334 self.assertRaises(TypeError, math.ceil, t)
335 self.assertRaises(TypeError, math.ceil, t, 0)
336
Mark Dickinson63566232009-09-18 21:04:19 +0000337 @requires_IEEE_754
338 def testCopysign(self):
Mark Dickinson06b59e02010-02-06 23:16:50 +0000339 self.assertEqual(math.copysign(1, 42), 1.0)
340 self.assertEqual(math.copysign(0., 42), 0.0)
341 self.assertEqual(math.copysign(1., -42), -1.0)
342 self.assertEqual(math.copysign(3, 0.), 3.0)
343 self.assertEqual(math.copysign(4., -0.), -4.0)
344
Mark Dickinson63566232009-09-18 21:04:19 +0000345 self.assertRaises(TypeError, math.copysign)
346 # copysign should let us distinguish signs of zeros
Ezio Melottib3aedd42010-11-20 19:04:17 +0000347 self.assertEqual(math.copysign(1., 0.), 1.)
348 self.assertEqual(math.copysign(1., -0.), -1.)
349 self.assertEqual(math.copysign(INF, 0.), INF)
350 self.assertEqual(math.copysign(INF, -0.), NINF)
351 self.assertEqual(math.copysign(NINF, 0.), INF)
352 self.assertEqual(math.copysign(NINF, -0.), NINF)
Mark Dickinson63566232009-09-18 21:04:19 +0000353 # and of infinities
Ezio Melottib3aedd42010-11-20 19:04:17 +0000354 self.assertEqual(math.copysign(1., INF), 1.)
355 self.assertEqual(math.copysign(1., NINF), -1.)
356 self.assertEqual(math.copysign(INF, INF), INF)
357 self.assertEqual(math.copysign(INF, NINF), NINF)
358 self.assertEqual(math.copysign(NINF, INF), INF)
359 self.assertEqual(math.copysign(NINF, NINF), NINF)
Mark Dickinson06b59e02010-02-06 23:16:50 +0000360 self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
361 self.assertTrue(math.isnan(math.copysign(NAN, INF)))
362 self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
363 self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
Mark Dickinson63566232009-09-18 21:04:19 +0000364 # copysign(INF, NAN) may be INF or it may be NINF, since
365 # we don't know whether the sign bit of NAN is set on any
366 # given platform.
Mark Dickinson06b59e02010-02-06 23:16:50 +0000367 self.assertTrue(math.isinf(math.copysign(INF, NAN)))
Mark Dickinson63566232009-09-18 21:04:19 +0000368 # similarly, copysign(2., NAN) could be 2. or -2.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000369 self.assertEqual(abs(math.copysign(2., NAN)), 2.)
Christian Heimes53876d92008-04-19 00:31:39 +0000370
Thomas Wouters89f507f2006-12-13 04:49:30 +0000371 def testCos(self):
372 self.assertRaises(TypeError, math.cos)
373 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
374 self.ftest('cos(0)', math.cos(0), 1)
375 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
376 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000377 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000378 self.assertTrue(math.isnan(math.cos(INF)))
379 self.assertTrue(math.isnan(math.cos(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000380 except ValueError:
381 self.assertRaises(ValueError, math.cos, INF)
382 self.assertRaises(ValueError, math.cos, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000383 self.assertTrue(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000384
Thomas Wouters89f507f2006-12-13 04:49:30 +0000385 def testCosh(self):
386 self.assertRaises(TypeError, math.cosh)
387 self.ftest('cosh(0)', math.cosh(0), 1)
388 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
Ezio Melottib3aedd42010-11-20 19:04:17 +0000389 self.assertEqual(math.cosh(INF), INF)
390 self.assertEqual(math.cosh(NINF), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000391 self.assertTrue(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000392
Thomas Wouters89f507f2006-12-13 04:49:30 +0000393 def testDegrees(self):
394 self.assertRaises(TypeError, math.degrees)
395 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
396 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
397 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000398
Thomas Wouters89f507f2006-12-13 04:49:30 +0000399 def testExp(self):
400 self.assertRaises(TypeError, math.exp)
401 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
402 self.ftest('exp(0)', math.exp(0), 1)
403 self.ftest('exp(1)', math.exp(1), math.e)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000404 self.assertEqual(math.exp(INF), INF)
405 self.assertEqual(math.exp(NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000406 self.assertTrue(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000407
Thomas Wouters89f507f2006-12-13 04:49:30 +0000408 def testFabs(self):
409 self.assertRaises(TypeError, math.fabs)
410 self.ftest('fabs(-1)', math.fabs(-1), 1)
411 self.ftest('fabs(0)', math.fabs(0), 0)
412 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000413
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000414 def testFactorial(self):
Mark Dickinson4c8a9a22010-05-15 17:02:38 +0000415 self.assertEqual(math.factorial(0), 1)
416 self.assertEqual(math.factorial(0.0), 1)
417 total = 1
418 for i in range(1, 1000):
419 total *= i
420 self.assertEqual(math.factorial(i), total)
421 self.assertEqual(math.factorial(float(i)), total)
422 self.assertEqual(math.factorial(i), py_factorial(i))
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000423 self.assertRaises(ValueError, math.factorial, -1)
Mark Dickinson4c8a9a22010-05-15 17:02:38 +0000424 self.assertRaises(ValueError, math.factorial, -1.0)
Mark Dickinson5990d282014-04-10 09:29:39 -0400425 self.assertRaises(ValueError, math.factorial, -10**100)
426 self.assertRaises(ValueError, math.factorial, -1e100)
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000427 self.assertRaises(ValueError, math.factorial, math.pi)
Mark Dickinson5990d282014-04-10 09:29:39 -0400428
429 # Other implementations may place different upper bounds.
430 @support.cpython_only
431 def testFactorialHugeInputs(self):
432 # Currently raises ValueError for inputs that are too large
433 # to fit into a C long.
434 self.assertRaises(OverflowError, math.factorial, 10**100)
435 self.assertRaises(OverflowError, math.factorial, 1e100)
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000436
Thomas Wouters89f507f2006-12-13 04:49:30 +0000437 def testFloor(self):
438 self.assertRaises(TypeError, math.floor)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000439 self.assertEqual(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000440 self.ftest('floor(0.5)', math.floor(0.5), 0)
441 self.ftest('floor(1.0)', math.floor(1.0), 1)
442 self.ftest('floor(1.5)', math.floor(1.5), 1)
443 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
444 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
445 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000446 # pow() relies on floor() to check for integers
447 # This fails on some platforms - so check it here
448 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
449 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000450 #self.assertEqual(math.ceil(INF), INF)
451 #self.assertEqual(math.ceil(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000452 #self.assertTrue(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000453
Guido van Rossum13e05de2007-08-23 22:56:55 +0000454 class TestFloor:
455 def __floor__(self):
456 return 42
457 class TestNoFloor:
458 pass
459 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
460 self.assertRaises(TypeError, math.floor, TestNoFloor())
461
462 t = TestNoFloor()
463 t.__floor__ = lambda *args: args
464 self.assertRaises(TypeError, math.floor, t)
465 self.assertRaises(TypeError, math.floor, t, 0)
466
Thomas Wouters89f507f2006-12-13 04:49:30 +0000467 def testFmod(self):
468 self.assertRaises(TypeError, math.fmod)
Mark Dickinson5bc7a442011-05-03 21:13:40 +0100469 self.ftest('fmod(10, 1)', math.fmod(10, 1), 0.0)
470 self.ftest('fmod(10, 0.5)', math.fmod(10, 0.5), 0.0)
471 self.ftest('fmod(10, 1.5)', math.fmod(10, 1.5), 1.0)
472 self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0)
473 self.ftest('fmod(-10, 0.5)', math.fmod(-10, 0.5), -0.0)
474 self.ftest('fmod(-10, 1.5)', math.fmod(-10, 1.5), -1.0)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000475 self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
476 self.assertTrue(math.isnan(math.fmod(1., NAN)))
477 self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000478 self.assertRaises(ValueError, math.fmod, 1., 0.)
479 self.assertRaises(ValueError, math.fmod, INF, 1.)
480 self.assertRaises(ValueError, math.fmod, NINF, 1.)
481 self.assertRaises(ValueError, math.fmod, INF, 0.)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000482 self.assertEqual(math.fmod(3.0, INF), 3.0)
483 self.assertEqual(math.fmod(-3.0, INF), -3.0)
484 self.assertEqual(math.fmod(3.0, NINF), 3.0)
485 self.assertEqual(math.fmod(-3.0, NINF), -3.0)
486 self.assertEqual(math.fmod(0.0, 3.0), 0.0)
487 self.assertEqual(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000488
Thomas Wouters89f507f2006-12-13 04:49:30 +0000489 def testFrexp(self):
490 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000491
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000492 def testfrexp(name, result, expected):
493 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000494 if abs(mant-emant) > eps or exp != eexp:
495 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000496 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000497
Thomas Wouters89f507f2006-12-13 04:49:30 +0000498 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
499 testfrexp('frexp(0)', math.frexp(0), (0, 0))
500 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
501 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000502
Ezio Melottib3aedd42010-11-20 19:04:17 +0000503 self.assertEqual(math.frexp(INF)[0], INF)
504 self.assertEqual(math.frexp(NINF)[0], NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000505 self.assertTrue(math.isnan(math.frexp(NAN)[0]))
Christian Heimes53876d92008-04-19 00:31:39 +0000506
Mark Dickinson63566232009-09-18 21:04:19 +0000507 @requires_IEEE_754
Mark Dickinson5c567082009-04-24 16:39:07 +0000508 @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
509 "fsum is not exact on machines with double rounding")
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000510 def testFsum(self):
511 # math.fsum relies on exact rounding for correct operation.
512 # There's a known problem with IA32 floating-point that causes
513 # inexact rounding in some situations, and will cause the
514 # math.fsum tests below to fail; see issue #2937. On non IEEE
515 # 754 platforms, and on IEEE 754 platforms that exhibit the
516 # problem described in issue #2937, we simply skip the whole
517 # test.
518
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000519 # Python version of math.fsum, for comparison. Uses a
520 # different algorithm based on frexp, ldexp and integer
521 # arithmetic.
522 from sys import float_info
523 mant_dig = float_info.mant_dig
524 etiny = float_info.min_exp - mant_dig
525
526 def msum(iterable):
527 """Full precision summation. Compute sum(iterable) without any
528 intermediate accumulation of error. Based on the 'lsum' function
529 at http://code.activestate.com/recipes/393090/
530
531 """
532 tmant, texp = 0, 0
533 for x in iterable:
534 mant, exp = math.frexp(x)
535 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
536 if texp > exp:
537 tmant <<= texp-exp
538 texp = exp
539 else:
540 mant <<= exp-texp
541 tmant += mant
542 # Round tmant * 2**texp to a float. The original recipe
543 # used float(str(tmant)) * 2.0**texp for this, but that's
544 # a little unsafe because str -> float conversion can't be
545 # relied upon to do correct rounding on all platforms.
546 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
547 if tail > 0:
548 h = 1 << (tail-1)
549 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
550 texp += tail
551 return math.ldexp(tmant, texp)
552
553 test_values = [
554 ([], 0.0),
555 ([0.0], 0.0),
556 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
557 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
558 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
559 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
560 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
561 ([1./n for n in range(1, 1001)],
562 float.fromhex('0x1.df11f45f4e61ap+2')),
563 ([(-1.)**n/n for n in range(1, 1001)],
564 float.fromhex('-0x1.62a2af1bd3624p-1')),
565 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
566 ([1e16, 1., 1e-16], 10000000000000002.0),
567 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
568 # exercise code for resizing partials array
569 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
570 [-2.**1022],
571 float.fromhex('0x1.5555555555555p+970')),
572 ]
573
574 for i, (vals, expected) in enumerate(test_values):
575 try:
576 actual = math.fsum(vals)
577 except OverflowError:
578 self.fail("test %d failed: got OverflowError, expected %r "
579 "for math.fsum(%.100r)" % (i, expected, vals))
580 except ValueError:
581 self.fail("test %d failed: got ValueError, expected %r "
582 "for math.fsum(%.100r)" % (i, expected, vals))
583 self.assertEqual(actual, expected)
584
585 from random import random, gauss, shuffle
586 for j in range(1000):
587 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
588 s = 0
589 for i in range(200):
590 v = gauss(0, random()) ** 7 - s
591 s += v
592 vals.append(v)
593 shuffle(vals)
594
595 s = msum(vals)
596 self.assertEqual(msum(vals), math.fsum(vals))
597
Thomas Wouters89f507f2006-12-13 04:49:30 +0000598 def testHypot(self):
599 self.assertRaises(TypeError, math.hypot)
600 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
601 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000602 self.assertEqual(math.hypot(NAN, INF), INF)
603 self.assertEqual(math.hypot(INF, NAN), INF)
604 self.assertEqual(math.hypot(NAN, NINF), INF)
605 self.assertEqual(math.hypot(NINF, NAN), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000606 self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
607 self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000608
Thomas Wouters89f507f2006-12-13 04:49:30 +0000609 def testLdexp(self):
610 self.assertRaises(TypeError, math.ldexp)
611 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
612 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
613 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
614 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000615 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
616 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000617 self.assertEqual(math.ldexp(1., -1000000), 0.)
618 self.assertEqual(math.ldexp(-1., -1000000), -0.)
619 self.assertEqual(math.ldexp(INF, 30), INF)
620 self.assertEqual(math.ldexp(NINF, -213), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000621 self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000622
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000623 # large second argument
624 for n in [10**5, 10**10, 10**20, 10**40]:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000625 self.assertEqual(math.ldexp(INF, -n), INF)
626 self.assertEqual(math.ldexp(NINF, -n), NINF)
627 self.assertEqual(math.ldexp(1., -n), 0.)
628 self.assertEqual(math.ldexp(-1., -n), -0.)
629 self.assertEqual(math.ldexp(0., -n), 0.)
630 self.assertEqual(math.ldexp(-0., -n), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000631 self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000632
633 self.assertRaises(OverflowError, math.ldexp, 1., n)
634 self.assertRaises(OverflowError, math.ldexp, -1., n)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000635 self.assertEqual(math.ldexp(0., n), 0.)
636 self.assertEqual(math.ldexp(-0., n), -0.)
637 self.assertEqual(math.ldexp(INF, n), INF)
638 self.assertEqual(math.ldexp(NINF, n), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000639 self.assertTrue(math.isnan(math.ldexp(NAN, n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000640
Thomas Wouters89f507f2006-12-13 04:49:30 +0000641 def testLog(self):
642 self.assertRaises(TypeError, math.log)
643 self.ftest('log(1/e)', math.log(1/math.e), -1)
644 self.ftest('log(1)', math.log(1), 0)
645 self.ftest('log(e)', math.log(math.e), 1)
646 self.ftest('log(32,2)', math.log(32,2), 5)
647 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
648 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Mark Dickinsonc6037172010-09-29 19:06:36 +0000649 self.ftest('log(10**1000)', math.log(10**1000),
650 2302.5850929940457)
651 self.assertRaises(ValueError, math.log, -1.5)
652 self.assertRaises(ValueError, math.log, -10**1000)
Christian Heimes53876d92008-04-19 00:31:39 +0000653 self.assertRaises(ValueError, math.log, NINF)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000654 self.assertEqual(math.log(INF), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000655 self.assertTrue(math.isnan(math.log(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000656
657 def testLog1p(self):
658 self.assertRaises(TypeError, math.log1p)
Christian Heimes53876d92008-04-19 00:31:39 +0000659 n= 2**90
Ezio Melottib3aedd42010-11-20 19:04:17 +0000660 self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000661
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200662 @requires_IEEE_754
663 def testLog2(self):
664 self.assertRaises(TypeError, math.log2)
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200665
666 # Check some integer values
667 self.assertEqual(math.log2(1), 0.0)
668 self.assertEqual(math.log2(2), 1.0)
669 self.assertEqual(math.log2(4), 2.0)
670
671 # Large integer values
672 self.assertEqual(math.log2(2**1023), 1023.0)
673 self.assertEqual(math.log2(2**1024), 1024.0)
674 self.assertEqual(math.log2(2**2000), 2000.0)
675
676 self.assertRaises(ValueError, math.log2, -1.5)
677 self.assertRaises(ValueError, math.log2, NINF)
678 self.assertTrue(math.isnan(math.log2(NAN)))
679
Victor Stinnercd9dd372011-05-10 23:40:17 +0200680 @requires_IEEE_754
Victor Stinnerebbbdaf2011-06-01 13:19:07 +0200681 # log2() is not accurate enough on Mac OS X Tiger (10.4)
682 @support.requires_mac_ver(10, 5)
Victor Stinnercd9dd372011-05-10 23:40:17 +0200683 def testLog2Exact(self):
684 # Check that we get exact equality for log2 of powers of 2.
685 actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
686 expected = [float(n) for n in range(-1074, 1024)]
687 self.assertEqual(actual, expected)
688
Thomas Wouters89f507f2006-12-13 04:49:30 +0000689 def testLog10(self):
690 self.assertRaises(TypeError, math.log10)
691 self.ftest('log10(0.1)', math.log10(0.1), -1)
692 self.ftest('log10(1)', math.log10(1), 0)
693 self.ftest('log10(10)', math.log10(10), 1)
Mark Dickinsonc6037172010-09-29 19:06:36 +0000694 self.ftest('log10(10**1000)', math.log10(10**1000), 1000.0)
695 self.assertRaises(ValueError, math.log10, -1.5)
696 self.assertRaises(ValueError, math.log10, -10**1000)
Christian Heimes53876d92008-04-19 00:31:39 +0000697 self.assertRaises(ValueError, math.log10, NINF)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000698 self.assertEqual(math.log(INF), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000699 self.assertTrue(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000700
Thomas Wouters89f507f2006-12-13 04:49:30 +0000701 def testModf(self):
702 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000703
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000704 def testmodf(name, result, expected):
705 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000706 if abs(v1-e1) > eps or abs(v2-e2):
707 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000708 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000709
Thomas Wouters89f507f2006-12-13 04:49:30 +0000710 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
711 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000712
Ezio Melottib3aedd42010-11-20 19:04:17 +0000713 self.assertEqual(math.modf(INF), (0.0, INF))
714 self.assertEqual(math.modf(NINF), (-0.0, NINF))
Christian Heimes53876d92008-04-19 00:31:39 +0000715
716 modf_nan = math.modf(NAN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000717 self.assertTrue(math.isnan(modf_nan[0]))
718 self.assertTrue(math.isnan(modf_nan[1]))
Christian Heimes53876d92008-04-19 00:31:39 +0000719
Thomas Wouters89f507f2006-12-13 04:49:30 +0000720 def testPow(self):
721 self.assertRaises(TypeError, math.pow)
722 self.ftest('pow(0,1)', math.pow(0,1), 0)
723 self.ftest('pow(1,0)', math.pow(1,0), 1)
724 self.ftest('pow(2,1)', math.pow(2,1), 2)
725 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000726 self.assertEqual(math.pow(INF, 1), INF)
727 self.assertEqual(math.pow(NINF, 1), NINF)
728 self.assertEqual((math.pow(1, INF)), 1.)
729 self.assertEqual((math.pow(1, NINF)), 1.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000730 self.assertTrue(math.isnan(math.pow(NAN, 1)))
731 self.assertTrue(math.isnan(math.pow(2, NAN)))
732 self.assertTrue(math.isnan(math.pow(0, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000733 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000734
735 # pow(0., x)
736 self.assertEqual(math.pow(0., INF), 0.)
737 self.assertEqual(math.pow(0., 3.), 0.)
738 self.assertEqual(math.pow(0., 2.3), 0.)
739 self.assertEqual(math.pow(0., 2.), 0.)
740 self.assertEqual(math.pow(0., 0.), 1.)
741 self.assertEqual(math.pow(0., -0.), 1.)
742 self.assertRaises(ValueError, math.pow, 0., -2.)
743 self.assertRaises(ValueError, math.pow, 0., -2.3)
744 self.assertRaises(ValueError, math.pow, 0., -3.)
745 self.assertRaises(ValueError, math.pow, 0., NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000746 self.assertTrue(math.isnan(math.pow(0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000747
748 # pow(INF, x)
749 self.assertEqual(math.pow(INF, INF), INF)
750 self.assertEqual(math.pow(INF, 3.), INF)
751 self.assertEqual(math.pow(INF, 2.3), INF)
752 self.assertEqual(math.pow(INF, 2.), INF)
753 self.assertEqual(math.pow(INF, 0.), 1.)
754 self.assertEqual(math.pow(INF, -0.), 1.)
755 self.assertEqual(math.pow(INF, -2.), 0.)
756 self.assertEqual(math.pow(INF, -2.3), 0.)
757 self.assertEqual(math.pow(INF, -3.), 0.)
758 self.assertEqual(math.pow(INF, NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000759 self.assertTrue(math.isnan(math.pow(INF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000760
761 # pow(-0., x)
762 self.assertEqual(math.pow(-0., INF), 0.)
763 self.assertEqual(math.pow(-0., 3.), -0.)
764 self.assertEqual(math.pow(-0., 2.3), 0.)
765 self.assertEqual(math.pow(-0., 2.), 0.)
766 self.assertEqual(math.pow(-0., 0.), 1.)
767 self.assertEqual(math.pow(-0., -0.), 1.)
768 self.assertRaises(ValueError, math.pow, -0., -2.)
769 self.assertRaises(ValueError, math.pow, -0., -2.3)
770 self.assertRaises(ValueError, math.pow, -0., -3.)
771 self.assertRaises(ValueError, math.pow, -0., NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000772 self.assertTrue(math.isnan(math.pow(-0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000773
774 # pow(NINF, x)
775 self.assertEqual(math.pow(NINF, INF), INF)
776 self.assertEqual(math.pow(NINF, 3.), NINF)
777 self.assertEqual(math.pow(NINF, 2.3), INF)
778 self.assertEqual(math.pow(NINF, 2.), INF)
779 self.assertEqual(math.pow(NINF, 0.), 1.)
780 self.assertEqual(math.pow(NINF, -0.), 1.)
781 self.assertEqual(math.pow(NINF, -2.), 0.)
782 self.assertEqual(math.pow(NINF, -2.3), 0.)
783 self.assertEqual(math.pow(NINF, -3.), -0.)
784 self.assertEqual(math.pow(NINF, NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000785 self.assertTrue(math.isnan(math.pow(NINF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000786
787 # pow(-1, x)
788 self.assertEqual(math.pow(-1., INF), 1.)
789 self.assertEqual(math.pow(-1., 3.), -1.)
790 self.assertRaises(ValueError, math.pow, -1., 2.3)
791 self.assertEqual(math.pow(-1., 2.), 1.)
792 self.assertEqual(math.pow(-1., 0.), 1.)
793 self.assertEqual(math.pow(-1., -0.), 1.)
794 self.assertEqual(math.pow(-1., -2.), 1.)
795 self.assertRaises(ValueError, math.pow, -1., -2.3)
796 self.assertEqual(math.pow(-1., -3.), -1.)
797 self.assertEqual(math.pow(-1., NINF), 1.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000798 self.assertTrue(math.isnan(math.pow(-1., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000799
800 # pow(1, x)
801 self.assertEqual(math.pow(1., INF), 1.)
802 self.assertEqual(math.pow(1., 3.), 1.)
803 self.assertEqual(math.pow(1., 2.3), 1.)
804 self.assertEqual(math.pow(1., 2.), 1.)
805 self.assertEqual(math.pow(1., 0.), 1.)
806 self.assertEqual(math.pow(1., -0.), 1.)
807 self.assertEqual(math.pow(1., -2.), 1.)
808 self.assertEqual(math.pow(1., -2.3), 1.)
809 self.assertEqual(math.pow(1., -3.), 1.)
810 self.assertEqual(math.pow(1., NINF), 1.)
811 self.assertEqual(math.pow(1., NAN), 1.)
812
813 # pow(x, 0) should be 1 for any x
814 self.assertEqual(math.pow(2.3, 0.), 1.)
815 self.assertEqual(math.pow(-2.3, 0.), 1.)
816 self.assertEqual(math.pow(NAN, 0.), 1.)
817 self.assertEqual(math.pow(2.3, -0.), 1.)
818 self.assertEqual(math.pow(-2.3, -0.), 1.)
819 self.assertEqual(math.pow(NAN, -0.), 1.)
820
821 # pow(x, y) is invalid if x is negative and y is not integral
822 self.assertRaises(ValueError, math.pow, -1., 2.3)
823 self.assertRaises(ValueError, math.pow, -15., -3.1)
824
825 # pow(x, NINF)
826 self.assertEqual(math.pow(1.9, NINF), 0.)
827 self.assertEqual(math.pow(1.1, NINF), 0.)
828 self.assertEqual(math.pow(0.9, NINF), INF)
829 self.assertEqual(math.pow(0.1, NINF), INF)
830 self.assertEqual(math.pow(-0.1, NINF), INF)
831 self.assertEqual(math.pow(-0.9, NINF), INF)
832 self.assertEqual(math.pow(-1.1, NINF), 0.)
833 self.assertEqual(math.pow(-1.9, NINF), 0.)
834
835 # pow(x, INF)
836 self.assertEqual(math.pow(1.9, INF), INF)
837 self.assertEqual(math.pow(1.1, INF), INF)
838 self.assertEqual(math.pow(0.9, INF), 0.)
839 self.assertEqual(math.pow(0.1, INF), 0.)
840 self.assertEqual(math.pow(-0.1, INF), 0.)
841 self.assertEqual(math.pow(-0.9, INF), 0.)
842 self.assertEqual(math.pow(-1.1, INF), INF)
843 self.assertEqual(math.pow(-1.9, INF), INF)
844
845 # pow(x, y) should work for x negative, y an integer
846 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
847 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
848 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
849 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
850 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
851 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
852 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
853 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
854 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
855 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
856
857 # the following tests have been commented out since they don't
858 # really belong here: the implementation of ** for floats is
Ezio Melotti13925002011-03-16 11:05:33 +0200859 # independent of the implementation of math.pow
Christian Heimesa342c012008-04-20 21:01:16 +0000860 #self.assertEqual(1**NAN, 1)
861 #self.assertEqual(1**INF, 1)
862 #self.assertEqual(1**NINF, 1)
863 #self.assertEqual(1**0, 1)
864 #self.assertEqual(1.**NAN, 1)
865 #self.assertEqual(1.**INF, 1)
866 #self.assertEqual(1.**NINF, 1)
867 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000868
Thomas Wouters89f507f2006-12-13 04:49:30 +0000869 def testRadians(self):
870 self.assertRaises(TypeError, math.radians)
871 self.ftest('radians(180)', math.radians(180), math.pi)
872 self.ftest('radians(90)', math.radians(90), math.pi/2)
873 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000874
Thomas Wouters89f507f2006-12-13 04:49:30 +0000875 def testSin(self):
876 self.assertRaises(TypeError, math.sin)
877 self.ftest('sin(0)', math.sin(0), 0)
878 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
879 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000880 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000881 self.assertTrue(math.isnan(math.sin(INF)))
882 self.assertTrue(math.isnan(math.sin(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000883 except ValueError:
884 self.assertRaises(ValueError, math.sin, INF)
885 self.assertRaises(ValueError, math.sin, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000886 self.assertTrue(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000887
Thomas Wouters89f507f2006-12-13 04:49:30 +0000888 def testSinh(self):
889 self.assertRaises(TypeError, math.sinh)
890 self.ftest('sinh(0)', math.sinh(0), 0)
891 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
892 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000893 self.assertEqual(math.sinh(INF), INF)
894 self.assertEqual(math.sinh(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000895 self.assertTrue(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000896
Thomas Wouters89f507f2006-12-13 04:49:30 +0000897 def testSqrt(self):
898 self.assertRaises(TypeError, math.sqrt)
899 self.ftest('sqrt(0)', math.sqrt(0), 0)
900 self.ftest('sqrt(1)', math.sqrt(1), 1)
901 self.ftest('sqrt(4)', math.sqrt(4), 2)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000902 self.assertEqual(math.sqrt(INF), INF)
Christian Heimes53876d92008-04-19 00:31:39 +0000903 self.assertRaises(ValueError, math.sqrt, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000904 self.assertTrue(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000905
Thomas Wouters89f507f2006-12-13 04:49:30 +0000906 def testTan(self):
907 self.assertRaises(TypeError, math.tan)
908 self.ftest('tan(0)', math.tan(0), 0)
909 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
910 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000911 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000912 self.assertTrue(math.isnan(math.tan(INF)))
913 self.assertTrue(math.isnan(math.tan(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000914 except:
915 self.assertRaises(ValueError, math.tan, INF)
916 self.assertRaises(ValueError, math.tan, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000917 self.assertTrue(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000918
Thomas Wouters89f507f2006-12-13 04:49:30 +0000919 def testTanh(self):
920 self.assertRaises(TypeError, math.tanh)
921 self.ftest('tanh(0)', math.tanh(0), 0)
922 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000923 self.ftest('tanh(inf)', math.tanh(INF), 1)
924 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000925 self.assertTrue(math.isnan(math.tanh(NAN)))
Victor Stinnerbe3da382010-11-07 14:14:27 +0000926
927 @requires_IEEE_754
928 @unittest.skipIf(sysconfig.get_config_var('TANH_PRESERVES_ZERO_SIGN') == 0,
929 "system tanh() function doesn't copy the sign")
930 def testTanhSign(self):
Christian Heimese57950f2008-04-21 13:08:03 +0000931 # check that tanh(-0.) == -0. on IEEE 754 systems
Victor Stinnerbe3da382010-11-07 14:14:27 +0000932 self.assertEqual(math.tanh(-0.), -0.)
933 self.assertEqual(math.copysign(1., math.tanh(-0.)),
934 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000935
Christian Heimes400adb02008-02-01 08:12:03 +0000936 def test_trunc(self):
937 self.assertEqual(math.trunc(1), 1)
938 self.assertEqual(math.trunc(-1), -1)
939 self.assertEqual(type(math.trunc(1)), int)
940 self.assertEqual(type(math.trunc(1.5)), int)
941 self.assertEqual(math.trunc(1.5), 1)
942 self.assertEqual(math.trunc(-1.5), -1)
943 self.assertEqual(math.trunc(1.999999), 1)
944 self.assertEqual(math.trunc(-1.999999), -1)
945 self.assertEqual(math.trunc(-0.999999), -0)
946 self.assertEqual(math.trunc(-100.999), -100)
947
948 class TestTrunc(object):
949 def __trunc__(self):
950 return 23
951
952 class TestNoTrunc(object):
953 pass
954
955 self.assertEqual(math.trunc(TestTrunc()), 23)
956
957 self.assertRaises(TypeError, math.trunc)
958 self.assertRaises(TypeError, math.trunc, 1, 2)
959 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
960
Mark Dickinson8e0c9962010-07-11 17:38:24 +0000961 def testIsfinite(self):
962 self.assertTrue(math.isfinite(0.0))
963 self.assertTrue(math.isfinite(-0.0))
964 self.assertTrue(math.isfinite(1.0))
965 self.assertTrue(math.isfinite(-1.0))
966 self.assertFalse(math.isfinite(float("nan")))
967 self.assertFalse(math.isfinite(float("inf")))
968 self.assertFalse(math.isfinite(float("-inf")))
969
Christian Heimes072c0f12008-01-03 23:01:04 +0000970 def testIsnan(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000971 self.assertTrue(math.isnan(float("nan")))
972 self.assertTrue(math.isnan(float("inf")* 0.))
973 self.assertFalse(math.isnan(float("inf")))
974 self.assertFalse(math.isnan(0.))
975 self.assertFalse(math.isnan(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000976
977 def testIsinf(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000978 self.assertTrue(math.isinf(float("inf")))
979 self.assertTrue(math.isinf(float("-inf")))
980 self.assertTrue(math.isinf(1E400))
981 self.assertTrue(math.isinf(-1E400))
982 self.assertFalse(math.isinf(float("nan")))
983 self.assertFalse(math.isinf(0.))
984 self.assertFalse(math.isinf(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000985
Mark Dickinsona5d0c7c2015-01-11 11:55:29 +0000986 @requires_IEEE_754
987 def test_nan_constant(self):
988 self.assertTrue(math.isnan(math.nan))
989
990 @requires_IEEE_754
991 def test_inf_constant(self):
992 self.assertTrue(math.isinf(math.inf))
993 self.assertGreater(math.inf, 0.0)
994 self.assertEqual(math.inf, float("inf"))
995 self.assertEqual(-math.inf, float("-inf"))
996
Thomas Wouters89f507f2006-12-13 04:49:30 +0000997 # RED_FLAG 16-Oct-2000 Tim
998 # While 2.0 is more consistent about exceptions than previous releases, it
999 # still fails this part of the test on some platforms. For now, we only
1000 # *run* test_exceptions() in verbose mode, so that this isn't normally
1001 # tested.
Serhiy Storchaka43767632013-11-03 21:31:38 +02001002 @unittest.skipUnless(verbose, 'requires verbose mode')
1003 def test_exceptions(self):
1004 try:
1005 x = math.exp(-1000000000)
1006 except:
1007 # mathmodule.c is failing to weed out underflows from libm, or
1008 # we've got an fp format with huge dynamic range
1009 self.fail("underflowing exp() should not have raised "
1010 "an exception")
1011 if x != 0:
1012 self.fail("underflowing exp() should have returned 0")
Tim Peters98c81842000-10-16 17:35:13 +00001013
Serhiy Storchaka43767632013-11-03 21:31:38 +02001014 # If this fails, probably using a strict IEEE-754 conforming libm, and x
1015 # is +Inf afterwards. But Python wants overflows detected by default.
1016 try:
1017 x = math.exp(1000000000)
1018 except OverflowError:
1019 pass
1020 else:
1021 self.fail("overflowing exp() didn't trigger OverflowError")
Thomas Wouters89f507f2006-12-13 04:49:30 +00001022
Serhiy Storchaka43767632013-11-03 21:31:38 +02001023 # If this fails, it could be a puzzle. One odd possibility is that
1024 # mathmodule.c's macros are getting confused while comparing
1025 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
1026 # as a result (and so raising OverflowError instead).
1027 try:
1028 x = math.sqrt(-1.0)
1029 except ValueError:
1030 pass
1031 else:
1032 self.fail("sqrt(-1) didn't raise ValueError")
Thomas Wouters89f507f2006-12-13 04:49:30 +00001033
Mark Dickinson63566232009-09-18 21:04:19 +00001034 @requires_IEEE_754
Christian Heimes53876d92008-04-19 00:31:39 +00001035 def test_testfile(self):
Christian Heimes53876d92008-04-19 00:31:39 +00001036 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
1037 # Skip if either the input or result is complex, or if
1038 # flags is nonempty
1039 if ai != 0. or ei != 0. or flags:
1040 continue
1041 if fn in ['rect', 'polar']:
1042 # no real versions of rect, polar
1043 continue
1044 func = getattr(math, fn)
Christian Heimesa342c012008-04-20 21:01:16 +00001045 try:
1046 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +00001047 except ValueError as exc:
1048 message = (("Unexpected ValueError: %s\n " +
1049 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +00001050 self.fail(message)
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001051 except OverflowError:
1052 message = ("Unexpected OverflowError in " +
1053 "test %s:%s(%r)\n" % (id, fn, ar))
1054 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +00001055 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001056
Victor Stinnerbe3da382010-11-07 14:14:27 +00001057 @requires_IEEE_754
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001058 def test_mtestfile(self):
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001059 fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}"
1060
1061 failures = []
1062 for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
1063 func = getattr(math, fn)
1064
1065 if 'invalid' in flags or 'divide-by-zero' in flags:
1066 expected = 'ValueError'
1067 elif 'overflow' in flags:
1068 expected = 'OverflowError'
1069
1070 try:
1071 got = func(arg)
1072 except ValueError:
1073 got = 'ValueError'
1074 except OverflowError:
1075 got = 'OverflowError'
1076
Mark Dickinson05d2e082009-12-11 20:17:17 +00001077 accuracy_failure = None
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001078 if isinstance(got, float) and isinstance(expected, float):
1079 if math.isnan(expected) and math.isnan(got):
1080 continue
1081 if not math.isnan(expected) and not math.isnan(got):
Mark Dickinson664b5112009-12-16 20:23:42 +00001082 if fn == 'lgamma':
1083 # we use a weaker accuracy test for lgamma;
1084 # lgamma only achieves an absolute error of
1085 # a few multiples of the machine accuracy, in
1086 # general.
Mark Dickinson05d2e082009-12-11 20:17:17 +00001087 accuracy_failure = acc_check(expected, got,
1088 rel_err = 5e-15,
1089 abs_err = 5e-15)
Mark Dickinsonbcdf9da2010-06-13 10:52:38 +00001090 elif fn == 'erfc':
1091 # erfc has less-than-ideal accuracy for large
1092 # arguments (x ~ 25 or so), mainly due to the
1093 # error involved in computing exp(-x*x).
1094 #
1095 # XXX Would be better to weaken this test only
1096 # for large x, instead of for all x.
1097 accuracy_failure = ulps_check(expected, got, 2000)
1098
Mark Dickinson05d2e082009-12-11 20:17:17 +00001099 else:
Mark Dickinson664b5112009-12-16 20:23:42 +00001100 accuracy_failure = ulps_check(expected, got, 20)
Mark Dickinson05d2e082009-12-11 20:17:17 +00001101 if accuracy_failure is None:
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001102 continue
1103
1104 if isinstance(got, str) and isinstance(expected, str):
1105 if got == expected:
1106 continue
1107
1108 fail_msg = fail_fmt.format(id, fn, arg, expected, got)
Mark Dickinson05d2e082009-12-11 20:17:17 +00001109 if accuracy_failure is not None:
1110 fail_msg += ' ({})'.format(accuracy_failure)
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001111 failures.append(fail_msg)
1112
1113 if failures:
1114 self.fail('Failures in test_mtestfile:\n ' +
1115 '\n '.join(failures))
1116
1117
Thomas Wouters89f507f2006-12-13 04:49:30 +00001118def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +00001119 from doctest import DocFileSuite
1120 suite = unittest.TestSuite()
1121 suite.addTest(unittest.makeSuite(MathTests))
1122 suite.addTest(DocFileSuite("ieee754.txt"))
1123 run_unittest(suite)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001124
1125if __name__ == '__main__':
1126 test_main()