blob: 9f5fdbab5bd58117f115b0127c0dfa14b3d1da14 [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
Georg Brandl2f037602006-10-28 13:51:49 +00004from test.test_support import run_unittest, verbose
5import unittest
6import math
Christian Heimes6f341092008-04-18 23:13:07 +00007import os
8import sys
Raymond Hettingerecbdd2e2008-06-09 06:54:45 +00009import random
Guido van Rossumfcce6301996-08-08 18:26:25 +000010
Christian Heimes6f341092008-04-18 23:13:07 +000011eps = 1E-05
12NAN = float('nan')
13INF = float('inf')
14NINF = float('-inf')
15
16# locate file with test values
17if __name__ == '__main__':
18 file = sys.argv[0]
19else:
20 file = __file__
21test_dir = os.path.dirname(file) or os.curdir
22test_file = os.path.join(test_dir, 'cmath_testcases.txt')
23
24def parse_testfile(fname):
25 """Parse a file with test values
26
27 Empty lines or lines starting with -- are ignored
28 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
29 """
30 with open(fname) as fp:
31 for line in fp:
32 # skip comment lines and blank lines
33 if line.startswith('--') or not line.strip():
34 continue
35
36 lhs, rhs = line.split('->')
37 id, fn, arg_real, arg_imag = lhs.split()
38 rhs_pieces = rhs.split()
39 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
40 flags = rhs_pieces[2:]
41
42 yield (id, fn,
43 float(arg_real), float(arg_imag),
44 float(exp_real), float(exp_imag),
45 flags
46 )
Guido van Rossumfcce6301996-08-08 18:26:25 +000047
Georg Brandl2f037602006-10-28 13:51:49 +000048class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000049
Georg Brandl2f037602006-10-28 13:51:49 +000050 def ftest(self, name, value, expected):
51 if abs(value-expected) > eps:
Nick Coghlanec2ce9b2007-07-27 10:36:30 +000052 # Use %r instead of %f so the error message
53 # displays full precision. Otherwise discrepancies
54 # in the last few bits will lead to very confusing
55 # error messages
56 self.fail('%s returned %r, expected %r' %
Georg Brandl2f037602006-10-28 13:51:49 +000057 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000058
Georg Brandl2f037602006-10-28 13:51:49 +000059 def testConstants(self):
60 self.ftest('pi', math.pi, 3.1415926)
61 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000062
Georg Brandl2f037602006-10-28 13:51:49 +000063 def testAcos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000064 self.assertRaises(TypeError, math.acos)
Georg Brandl2f037602006-10-28 13:51:49 +000065 self.ftest('acos(-1)', math.acos(-1), math.pi)
66 self.ftest('acos(0)', math.acos(0), math.pi/2)
67 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +000068 self.assertRaises(ValueError, math.acos, INF)
69 self.assertRaises(ValueError, math.acos, NINF)
70 self.assert_(math.isnan(math.acos(NAN)))
71
72 def testAcosh(self):
73 self.assertRaises(TypeError, math.acosh)
74 self.ftest('acosh(1)', math.acosh(1), 0)
75 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
76 self.assertRaises(ValueError, math.acosh, 0)
77 self.assertRaises(ValueError, math.acosh, -1)
78 self.assertEquals(math.acosh(INF), INF)
79 self.assertRaises(ValueError, math.acosh, NINF)
80 self.assert_(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000081
Georg Brandl2f037602006-10-28 13:51:49 +000082 def testAsin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000083 self.assertRaises(TypeError, math.asin)
Georg Brandl2f037602006-10-28 13:51:49 +000084 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
85 self.ftest('asin(0)', math.asin(0), 0)
86 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes6f341092008-04-18 23:13:07 +000087 self.assertRaises(ValueError, math.asin, INF)
88 self.assertRaises(ValueError, math.asin, NINF)
89 self.assert_(math.isnan(math.asin(NAN)))
90
91 def testAsinh(self):
92 self.assertRaises(TypeError, math.asinh)
93 self.ftest('asinh(0)', math.asinh(0), 0)
94 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
95 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
96 self.assertEquals(math.asinh(INF), INF)
97 self.assertEquals(math.asinh(NINF), NINF)
98 self.assert_(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000099
Georg Brandl2f037602006-10-28 13:51:49 +0000100 def testAtan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000101 self.assertRaises(TypeError, math.atan)
Georg Brandl2f037602006-10-28 13:51:49 +0000102 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
103 self.ftest('atan(0)', math.atan(0), 0)
104 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes6f341092008-04-18 23:13:07 +0000105 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Mark Dickinsone941d972008-04-19 18:51:48 +0000106 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Christian Heimes6f341092008-04-18 23:13:07 +0000107 self.assert_(math.isnan(math.atan(NAN)))
108
109 def testAtanh(self):
110 self.assertRaises(TypeError, math.atan)
111 self.ftest('atanh(0)', math.atanh(0), 0)
112 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
113 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
114 self.assertRaises(ValueError, math.atanh, 1)
115 self.assertRaises(ValueError, math.atanh, -1)
116 self.assertRaises(ValueError, math.atanh, INF)
117 self.assertRaises(ValueError, math.atanh, NINF)
118 self.assert_(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000119
Georg Brandl2f037602006-10-28 13:51:49 +0000120 def testAtan2(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000121 self.assertRaises(TypeError, math.atan2)
Georg Brandl2f037602006-10-28 13:51:49 +0000122 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
123 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
124 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
125 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
126 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000127
Mark Dickinsond6d51482008-04-20 20:38:48 +0000128 # math.atan2(0, x)
129 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
130 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
131 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
132 self.assertEqual(math.atan2(0., 0.), 0.)
133 self.assertEqual(math.atan2(0., 2.3), 0.)
134 self.assertEqual(math.atan2(0., INF), 0.)
135 self.assert_(math.isnan(math.atan2(0., NAN)))
136 # math.atan2(-0, x)
137 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
138 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
139 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
140 self.assertEqual(math.atan2(-0., 0.), -0.)
141 self.assertEqual(math.atan2(-0., 2.3), -0.)
142 self.assertEqual(math.atan2(-0., INF), -0.)
143 self.assert_(math.isnan(math.atan2(-0., NAN)))
144 # math.atan2(INF, x)
145 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
146 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
147 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
148 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
149 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
150 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
151 self.assert_(math.isnan(math.atan2(INF, NAN)))
152 # math.atan2(NINF, x)
153 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
154 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
155 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
156 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
157 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
158 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
159 self.assert_(math.isnan(math.atan2(NINF, NAN)))
160 # math.atan2(+finite, x)
161 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
162 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
163 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
164 self.assertEqual(math.atan2(2.3, INF), 0.)
165 self.assert_(math.isnan(math.atan2(2.3, NAN)))
166 # math.atan2(-finite, x)
167 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
168 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
169 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
170 self.assertEqual(math.atan2(-2.3, INF), -0.)
171 self.assert_(math.isnan(math.atan2(-2.3, NAN)))
172 # math.atan2(NAN, x)
173 self.assert_(math.isnan(math.atan2(NAN, NINF)))
174 self.assert_(math.isnan(math.atan2(NAN, -2.3)))
175 self.assert_(math.isnan(math.atan2(NAN, -0.)))
176 self.assert_(math.isnan(math.atan2(NAN, 0.)))
177 self.assert_(math.isnan(math.atan2(NAN, 2.3)))
178 self.assert_(math.isnan(math.atan2(NAN, INF)))
179 self.assert_(math.isnan(math.atan2(NAN, NAN)))
180
Georg Brandl2f037602006-10-28 13:51:49 +0000181 def testCeil(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000182 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000183 # These types will be int in py3k.
184 self.assertEquals(float, type(math.ceil(1)))
185 self.assertEquals(float, type(math.ceil(1L)))
186 self.assertEquals(float, type(math.ceil(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000187 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
188 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
189 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
190 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
191 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
192 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000193 self.assertEquals(math.ceil(INF), INF)
194 self.assertEquals(math.ceil(NINF), NINF)
195 self.assert_(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000196
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000197 class TestCeil(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000198 def __float__(self):
199 return 41.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000200 class TestNoCeil(object):
201 pass
202 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
203 self.assertRaises(TypeError, math.ceil, TestNoCeil())
204
205 t = TestNoCeil()
206 t.__ceil__ = lambda *args: args
207 self.assertRaises(TypeError, math.ceil, t)
208 self.assertRaises(TypeError, math.ceil, t, 0)
209
Christian Heimes6f341092008-04-18 23:13:07 +0000210 if float.__getformat__("double").startswith("IEEE"):
211 def testCopysign(self):
212 self.assertRaises(TypeError, math.copysign)
213 # copysign should let us distinguish signs of zeros
214 self.assertEquals(copysign(1., 0.), 1.)
215 self.assertEquals(copysign(1., -0.), -1.)
216 self.assertEquals(copysign(INF, 0.), INF)
217 self.assertEquals(copysign(INF, -0.), NINF)
218 self.assertEquals(copysign(NINF, 0.), INF)
219 self.assertEquals(copysign(NINF, -0.), NINF)
220 # and of infinities
221 self.assertEquals(copysign(1., INF), 1.)
222 self.assertEquals(copysign(1., NINF), -1.)
223 self.assertEquals(copysign(INF, INF), INF)
224 self.assertEquals(copysign(INF, NINF), NINF)
225 self.assertEquals(copysign(NINF, INF), INF)
226 self.assertEquals(copysign(NINF, NINF), NINF)
227 self.assert_(math.isnan(copysign(NAN, 1.)))
228 self.assert_(math.isnan(copysign(NAN, INF)))
229 self.assert_(math.isnan(copysign(NAN, NINF)))
230 self.assert_(math.isnan(copysign(NAN, NAN)))
231 # copysign(INF, NAN) may be INF or it may be NINF, since
232 # we don't know whether the sign bit of NAN is set on any
233 # given platform.
234 self.assert_(math.isinf(copysign(INF, NAN)))
235 # similarly, copysign(2., NAN) could be 2. or -2.
236 self.assertEquals(abs(copysign(2., NAN)), 2.)
237
Georg Brandl2f037602006-10-28 13:51:49 +0000238 def testCos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000239 self.assertRaises(TypeError, math.cos)
Georg Brandl2f037602006-10-28 13:51:49 +0000240 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
241 self.ftest('cos(0)', math.cos(0), 1)
242 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
243 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000244 try:
245 self.assert_(math.isnan(math.cos(INF)))
246 self.assert_(math.isnan(math.cos(NINF)))
247 except ValueError:
248 self.assertRaises(ValueError, math.cos, INF)
249 self.assertRaises(ValueError, math.cos, NINF)
250 self.assert_(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000251
Georg Brandl2f037602006-10-28 13:51:49 +0000252 def testCosh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000253 self.assertRaises(TypeError, math.cosh)
Georg Brandl2f037602006-10-28 13:51:49 +0000254 self.ftest('cosh(0)', math.cosh(0), 1)
255 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
Christian Heimes6f341092008-04-18 23:13:07 +0000256 self.assertEquals(math.cosh(INF), INF)
257 self.assertEquals(math.cosh(NINF), INF)
258 self.assert_(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000259
Georg Brandl2f037602006-10-28 13:51:49 +0000260 def testDegrees(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000261 self.assertRaises(TypeError, math.degrees)
Georg Brandl2f037602006-10-28 13:51:49 +0000262 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
263 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
264 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000265
Georg Brandl2f037602006-10-28 13:51:49 +0000266 def testExp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000267 self.assertRaises(TypeError, math.exp)
Georg Brandl2f037602006-10-28 13:51:49 +0000268 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
269 self.ftest('exp(0)', math.exp(0), 1)
270 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes6f341092008-04-18 23:13:07 +0000271 self.assertEquals(math.exp(INF), INF)
272 self.assertEquals(math.exp(NINF), 0.)
273 self.assert_(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000274
Georg Brandl2f037602006-10-28 13:51:49 +0000275 def testFabs(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000276 self.assertRaises(TypeError, math.fabs)
Georg Brandl2f037602006-10-28 13:51:49 +0000277 self.ftest('fabs(-1)', math.fabs(-1), 1)
278 self.ftest('fabs(0)', math.fabs(0), 0)
279 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000280
Raymond Hettingerecbdd2e2008-06-09 06:54:45 +0000281 def testFactorial(self):
282 def fact(n):
283 result = 1
284 for i in range(1, int(n)+1):
285 result *= i
286 return result
287 values = range(10) + [50, 100, 500]
288 random.shuffle(values)
289 for x in range(10):
290 for cast in (int, long, float):
291 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
292 self.assertRaises(ValueError, math.factorial, -1)
293 self.assertRaises(ValueError, math.factorial, math.pi)
294
Georg Brandl2f037602006-10-28 13:51:49 +0000295 def testFloor(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000296 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000297 # These types will be int in py3k.
298 self.assertEquals(float, type(math.floor(1)))
299 self.assertEquals(float, type(math.floor(1L)))
300 self.assertEquals(float, type(math.floor(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000301 self.ftest('floor(0.5)', math.floor(0.5), 0)
302 self.ftest('floor(1.0)', math.floor(1.0), 1)
303 self.ftest('floor(1.5)', math.floor(1.5), 1)
304 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
305 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
306 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Nick Coghlan00f20292007-07-26 14:03:00 +0000307 # pow() relies on floor() to check for integers
308 # This fails on some platforms - so check it here
309 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
310 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes6f341092008-04-18 23:13:07 +0000311 self.assertEquals(math.ceil(INF), INF)
312 self.assertEquals(math.ceil(NINF), NINF)
313 self.assert_(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000314
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000315 class TestFloor(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000316 def __float__(self):
317 return 42.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000318 class TestNoFloor(object):
319 pass
320 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
321 self.assertRaises(TypeError, math.floor, TestNoFloor())
322
323 t = TestNoFloor()
324 t.__floor__ = lambda *args: args
325 self.assertRaises(TypeError, math.floor, t)
326 self.assertRaises(TypeError, math.floor, t, 0)
327
Georg Brandl2f037602006-10-28 13:51:49 +0000328 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000329 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +0000330 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
331 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
332 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
333 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
334 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
335 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000336 self.assert_(math.isnan(math.fmod(NAN, 1.)))
337 self.assert_(math.isnan(math.fmod(1., NAN)))
338 self.assert_(math.isnan(math.fmod(NAN, NAN)))
339 self.assertRaises(ValueError, math.fmod, 1., 0.)
340 self.assertRaises(ValueError, math.fmod, INF, 1.)
341 self.assertRaises(ValueError, math.fmod, NINF, 1.)
342 self.assertRaises(ValueError, math.fmod, INF, 0.)
343 self.assertEquals(math.fmod(3.0, INF), 3.0)
344 self.assertEquals(math.fmod(-3.0, INF), -3.0)
345 self.assertEquals(math.fmod(3.0, NINF), 3.0)
346 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
347 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
348 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000349
Georg Brandl2f037602006-10-28 13:51:49 +0000350 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000351 self.assertRaises(TypeError, math.frexp)
352
Georg Brandl2f037602006-10-28 13:51:49 +0000353 def testfrexp(name, (mant, exp), (emant, eexp)):
354 if abs(mant-emant) > eps or exp != eexp:
355 self.fail('%s returned %r, expected %r'%\
356 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000357
Georg Brandl2f037602006-10-28 13:51:49 +0000358 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
359 testfrexp('frexp(0)', math.frexp(0), (0, 0))
360 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
361 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000362
Christian Heimes6f341092008-04-18 23:13:07 +0000363 self.assertEquals(math.frexp(INF)[0], INF)
364 self.assertEquals(math.frexp(NINF)[0], NINF)
365 self.assert_(math.isnan(math.frexp(NAN)[0]))
366
Georg Brandl2f037602006-10-28 13:51:49 +0000367 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000368 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000369 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
370 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes6f341092008-04-18 23:13:07 +0000371 self.assertEqual(math.hypot(NAN, INF), INF)
372 self.assertEqual(math.hypot(INF, NAN), INF)
373 self.assertEqual(math.hypot(NAN, NINF), INF)
374 self.assertEqual(math.hypot(NINF, NAN), INF)
375 self.assert_(math.isnan(math.hypot(1.0, NAN)))
376 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000377
Georg Brandl2f037602006-10-28 13:51:49 +0000378 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000379 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000380 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
381 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
382 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
383 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes6f341092008-04-18 23:13:07 +0000384 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
385 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
386 self.assertEquals(math.ldexp(1., -1000000), 0.)
387 self.assertEquals(math.ldexp(-1., -1000000), -0.)
388 self.assertEquals(math.ldexp(INF, 30), INF)
389 self.assertEquals(math.ldexp(NINF, -213), NINF)
390 self.assert_(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000391
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000392 # large second argument
393 for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
394 self.assertEquals(math.ldexp(INF, -n), INF)
395 self.assertEquals(math.ldexp(NINF, -n), NINF)
396 self.assertEquals(math.ldexp(1., -n), 0.)
397 self.assertEquals(math.ldexp(-1., -n), -0.)
398 self.assertEquals(math.ldexp(0., -n), 0.)
399 self.assertEquals(math.ldexp(-0., -n), -0.)
400 self.assert_(math.isnan(math.ldexp(NAN, -n)))
401
402 self.assertRaises(OverflowError, math.ldexp, 1., n)
403 self.assertRaises(OverflowError, math.ldexp, -1., n)
404 self.assertEquals(math.ldexp(0., n), 0.)
405 self.assertEquals(math.ldexp(-0., n), -0.)
406 self.assertEquals(math.ldexp(INF, n), INF)
407 self.assertEquals(math.ldexp(NINF, n), NINF)
408 self.assert_(math.isnan(math.ldexp(NAN, n)))
409
Georg Brandl2f037602006-10-28 13:51:49 +0000410 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000411 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000412 self.ftest('log(1/e)', math.log(1/math.e), -1)
413 self.ftest('log(1)', math.log(1), 0)
414 self.ftest('log(e)', math.log(math.e), 1)
415 self.ftest('log(32,2)', math.log(32,2), 5)
416 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
417 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000418 self.assertEquals(math.log(INF), INF)
419 self.assertRaises(ValueError, math.log, NINF)
420 self.assert_(math.isnan(math.log(NAN)))
421
422 def testLog1p(self):
423 self.assertRaises(TypeError, math.log1p)
424 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
425 self.ftest('log1p(0)', math.log1p(0), 0)
426 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
427 self.ftest('log1p(1)', math.log1p(1), math.log(2))
428 self.assertEquals(math.log1p(INF), INF)
429 self.assertRaises(ValueError, math.log1p, NINF)
430 self.assert_(math.isnan(math.log1p(NAN)))
431 n= 2**90
432 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
433 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000434
Georg Brandl2f037602006-10-28 13:51:49 +0000435 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000436 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000437 self.ftest('log10(0.1)', math.log10(0.1), -1)
438 self.ftest('log10(1)', math.log10(1), 0)
439 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes6f341092008-04-18 23:13:07 +0000440 self.assertEquals(math.log(INF), INF)
441 self.assertRaises(ValueError, math.log10, NINF)
442 self.assert_(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000443
Georg Brandl2f037602006-10-28 13:51:49 +0000444 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000445 self.assertRaises(TypeError, math.modf)
446
Georg Brandl2f037602006-10-28 13:51:49 +0000447 def testmodf(name, (v1, v2), (e1, e2)):
448 if abs(v1-e1) > eps or abs(v2-e2):
449 self.fail('%s returned %r, expected %r'%\
450 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000451
Georg Brandl2f037602006-10-28 13:51:49 +0000452 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
453 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Tim Petersabd8a332006-11-03 02:32:46 +0000454
Christian Heimes6f341092008-04-18 23:13:07 +0000455 self.assertEquals(math.modf(INF), (0.0, INF))
456 self.assertEquals(math.modf(NINF), (-0.0, NINF))
457
458 modf_nan = math.modf(NAN)
459 self.assert_(math.isnan(modf_nan[0]))
460 self.assert_(math.isnan(modf_nan[1]))
461
Georg Brandl2f037602006-10-28 13:51:49 +0000462 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000463 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000464 self.ftest('pow(0,1)', math.pow(0,1), 0)
465 self.ftest('pow(1,0)', math.pow(1,0), 1)
466 self.ftest('pow(2,1)', math.pow(2,1), 2)
467 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes6f341092008-04-18 23:13:07 +0000468 self.assertEqual(math.pow(INF, 1), INF)
469 self.assertEqual(math.pow(NINF, 1), NINF)
470 self.assertEqual((math.pow(1, INF)), 1.)
471 self.assertEqual((math.pow(1, NINF)), 1.)
472 self.assert_(math.isnan(math.pow(NAN, 1)))
473 self.assert_(math.isnan(math.pow(2, NAN)))
474 self.assert_(math.isnan(math.pow(0, NAN)))
475 self.assertEqual(math.pow(1, NAN), 1)
Mark Dickinsone941d972008-04-19 18:51:48 +0000476
477 # pow(0., x)
478 self.assertEqual(math.pow(0., INF), 0.)
479 self.assertEqual(math.pow(0., 3.), 0.)
480 self.assertEqual(math.pow(0., 2.3), 0.)
481 self.assertEqual(math.pow(0., 2.), 0.)
482 self.assertEqual(math.pow(0., 0.), 1.)
483 self.assertEqual(math.pow(0., -0.), 1.)
484 self.assertRaises(ValueError, math.pow, 0., -2.)
485 self.assertRaises(ValueError, math.pow, 0., -2.3)
486 self.assertRaises(ValueError, math.pow, 0., -3.)
487 self.assertRaises(ValueError, math.pow, 0., NINF)
488 self.assert_(math.isnan(math.pow(0., NAN)))
489
490 # pow(INF, x)
491 self.assertEqual(math.pow(INF, INF), INF)
492 self.assertEqual(math.pow(INF, 3.), INF)
493 self.assertEqual(math.pow(INF, 2.3), INF)
494 self.assertEqual(math.pow(INF, 2.), INF)
495 self.assertEqual(math.pow(INF, 0.), 1.)
496 self.assertEqual(math.pow(INF, -0.), 1.)
497 self.assertEqual(math.pow(INF, -2.), 0.)
498 self.assertEqual(math.pow(INF, -2.3), 0.)
499 self.assertEqual(math.pow(INF, -3.), 0.)
500 self.assertEqual(math.pow(INF, NINF), 0.)
501 self.assert_(math.isnan(math.pow(INF, NAN)))
502
503 # pow(-0., x)
504 self.assertEqual(math.pow(-0., INF), 0.)
505 self.assertEqual(math.pow(-0., 3.), -0.)
506 self.assertEqual(math.pow(-0., 2.3), 0.)
507 self.assertEqual(math.pow(-0., 2.), 0.)
508 self.assertEqual(math.pow(-0., 0.), 1.)
509 self.assertEqual(math.pow(-0., -0.), 1.)
510 self.assertRaises(ValueError, math.pow, -0., -2.)
511 self.assertRaises(ValueError, math.pow, -0., -2.3)
512 self.assertRaises(ValueError, math.pow, -0., -3.)
513 self.assertRaises(ValueError, math.pow, -0., NINF)
514 self.assert_(math.isnan(math.pow(-0., NAN)))
515
516 # pow(NINF, x)
517 self.assertEqual(math.pow(NINF, INF), INF)
518 self.assertEqual(math.pow(NINF, 3.), NINF)
519 self.assertEqual(math.pow(NINF, 2.3), INF)
520 self.assertEqual(math.pow(NINF, 2.), INF)
521 self.assertEqual(math.pow(NINF, 0.), 1.)
522 self.assertEqual(math.pow(NINF, -0.), 1.)
523 self.assertEqual(math.pow(NINF, -2.), 0.)
524 self.assertEqual(math.pow(NINF, -2.3), 0.)
525 self.assertEqual(math.pow(NINF, -3.), -0.)
526 self.assertEqual(math.pow(NINF, NINF), 0.)
527 self.assert_(math.isnan(math.pow(NINF, NAN)))
528
529 # pow(-1, x)
530 self.assertEqual(math.pow(-1., INF), 1.)
531 self.assertEqual(math.pow(-1., 3.), -1.)
532 self.assertRaises(ValueError, math.pow, -1., 2.3)
533 self.assertEqual(math.pow(-1., 2.), 1.)
534 self.assertEqual(math.pow(-1., 0.), 1.)
535 self.assertEqual(math.pow(-1., -0.), 1.)
536 self.assertEqual(math.pow(-1., -2.), 1.)
537 self.assertRaises(ValueError, math.pow, -1., -2.3)
538 self.assertEqual(math.pow(-1., -3.), -1.)
539 self.assertEqual(math.pow(-1., NINF), 1.)
540 self.assert_(math.isnan(math.pow(-1., NAN)))
541
542 # pow(1, x)
543 self.assertEqual(math.pow(1., INF), 1.)
544 self.assertEqual(math.pow(1., 3.), 1.)
545 self.assertEqual(math.pow(1., 2.3), 1.)
546 self.assertEqual(math.pow(1., 2.), 1.)
547 self.assertEqual(math.pow(1., 0.), 1.)
548 self.assertEqual(math.pow(1., -0.), 1.)
549 self.assertEqual(math.pow(1., -2.), 1.)
550 self.assertEqual(math.pow(1., -2.3), 1.)
551 self.assertEqual(math.pow(1., -3.), 1.)
552 self.assertEqual(math.pow(1., NINF), 1.)
553 self.assertEqual(math.pow(1., NAN), 1.)
554
555 # pow(x, 0) should be 1 for any x
556 self.assertEqual(math.pow(2.3, 0.), 1.)
557 self.assertEqual(math.pow(-2.3, 0.), 1.)
558 self.assertEqual(math.pow(NAN, 0.), 1.)
559 self.assertEqual(math.pow(2.3, -0.), 1.)
560 self.assertEqual(math.pow(-2.3, -0.), 1.)
561 self.assertEqual(math.pow(NAN, -0.), 1.)
562
563 # pow(x, y) is invalid if x is negative and y is not integral
564 self.assertRaises(ValueError, math.pow, -1., 2.3)
565 self.assertRaises(ValueError, math.pow, -15., -3.1)
566
567 # pow(x, NINF)
568 self.assertEqual(math.pow(1.9, NINF), 0.)
569 self.assertEqual(math.pow(1.1, NINF), 0.)
570 self.assertEqual(math.pow(0.9, NINF), INF)
571 self.assertEqual(math.pow(0.1, NINF), INF)
572 self.assertEqual(math.pow(-0.1, NINF), INF)
573 self.assertEqual(math.pow(-0.9, NINF), INF)
574 self.assertEqual(math.pow(-1.1, NINF), 0.)
575 self.assertEqual(math.pow(-1.9, NINF), 0.)
576
577 # pow(x, INF)
578 self.assertEqual(math.pow(1.9, INF), INF)
579 self.assertEqual(math.pow(1.1, INF), INF)
580 self.assertEqual(math.pow(0.9, INF), 0.)
581 self.assertEqual(math.pow(0.1, INF), 0.)
582 self.assertEqual(math.pow(-0.1, INF), 0.)
583 self.assertEqual(math.pow(-0.9, INF), 0.)
584 self.assertEqual(math.pow(-1.1, INF), INF)
585 self.assertEqual(math.pow(-1.9, INF), INF)
586
Mark Dickinsoncec3f132008-04-20 04:13:13 +0000587 # pow(x, y) should work for x negative, y an integer
588 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
589 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
590 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
591 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
592 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
593 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
594 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
595 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
596 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
597 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
598
Mark Dickinsone941d972008-04-19 18:51:48 +0000599 # the following tests have been commented out since they don't
600 # really belong here: the implementation of ** for floats is
601 # independent of the implemention of math.pow
602 #self.assertEqual(1**NAN, 1)
603 #self.assertEqual(1**INF, 1)
604 #self.assertEqual(1**NINF, 1)
605 #self.assertEqual(1**0, 1)
606 #self.assertEqual(1.**NAN, 1)
607 #self.assertEqual(1.**INF, 1)
608 #self.assertEqual(1.**NINF, 1)
609 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000610
Georg Brandl2f037602006-10-28 13:51:49 +0000611 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000612 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000613 self.ftest('radians(180)', math.radians(180), math.pi)
614 self.ftest('radians(90)', math.radians(90), math.pi/2)
615 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000616
Georg Brandl2f037602006-10-28 13:51:49 +0000617 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000618 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000619 self.ftest('sin(0)', math.sin(0), 0)
620 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
621 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000622 try:
623 self.assert_(math.isnan(math.sin(INF)))
624 self.assert_(math.isnan(math.sin(NINF)))
625 except ValueError:
626 self.assertRaises(ValueError, math.sin, INF)
627 self.assertRaises(ValueError, math.sin, NINF)
628 self.assert_(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000629
Georg Brandl2f037602006-10-28 13:51:49 +0000630 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000631 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000632 self.ftest('sinh(0)', math.sinh(0), 0)
633 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
634 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000635 self.assertEquals(math.sinh(INF), INF)
Mark Dickinsone941d972008-04-19 18:51:48 +0000636 self.assertEquals(math.sinh(NINF), NINF)
Christian Heimes6f341092008-04-18 23:13:07 +0000637 self.assert_(math.isnan(math.sinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000638
Georg Brandl2f037602006-10-28 13:51:49 +0000639 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000640 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000641 self.ftest('sqrt(0)', math.sqrt(0), 0)
642 self.ftest('sqrt(1)', math.sqrt(1), 1)
643 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000644 self.assertEquals(math.sqrt(INF), INF)
645 self.assertRaises(ValueError, math.sqrt, NINF)
646 self.assert_(math.isnan(math.sqrt(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000647
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000648 def testSum(self):
Mark Dickinsonfef6b132008-07-30 16:20:10 +0000649 # math.fsum relies on exact rounding for correct operation.
Mark Dickinsonbadd7da2008-05-23 12:07:36 +0000650 # There's a known problem with IA32 floating-point that causes
651 # inexact rounding in some situations, and will cause the
Mark Dickinsonfef6b132008-07-30 16:20:10 +0000652 # math.fsum tests below to fail; see issue #2937. On non IEEE
Mark Dickinsonbadd7da2008-05-23 12:07:36 +0000653 # 754 platforms, and on IEEE 754 platforms that exhibit the
654 # problem described in issue #2937, we simply skip the whole
655 # test.
656
657 if not float.__getformat__("double").startswith("IEEE"):
658 return
659
660 # on IEEE 754 compliant machines, both of the expressions
661 # below should round to 10000000000000002.0.
Mark Dickinsonff5f16e2008-07-29 18:45:38 +0000662 if 1e16+2.0 != 1e16+2.9999:
Mark Dickinsonbadd7da2008-05-23 12:07:36 +0000663 return
664
Mark Dickinsonfef6b132008-07-30 16:20:10 +0000665 # Python version of math.fsum, for comparison. Uses a
Mark Dickinsonff5f16e2008-07-29 18:45:38 +0000666 # different algorithm based on frexp, ldexp and integer
667 # arithmetic.
668 from sys import float_info
669 mant_dig = float_info.mant_dig
670 etiny = float_info.min_exp - mant_dig
671
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000672 def msum(iterable):
Mark Dickinsonff5f16e2008-07-29 18:45:38 +0000673 """Full precision summation. Compute sum(iterable) without any
674 intermediate accumulation of error. Based on the 'lsum' function
675 at http://code.activestate.com/recipes/393090/
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000676
677 """
Mark Dickinsonff5f16e2008-07-29 18:45:38 +0000678 tmant, texp = 0, 0
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000679 for x in iterable:
Mark Dickinsonff5f16e2008-07-29 18:45:38 +0000680 mant, exp = math.frexp(x)
681 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
682 if texp > exp:
683 tmant <<= texp-exp
684 texp = exp
685 else:
686 mant <<= exp-texp
687 tmant += mant
688 # Round tmant * 2**texp to a float. The original recipe
689 # used float(str(tmant)) * 2.0**texp for this, but that's
690 # a little unsafe because str -> float conversion can't be
691 # relied upon to do correct rounding on all platforms.
692 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
693 if tail > 0:
694 h = 1 << (tail-1)
695 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
696 texp += tail
697 return math.ldexp(tmant, texp)
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000698
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000699 test_values = [
700 ([], 0.0),
701 ([0.0], 0.0),
702 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000703 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
704 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
705 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000706 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
Mark Dickinsonff5f16e2008-07-29 18:45:38 +0000707 ([1./n for n in range(1, 1001)],
708 float.fromhex('0x1.df11f45f4e61ap+2')),
709 ([(-1.)**n/n for n in range(1, 1001)],
710 float.fromhex('-0x1.62a2af1bd3624p-1')),
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000711 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000712 ([1e16, 1., 1e-16], 10000000000000002.0),
Mark Dickinson8df4e222008-05-30 02:46:53 +0000713 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
Mark Dickinsonff5f16e2008-07-29 18:45:38 +0000714 # exercise code for resizing partials array
715 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
716 [-2.**1022],
717 float.fromhex('0x1.5555555555555p+970')),
718 ]
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000719
Mark Dickinson3e9c60c2008-07-27 07:15:29 +0000720 for i, (vals, expected) in enumerate(test_values):
721 try:
Mark Dickinsonfef6b132008-07-30 16:20:10 +0000722 actual = math.fsum(vals)
Mark Dickinson3e9c60c2008-07-27 07:15:29 +0000723 except OverflowError:
724 self.fail("test %d failed: got OverflowError, expected %r "
Mark Dickinsonfef6b132008-07-30 16:20:10 +0000725 "for math.fsum(%.100r)" % (i, expected, vals))
Mark Dickinson3e9c60c2008-07-27 07:15:29 +0000726 except ValueError:
727 self.fail("test %d failed: got ValueError, expected %r "
Mark Dickinsonfef6b132008-07-30 16:20:10 +0000728 "for math.fsum(%.100r)" % (i, expected, vals))
Mark Dickinson3e9c60c2008-07-27 07:15:29 +0000729 self.assertEqual(actual, expected)
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000730
731 from random import random, gauss, shuffle
732 for j in xrange(1000):
733 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
734 s = 0
735 for i in xrange(200):
736 v = gauss(0, random()) ** 7 - s
737 s += v
738 vals.append(v)
739 shuffle(vals)
740
741 s = msum(vals)
Mark Dickinsonfef6b132008-07-30 16:20:10 +0000742 self.assertEqual(msum(vals), math.fsum(vals))
Mark Dickinsonc11c3392008-05-23 02:36:48 +0000743
744
Georg Brandl2f037602006-10-28 13:51:49 +0000745 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000746 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000747 self.ftest('tan(0)', math.tan(0), 0)
748 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
749 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000750 try:
751 self.assert_(math.isnan(math.tan(INF)))
752 self.assert_(math.isnan(math.tan(NINF)))
753 except:
754 self.assertRaises(ValueError, math.tan, INF)
755 self.assertRaises(ValueError, math.tan, NINF)
756 self.assert_(math.isnan(math.tan(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000757
Georg Brandl2f037602006-10-28 13:51:49 +0000758 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000759 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000760 self.ftest('tanh(0)', math.tanh(0), 0)
761 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000762 self.ftest('tanh(inf)', math.tanh(INF), 1)
763 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
764 self.assert_(math.isnan(math.tanh(NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000765 # check that tanh(-0.) == -0. on IEEE 754 systems
766 if float.__getformat__("double").startswith("IEEE"):
767 self.assertEqual(math.tanh(-0.), -0.)
768 self.assertEqual(math.copysign(1., math.tanh(-0.)),
769 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000770
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000771 def test_trunc(self):
772 self.assertEqual(math.trunc(1), 1)
773 self.assertEqual(math.trunc(-1), -1)
774 self.assertEqual(type(math.trunc(1)), int)
775 self.assertEqual(type(math.trunc(1.5)), int)
776 self.assertEqual(math.trunc(1.5), 1)
777 self.assertEqual(math.trunc(-1.5), -1)
778 self.assertEqual(math.trunc(1.999999), 1)
779 self.assertEqual(math.trunc(-1.999999), -1)
780 self.assertEqual(math.trunc(-0.999999), -0)
781 self.assertEqual(math.trunc(-100.999), -100)
782
783 class TestTrunc(object):
784 def __trunc__(self):
785 return 23
786
787 class TestNoTrunc(object):
788 pass
789
790 self.assertEqual(math.trunc(TestTrunc()), 23)
791
792 self.assertRaises(TypeError, math.trunc)
793 self.assertRaises(TypeError, math.trunc, 1, 2)
794 # XXX: This is not ideal, but see the comment in math_trunc().
795 self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
796
797 t = TestNoTrunc()
798 t.__trunc__ = lambda *args: args
799 self.assertEquals((), math.trunc(t))
800 self.assertRaises(TypeError, math.trunc, t, 0)
801
Christian Heimeseebb79c2008-01-03 22:32:26 +0000802 def testCopysign(self):
803 self.assertEqual(math.copysign(1, 42), 1.0)
804 self.assertEqual(math.copysign(0., 42), 0.0)
805 self.assertEqual(math.copysign(1., -42), -1.0)
806 self.assertEqual(math.copysign(3, 0.), 3.0)
807 self.assertEqual(math.copysign(4., -0.), -4.0)
808
Christian Heimese2ca4242008-01-03 20:23:15 +0000809 def testIsnan(self):
810 self.assert_(math.isnan(float("nan")))
811 self.assert_(math.isnan(float("inf")* 0.))
812 self.failIf(math.isnan(float("inf")))
813 self.failIf(math.isnan(0.))
814 self.failIf(math.isnan(1.))
815
816 def testIsinf(self):
817 self.assert_(math.isinf(float("inf")))
818 self.assert_(math.isinf(float("-inf")))
819 self.assert_(math.isinf(1E400))
820 self.assert_(math.isinf(-1E400))
821 self.failIf(math.isinf(float("nan")))
822 self.failIf(math.isinf(0.))
823 self.failIf(math.isinf(1.))
824
Georg Brandl2f037602006-10-28 13:51:49 +0000825 # RED_FLAG 16-Oct-2000 Tim
826 # While 2.0 is more consistent about exceptions than previous releases, it
827 # still fails this part of the test on some platforms. For now, we only
828 # *run* test_exceptions() in verbose mode, so that this isn't normally
829 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000830
Georg Brandl2f037602006-10-28 13:51:49 +0000831 if verbose:
832 def test_exceptions(self):
833 try:
834 x = math.exp(-1000000000)
835 except:
836 # mathmodule.c is failing to weed out underflows from libm, or
837 # we've got an fp format with huge dynamic range
838 self.fail("underflowing exp() should not have raised "
839 "an exception")
840 if x != 0:
841 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000842
Georg Brandl2f037602006-10-28 13:51:49 +0000843 # If this fails, probably using a strict IEEE-754 conforming libm, and x
844 # is +Inf afterwards. But Python wants overflows detected by default.
845 try:
846 x = math.exp(1000000000)
847 except OverflowError:
848 pass
849 else:
850 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000851
Georg Brandl2f037602006-10-28 13:51:49 +0000852 # If this fails, it could be a puzzle. One odd possibility is that
853 # mathmodule.c's macros are getting confused while comparing
854 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
855 # as a result (and so raising OverflowError instead).
856 try:
857 x = math.sqrt(-1.0)
858 except ValueError:
859 pass
860 else:
861 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000862
Christian Heimes6f341092008-04-18 23:13:07 +0000863 def test_testfile(self):
864 if not float.__getformat__("double").startswith("IEEE"):
865 return
866 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
867 # Skip if either the input or result is complex, or if
868 # flags is nonempty
869 if ai != 0. or ei != 0. or flags:
870 continue
871 if fn in ['rect', 'polar']:
872 # no real versions of rect, polar
873 continue
874 func = getattr(math, fn)
Mark Dickinson9f99d702008-04-20 01:22:30 +0000875 try:
876 result = func(ar)
877 except ValueError:
878 message = ("Unexpected ValueError in " +
879 "test %s:%s(%r)\n" % (id, fn, ar))
880 self.fail(message)
Mark Dickinsond0558352008-05-23 03:30:01 +0000881 except OverflowError:
882 message = ("Unexpected OverflowError in " +
883 "test %s:%s(%r)\n" % (id, fn, ar))
884 self.fail(message)
Christian Heimes6f341092008-04-18 23:13:07 +0000885 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Georg Brandl2f037602006-10-28 13:51:49 +0000886
887def test_main():
Christian Heimes6f341092008-04-18 23:13:07 +0000888 from doctest import DocFileSuite
889 suite = unittest.TestSuite()
890 suite.addTest(unittest.makeSuite(MathTests))
891 suite.addTest(DocFileSuite("ieee754.txt"))
892 run_unittest(suite)
Georg Brandl2f037602006-10-28 13:51:49 +0000893
894if __name__ == '__main__':
895 test_main()