blob: 6f1578285962a82910e98d1bc1403a03d61cbb10 [file] [log] [blame]
Guido van Rossumfcce6301996-08-08 18:26:25 +00001# Python test set -- math module
2# XXXX Should not do tests around zero only
3
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test.support import run_unittest, verbose
Thomas Wouters89f507f2006-12-13 04:49:30 +00005import unittest
6import math
Christian Heimes53876d92008-04-19 00:31:39 +00007import os
8import sys
Georg Brandlc28e1fa2008-06-10 19:20:26 +00009import random
Guido van Rossumfcce6301996-08-08 18:26:25 +000010
Christian Heimes53876d92008-04-19 00:31:39 +000011eps = 1E-05
12NAN = float('nan')
13INF = float('inf')
14NINF = float('-inf')
15
Mark Dickinson63566232009-09-18 21:04:19 +000016# decorator for skipping tests on non-IEEE 754 platforms
17requires_IEEE_754 = unittest.skipUnless(
18 float.__getformat__("double").startswith("IEEE"),
19 "test requires IEEE 754 doubles")
20
Mark Dickinson5c567082009-04-24 16:39:07 +000021# detect evidence of double-rounding: fsum is not always correctly
22# rounded on machines that suffer from double rounding.
23x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
24HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
25
Christian Heimes53876d92008-04-19 00:31:39 +000026# locate file with test values
27if __name__ == '__main__':
28 file = sys.argv[0]
29else:
30 file = __file__
31test_dir = os.path.dirname(file) or os.curdir
32test_file = os.path.join(test_dir, 'cmath_testcases.txt')
33
34def parse_testfile(fname):
35 """Parse a file with test values
36
37 Empty lines or lines starting with -- are ignored
38 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
39 """
40 with open(fname) as fp:
41 for line in fp:
42 # skip comment lines and blank lines
43 if line.startswith('--') or not line.strip():
44 continue
45
46 lhs, rhs = line.split('->')
47 id, fn, arg_real, arg_imag = lhs.split()
48 rhs_pieces = rhs.split()
49 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
50 flags = rhs_pieces[2:]
51
52 yield (id, fn,
53 float(arg_real), float(arg_imag),
54 float(exp_real), float(exp_imag),
55 flags
56 )
Guido van Rossumfcce6301996-08-08 18:26:25 +000057
Thomas Wouters89f507f2006-12-13 04:49:30 +000058class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000059
Thomas Wouters89f507f2006-12-13 04:49:30 +000060 def ftest(self, name, value, expected):
61 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +000062 # Use %r instead of %f so the error message
63 # displays full precision. Otherwise discrepancies
64 # in the last few bits will lead to very confusing
65 # error messages
66 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +000067 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000068
Thomas Wouters89f507f2006-12-13 04:49:30 +000069 def testConstants(self):
70 self.ftest('pi', math.pi, 3.1415926)
71 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000072
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 def testAcos(self):
74 self.assertRaises(TypeError, math.acos)
75 self.ftest('acos(-1)', math.acos(-1), math.pi)
76 self.ftest('acos(0)', math.acos(0), math.pi/2)
77 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +000078 self.assertRaises(ValueError, math.acos, INF)
79 self.assertRaises(ValueError, math.acos, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000080 self.assertTrue(math.isnan(math.acos(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +000081
82 def testAcosh(self):
83 self.assertRaises(TypeError, math.acosh)
84 self.ftest('acosh(1)', math.acosh(1), 0)
85 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
86 self.assertRaises(ValueError, math.acosh, 0)
87 self.assertRaises(ValueError, math.acosh, -1)
88 self.assertEquals(math.acosh(INF), INF)
89 self.assertRaises(ValueError, math.acosh, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000090 self.assertTrue(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000091
Thomas Wouters89f507f2006-12-13 04:49:30 +000092 def testAsin(self):
93 self.assertRaises(TypeError, math.asin)
94 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
95 self.ftest('asin(0)', math.asin(0), 0)
96 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +000097 self.assertRaises(ValueError, math.asin, INF)
98 self.assertRaises(ValueError, math.asin, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000099 self.assertTrue(math.isnan(math.asin(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000100
101 def testAsinh(self):
102 self.assertRaises(TypeError, math.asinh)
103 self.ftest('asinh(0)', math.asinh(0), 0)
104 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
105 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
106 self.assertEquals(math.asinh(INF), INF)
107 self.assertEquals(math.asinh(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000108 self.assertTrue(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000109
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 def testAtan(self):
111 self.assertRaises(TypeError, math.atan)
112 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
113 self.ftest('atan(0)', math.atan(0), 0)
114 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes53876d92008-04-19 00:31:39 +0000115 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Christian Heimesa342c012008-04-20 21:01:16 +0000116 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000117 self.assertTrue(math.isnan(math.atan(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000118
119 def testAtanh(self):
120 self.assertRaises(TypeError, math.atan)
121 self.ftest('atanh(0)', math.atanh(0), 0)
122 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
123 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
124 self.assertRaises(ValueError, math.atanh, 1)
125 self.assertRaises(ValueError, math.atanh, -1)
126 self.assertRaises(ValueError, math.atanh, INF)
127 self.assertRaises(ValueError, math.atanh, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000128 self.assertTrue(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000129
Thomas Wouters89f507f2006-12-13 04:49:30 +0000130 def testAtan2(self):
131 self.assertRaises(TypeError, math.atan2)
132 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
133 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
134 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
135 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
136 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000137
Christian Heimese57950f2008-04-21 13:08:03 +0000138 # math.atan2(0, x)
139 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
140 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
141 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
142 self.assertEqual(math.atan2(0., 0.), 0.)
143 self.assertEqual(math.atan2(0., 2.3), 0.)
144 self.assertEqual(math.atan2(0., INF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000145 self.assertTrue(math.isnan(math.atan2(0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000146 # math.atan2(-0, x)
147 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
148 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
149 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
150 self.assertEqual(math.atan2(-0., 0.), -0.)
151 self.assertEqual(math.atan2(-0., 2.3), -0.)
152 self.assertEqual(math.atan2(-0., INF), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000153 self.assertTrue(math.isnan(math.atan2(-0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000154 # math.atan2(INF, x)
155 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
156 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
157 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
158 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
159 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
160 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000161 self.assertTrue(math.isnan(math.atan2(INF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000162 # math.atan2(NINF, x)
163 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
164 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
165 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
166 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
167 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
168 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000169 self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000170 # math.atan2(+finite, x)
171 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
172 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
173 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
174 self.assertEqual(math.atan2(2.3, INF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000175 self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000176 # math.atan2(-finite, x)
177 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
178 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
179 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
180 self.assertEqual(math.atan2(-2.3, INF), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000181 self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000182 # math.atan2(NAN, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000183 self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
184 self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
185 self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
186 self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
187 self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
188 self.assertTrue(math.isnan(math.atan2(NAN, INF)))
189 self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000190
Thomas Wouters89f507f2006-12-13 04:49:30 +0000191 def testCeil(self):
192 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000193 self.assertEquals(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000194 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
195 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
196 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
197 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
198 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
199 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000200 #self.assertEquals(math.ceil(INF), INF)
201 #self.assertEquals(math.ceil(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000202 #self.assertTrue(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000203
Guido van Rossum13e05de2007-08-23 22:56:55 +0000204 class TestCeil:
205 def __ceil__(self):
206 return 42
207 class TestNoCeil:
208 pass
209 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
210 self.assertRaises(TypeError, math.ceil, TestNoCeil())
211
212 t = TestNoCeil()
213 t.__ceil__ = lambda *args: args
214 self.assertRaises(TypeError, math.ceil, t)
215 self.assertRaises(TypeError, math.ceil, t, 0)
216
Mark Dickinson63566232009-09-18 21:04:19 +0000217 @requires_IEEE_754
218 def testCopysign(self):
219 self.assertRaises(TypeError, math.copysign)
220 # copysign should let us distinguish signs of zeros
221 self.assertEquals(copysign(1., 0.), 1.)
222 self.assertEquals(copysign(1., -0.), -1.)
223 self.assertEquals(copysign(INF, 0.), INF)
224 self.assertEquals(copysign(INF, -0.), NINF)
225 self.assertEquals(copysign(NINF, 0.), INF)
226 self.assertEquals(copysign(NINF, -0.), NINF)
227 # and of infinities
228 self.assertEquals(copysign(1., INF), 1.)
229 self.assertEquals(copysign(1., NINF), -1.)
230 self.assertEquals(copysign(INF, INF), INF)
231 self.assertEquals(copysign(INF, NINF), NINF)
232 self.assertEquals(copysign(NINF, INF), INF)
233 self.assertEquals(copysign(NINF, NINF), NINF)
234 self.assertTrue(math.isnan(copysign(NAN, 1.)))
235 self.assertTrue(math.isnan(copysign(NAN, INF)))
236 self.assertTrue(math.isnan(copysign(NAN, NINF)))
237 self.assertTrue(math.isnan(copysign(NAN, NAN)))
238 # copysign(INF, NAN) may be INF or it may be NINF, since
239 # we don't know whether the sign bit of NAN is set on any
240 # given platform.
241 self.assertTrue(math.isinf(copysign(INF, NAN)))
242 # similarly, copysign(2., NAN) could be 2. or -2.
243 self.assertEquals(abs(copysign(2., NAN)), 2.)
Christian Heimes53876d92008-04-19 00:31:39 +0000244
Thomas Wouters89f507f2006-12-13 04:49:30 +0000245 def testCos(self):
246 self.assertRaises(TypeError, math.cos)
247 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
248 self.ftest('cos(0)', math.cos(0), 1)
249 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
250 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000251 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000252 self.assertTrue(math.isnan(math.cos(INF)))
253 self.assertTrue(math.isnan(math.cos(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000254 except ValueError:
255 self.assertRaises(ValueError, math.cos, INF)
256 self.assertRaises(ValueError, math.cos, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000257 self.assertTrue(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000258
Thomas Wouters89f507f2006-12-13 04:49:30 +0000259 def testCosh(self):
260 self.assertRaises(TypeError, math.cosh)
261 self.ftest('cosh(0)', math.cosh(0), 1)
262 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
Christian Heimes53876d92008-04-19 00:31:39 +0000263 self.assertEquals(math.cosh(INF), INF)
264 self.assertEquals(math.cosh(NINF), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000265 self.assertTrue(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000266
Thomas Wouters89f507f2006-12-13 04:49:30 +0000267 def testDegrees(self):
268 self.assertRaises(TypeError, math.degrees)
269 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
270 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
271 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000272
Thomas Wouters89f507f2006-12-13 04:49:30 +0000273 def testExp(self):
274 self.assertRaises(TypeError, math.exp)
275 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
276 self.ftest('exp(0)', math.exp(0), 1)
277 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes53876d92008-04-19 00:31:39 +0000278 self.assertEquals(math.exp(INF), INF)
279 self.assertEquals(math.exp(NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000280 self.assertTrue(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000281
Thomas Wouters89f507f2006-12-13 04:49:30 +0000282 def testFabs(self):
283 self.assertRaises(TypeError, math.fabs)
284 self.ftest('fabs(-1)', math.fabs(-1), 1)
285 self.ftest('fabs(0)', math.fabs(0), 0)
286 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000287
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000288 def testFactorial(self):
289 def fact(n):
290 result = 1
291 for i in range(1, int(n)+1):
292 result *= i
293 return result
294 values = list(range(10)) + [50, 100, 500]
295 random.shuffle(values)
296 for x in range(10):
297 for cast in (int, float):
298 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
299 self.assertRaises(ValueError, math.factorial, -1)
300 self.assertRaises(ValueError, math.factorial, math.pi)
301
Thomas Wouters89f507f2006-12-13 04:49:30 +0000302 def testFloor(self):
303 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000304 self.assertEquals(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000305 self.ftest('floor(0.5)', math.floor(0.5), 0)
306 self.ftest('floor(1.0)', math.floor(1.0), 1)
307 self.ftest('floor(1.5)', math.floor(1.5), 1)
308 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
309 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
310 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000311 # pow() relies on floor() to check for integers
312 # This fails on some platforms - so check it here
313 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
314 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes53876d92008-04-19 00:31:39 +0000315 #self.assertEquals(math.ceil(INF), INF)
316 #self.assertEquals(math.ceil(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000317 #self.assertTrue(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000318
Guido van Rossum13e05de2007-08-23 22:56:55 +0000319 class TestFloor:
320 def __floor__(self):
321 return 42
322 class TestNoFloor:
323 pass
324 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
325 self.assertRaises(TypeError, math.floor, TestNoFloor())
326
327 t = TestNoFloor()
328 t.__floor__ = lambda *args: args
329 self.assertRaises(TypeError, math.floor, t)
330 self.assertRaises(TypeError, math.floor, t, 0)
331
Thomas Wouters89f507f2006-12-13 04:49:30 +0000332 def testFmod(self):
333 self.assertRaises(TypeError, math.fmod)
334 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
335 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
336 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
337 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
338 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
339 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000340 self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
341 self.assertTrue(math.isnan(math.fmod(1., NAN)))
342 self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000343 self.assertRaises(ValueError, math.fmod, 1., 0.)
344 self.assertRaises(ValueError, math.fmod, INF, 1.)
345 self.assertRaises(ValueError, math.fmod, NINF, 1.)
346 self.assertRaises(ValueError, math.fmod, INF, 0.)
347 self.assertEquals(math.fmod(3.0, INF), 3.0)
348 self.assertEquals(math.fmod(-3.0, INF), -3.0)
349 self.assertEquals(math.fmod(3.0, NINF), 3.0)
350 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
351 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
352 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000353
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 def testFrexp(self):
355 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000356
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000357 def testfrexp(name, result, expected):
358 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000359 if abs(mant-emant) > eps or exp != eexp:
360 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000361 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000362
Thomas Wouters89f507f2006-12-13 04:49:30 +0000363 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
364 testfrexp('frexp(0)', math.frexp(0), (0, 0))
365 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
366 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000367
Christian Heimes53876d92008-04-19 00:31:39 +0000368 self.assertEquals(math.frexp(INF)[0], INF)
369 self.assertEquals(math.frexp(NINF)[0], NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000370 self.assertTrue(math.isnan(math.frexp(NAN)[0]))
Christian Heimes53876d92008-04-19 00:31:39 +0000371
Mark Dickinson63566232009-09-18 21:04:19 +0000372 @requires_IEEE_754
Mark Dickinson5c567082009-04-24 16:39:07 +0000373 @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
374 "fsum is not exact on machines with double rounding")
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000375 def testFsum(self):
376 # math.fsum relies on exact rounding for correct operation.
377 # There's a known problem with IA32 floating-point that causes
378 # inexact rounding in some situations, and will cause the
379 # math.fsum tests below to fail; see issue #2937. On non IEEE
380 # 754 platforms, and on IEEE 754 platforms that exhibit the
381 # problem described in issue #2937, we simply skip the whole
382 # test.
383
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000384 # Python version of math.fsum, for comparison. Uses a
385 # different algorithm based on frexp, ldexp and integer
386 # arithmetic.
387 from sys import float_info
388 mant_dig = float_info.mant_dig
389 etiny = float_info.min_exp - mant_dig
390
391 def msum(iterable):
392 """Full precision summation. Compute sum(iterable) without any
393 intermediate accumulation of error. Based on the 'lsum' function
394 at http://code.activestate.com/recipes/393090/
395
396 """
397 tmant, texp = 0, 0
398 for x in iterable:
399 mant, exp = math.frexp(x)
400 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
401 if texp > exp:
402 tmant <<= texp-exp
403 texp = exp
404 else:
405 mant <<= exp-texp
406 tmant += mant
407 # Round tmant * 2**texp to a float. The original recipe
408 # used float(str(tmant)) * 2.0**texp for this, but that's
409 # a little unsafe because str -> float conversion can't be
410 # relied upon to do correct rounding on all platforms.
411 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
412 if tail > 0:
413 h = 1 << (tail-1)
414 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
415 texp += tail
416 return math.ldexp(tmant, texp)
417
418 test_values = [
419 ([], 0.0),
420 ([0.0], 0.0),
421 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
422 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
423 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
424 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
425 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
426 ([1./n for n in range(1, 1001)],
427 float.fromhex('0x1.df11f45f4e61ap+2')),
428 ([(-1.)**n/n for n in range(1, 1001)],
429 float.fromhex('-0x1.62a2af1bd3624p-1')),
430 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
431 ([1e16, 1., 1e-16], 10000000000000002.0),
432 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
433 # exercise code for resizing partials array
434 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
435 [-2.**1022],
436 float.fromhex('0x1.5555555555555p+970')),
437 ]
438
439 for i, (vals, expected) in enumerate(test_values):
440 try:
441 actual = math.fsum(vals)
442 except OverflowError:
443 self.fail("test %d failed: got OverflowError, expected %r "
444 "for math.fsum(%.100r)" % (i, expected, vals))
445 except ValueError:
446 self.fail("test %d failed: got ValueError, expected %r "
447 "for math.fsum(%.100r)" % (i, expected, vals))
448 self.assertEqual(actual, expected)
449
450 from random import random, gauss, shuffle
451 for j in range(1000):
452 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
453 s = 0
454 for i in range(200):
455 v = gauss(0, random()) ** 7 - s
456 s += v
457 vals.append(v)
458 shuffle(vals)
459
460 s = msum(vals)
461 self.assertEqual(msum(vals), math.fsum(vals))
462
Thomas Wouters89f507f2006-12-13 04:49:30 +0000463 def testHypot(self):
464 self.assertRaises(TypeError, math.hypot)
465 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
466 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000467 self.assertEqual(math.hypot(NAN, INF), INF)
468 self.assertEqual(math.hypot(INF, NAN), INF)
469 self.assertEqual(math.hypot(NAN, NINF), INF)
470 self.assertEqual(math.hypot(NINF, NAN), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000471 self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
472 self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000473
Thomas Wouters89f507f2006-12-13 04:49:30 +0000474 def testLdexp(self):
475 self.assertRaises(TypeError, math.ldexp)
476 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
477 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
478 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
479 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000480 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
481 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
482 self.assertEquals(math.ldexp(1., -1000000), 0.)
483 self.assertEquals(math.ldexp(-1., -1000000), -0.)
484 self.assertEquals(math.ldexp(INF, 30), INF)
485 self.assertEquals(math.ldexp(NINF, -213), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000486 self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000487
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000488 # large second argument
489 for n in [10**5, 10**10, 10**20, 10**40]:
490 self.assertEquals(math.ldexp(INF, -n), INF)
491 self.assertEquals(math.ldexp(NINF, -n), NINF)
492 self.assertEquals(math.ldexp(1., -n), 0.)
493 self.assertEquals(math.ldexp(-1., -n), -0.)
494 self.assertEquals(math.ldexp(0., -n), 0.)
495 self.assertEquals(math.ldexp(-0., -n), -0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000496 self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000497
498 self.assertRaises(OverflowError, math.ldexp, 1., n)
499 self.assertRaises(OverflowError, math.ldexp, -1., n)
500 self.assertEquals(math.ldexp(0., n), 0.)
501 self.assertEquals(math.ldexp(-0., n), -0.)
502 self.assertEquals(math.ldexp(INF, n), INF)
503 self.assertEquals(math.ldexp(NINF, n), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000504 self.assertTrue(math.isnan(math.ldexp(NAN, n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000505
Thomas Wouters89f507f2006-12-13 04:49:30 +0000506 def testLog(self):
507 self.assertRaises(TypeError, math.log)
508 self.ftest('log(1/e)', math.log(1/math.e), -1)
509 self.ftest('log(1)', math.log(1), 0)
510 self.ftest('log(e)', math.log(math.e), 1)
511 self.ftest('log(32,2)', math.log(32,2), 5)
512 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
513 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000514 self.assertEquals(math.log(INF), INF)
515 self.assertRaises(ValueError, math.log, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000516 self.assertTrue(math.isnan(math.log(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000517
518 def testLog1p(self):
519 self.assertRaises(TypeError, math.log1p)
520 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
521 self.ftest('log1p(0)', math.log1p(0), 0)
522 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
523 self.ftest('log1p(1)', math.log1p(1), math.log(2))
524 self.assertEquals(math.log1p(INF), INF)
525 self.assertRaises(ValueError, math.log1p, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000526 self.assertTrue(math.isnan(math.log1p(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000527 n= 2**90
528 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
529 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000530
Thomas Wouters89f507f2006-12-13 04:49:30 +0000531 def testLog10(self):
532 self.assertRaises(TypeError, math.log10)
533 self.ftest('log10(0.1)', math.log10(0.1), -1)
534 self.ftest('log10(1)', math.log10(1), 0)
535 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes53876d92008-04-19 00:31:39 +0000536 self.assertEquals(math.log(INF), INF)
537 self.assertRaises(ValueError, math.log10, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000538 self.assertTrue(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000539
Thomas Wouters89f507f2006-12-13 04:49:30 +0000540 def testModf(self):
541 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000542
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000543 def testmodf(name, result, expected):
544 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000545 if abs(v1-e1) > eps or abs(v2-e2):
546 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000547 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000548
Thomas Wouters89f507f2006-12-13 04:49:30 +0000549 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
550 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000551
Christian Heimes53876d92008-04-19 00:31:39 +0000552 self.assertEquals(math.modf(INF), (0.0, INF))
553 self.assertEquals(math.modf(NINF), (-0.0, NINF))
554
555 modf_nan = math.modf(NAN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000556 self.assertTrue(math.isnan(modf_nan[0]))
557 self.assertTrue(math.isnan(modf_nan[1]))
Christian Heimes53876d92008-04-19 00:31:39 +0000558
Thomas Wouters89f507f2006-12-13 04:49:30 +0000559 def testPow(self):
560 self.assertRaises(TypeError, math.pow)
561 self.ftest('pow(0,1)', math.pow(0,1), 0)
562 self.ftest('pow(1,0)', math.pow(1,0), 1)
563 self.ftest('pow(2,1)', math.pow(2,1), 2)
564 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000565 self.assertEqual(math.pow(INF, 1), INF)
566 self.assertEqual(math.pow(NINF, 1), NINF)
567 self.assertEqual((math.pow(1, INF)), 1.)
568 self.assertEqual((math.pow(1, NINF)), 1.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000569 self.assertTrue(math.isnan(math.pow(NAN, 1)))
570 self.assertTrue(math.isnan(math.pow(2, NAN)))
571 self.assertTrue(math.isnan(math.pow(0, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000572 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000573
574 # pow(0., x)
575 self.assertEqual(math.pow(0., INF), 0.)
576 self.assertEqual(math.pow(0., 3.), 0.)
577 self.assertEqual(math.pow(0., 2.3), 0.)
578 self.assertEqual(math.pow(0., 2.), 0.)
579 self.assertEqual(math.pow(0., 0.), 1.)
580 self.assertEqual(math.pow(0., -0.), 1.)
581 self.assertRaises(ValueError, math.pow, 0., -2.)
582 self.assertRaises(ValueError, math.pow, 0., -2.3)
583 self.assertRaises(ValueError, math.pow, 0., -3.)
584 self.assertRaises(ValueError, math.pow, 0., NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000585 self.assertTrue(math.isnan(math.pow(0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000586
587 # pow(INF, x)
588 self.assertEqual(math.pow(INF, INF), INF)
589 self.assertEqual(math.pow(INF, 3.), INF)
590 self.assertEqual(math.pow(INF, 2.3), INF)
591 self.assertEqual(math.pow(INF, 2.), INF)
592 self.assertEqual(math.pow(INF, 0.), 1.)
593 self.assertEqual(math.pow(INF, -0.), 1.)
594 self.assertEqual(math.pow(INF, -2.), 0.)
595 self.assertEqual(math.pow(INF, -2.3), 0.)
596 self.assertEqual(math.pow(INF, -3.), 0.)
597 self.assertEqual(math.pow(INF, NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000598 self.assertTrue(math.isnan(math.pow(INF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000599
600 # pow(-0., x)
601 self.assertEqual(math.pow(-0., INF), 0.)
602 self.assertEqual(math.pow(-0., 3.), -0.)
603 self.assertEqual(math.pow(-0., 2.3), 0.)
604 self.assertEqual(math.pow(-0., 2.), 0.)
605 self.assertEqual(math.pow(-0., 0.), 1.)
606 self.assertEqual(math.pow(-0., -0.), 1.)
607 self.assertRaises(ValueError, math.pow, -0., -2.)
608 self.assertRaises(ValueError, math.pow, -0., -2.3)
609 self.assertRaises(ValueError, math.pow, -0., -3.)
610 self.assertRaises(ValueError, math.pow, -0., NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000611 self.assertTrue(math.isnan(math.pow(-0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000612
613 # pow(NINF, x)
614 self.assertEqual(math.pow(NINF, INF), INF)
615 self.assertEqual(math.pow(NINF, 3.), NINF)
616 self.assertEqual(math.pow(NINF, 2.3), INF)
617 self.assertEqual(math.pow(NINF, 2.), INF)
618 self.assertEqual(math.pow(NINF, 0.), 1.)
619 self.assertEqual(math.pow(NINF, -0.), 1.)
620 self.assertEqual(math.pow(NINF, -2.), 0.)
621 self.assertEqual(math.pow(NINF, -2.3), 0.)
622 self.assertEqual(math.pow(NINF, -3.), -0.)
623 self.assertEqual(math.pow(NINF, NINF), 0.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000624 self.assertTrue(math.isnan(math.pow(NINF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000625
626 # pow(-1, x)
627 self.assertEqual(math.pow(-1., INF), 1.)
628 self.assertEqual(math.pow(-1., 3.), -1.)
629 self.assertRaises(ValueError, math.pow, -1., 2.3)
630 self.assertEqual(math.pow(-1., 2.), 1.)
631 self.assertEqual(math.pow(-1., 0.), 1.)
632 self.assertEqual(math.pow(-1., -0.), 1.)
633 self.assertEqual(math.pow(-1., -2.), 1.)
634 self.assertRaises(ValueError, math.pow, -1., -2.3)
635 self.assertEqual(math.pow(-1., -3.), -1.)
636 self.assertEqual(math.pow(-1., NINF), 1.)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000637 self.assertTrue(math.isnan(math.pow(-1., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000638
639 # pow(1, x)
640 self.assertEqual(math.pow(1., INF), 1.)
641 self.assertEqual(math.pow(1., 3.), 1.)
642 self.assertEqual(math.pow(1., 2.3), 1.)
643 self.assertEqual(math.pow(1., 2.), 1.)
644 self.assertEqual(math.pow(1., 0.), 1.)
645 self.assertEqual(math.pow(1., -0.), 1.)
646 self.assertEqual(math.pow(1., -2.), 1.)
647 self.assertEqual(math.pow(1., -2.3), 1.)
648 self.assertEqual(math.pow(1., -3.), 1.)
649 self.assertEqual(math.pow(1., NINF), 1.)
650 self.assertEqual(math.pow(1., NAN), 1.)
651
652 # pow(x, 0) should be 1 for any x
653 self.assertEqual(math.pow(2.3, 0.), 1.)
654 self.assertEqual(math.pow(-2.3, 0.), 1.)
655 self.assertEqual(math.pow(NAN, 0.), 1.)
656 self.assertEqual(math.pow(2.3, -0.), 1.)
657 self.assertEqual(math.pow(-2.3, -0.), 1.)
658 self.assertEqual(math.pow(NAN, -0.), 1.)
659
660 # pow(x, y) is invalid if x is negative and y is not integral
661 self.assertRaises(ValueError, math.pow, -1., 2.3)
662 self.assertRaises(ValueError, math.pow, -15., -3.1)
663
664 # pow(x, NINF)
665 self.assertEqual(math.pow(1.9, NINF), 0.)
666 self.assertEqual(math.pow(1.1, NINF), 0.)
667 self.assertEqual(math.pow(0.9, NINF), INF)
668 self.assertEqual(math.pow(0.1, NINF), INF)
669 self.assertEqual(math.pow(-0.1, NINF), INF)
670 self.assertEqual(math.pow(-0.9, NINF), INF)
671 self.assertEqual(math.pow(-1.1, NINF), 0.)
672 self.assertEqual(math.pow(-1.9, NINF), 0.)
673
674 # pow(x, INF)
675 self.assertEqual(math.pow(1.9, INF), INF)
676 self.assertEqual(math.pow(1.1, INF), INF)
677 self.assertEqual(math.pow(0.9, INF), 0.)
678 self.assertEqual(math.pow(0.1, INF), 0.)
679 self.assertEqual(math.pow(-0.1, INF), 0.)
680 self.assertEqual(math.pow(-0.9, INF), 0.)
681 self.assertEqual(math.pow(-1.1, INF), INF)
682 self.assertEqual(math.pow(-1.9, INF), INF)
683
684 # pow(x, y) should work for x negative, y an integer
685 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
686 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
687 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
688 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
689 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
690 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
691 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
692 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
693 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
694 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
695
696 # the following tests have been commented out since they don't
697 # really belong here: the implementation of ** for floats is
698 # independent of the implemention of math.pow
699 #self.assertEqual(1**NAN, 1)
700 #self.assertEqual(1**INF, 1)
701 #self.assertEqual(1**NINF, 1)
702 #self.assertEqual(1**0, 1)
703 #self.assertEqual(1.**NAN, 1)
704 #self.assertEqual(1.**INF, 1)
705 #self.assertEqual(1.**NINF, 1)
706 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000707
Thomas Wouters89f507f2006-12-13 04:49:30 +0000708 def testRadians(self):
709 self.assertRaises(TypeError, math.radians)
710 self.ftest('radians(180)', math.radians(180), math.pi)
711 self.ftest('radians(90)', math.radians(90), math.pi/2)
712 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000713
Thomas Wouters89f507f2006-12-13 04:49:30 +0000714 def testSin(self):
715 self.assertRaises(TypeError, math.sin)
716 self.ftest('sin(0)', math.sin(0), 0)
717 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
718 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000719 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000720 self.assertTrue(math.isnan(math.sin(INF)))
721 self.assertTrue(math.isnan(math.sin(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000722 except ValueError:
723 self.assertRaises(ValueError, math.sin, INF)
724 self.assertRaises(ValueError, math.sin, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000725 self.assertTrue(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000726
Thomas Wouters89f507f2006-12-13 04:49:30 +0000727 def testSinh(self):
728 self.assertRaises(TypeError, math.sinh)
729 self.ftest('sinh(0)', math.sinh(0), 0)
730 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
731 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000732 self.assertEquals(math.sinh(INF), INF)
Christian Heimesa342c012008-04-20 21:01:16 +0000733 self.assertEquals(math.sinh(NINF), NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000734 self.assertTrue(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000735
Thomas Wouters89f507f2006-12-13 04:49:30 +0000736 def testSqrt(self):
737 self.assertRaises(TypeError, math.sqrt)
738 self.ftest('sqrt(0)', math.sqrt(0), 0)
739 self.ftest('sqrt(1)', math.sqrt(1), 1)
740 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes53876d92008-04-19 00:31:39 +0000741 self.assertEquals(math.sqrt(INF), INF)
742 self.assertRaises(ValueError, math.sqrt, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000743 self.assertTrue(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000744
Thomas Wouters89f507f2006-12-13 04:49:30 +0000745 def testTan(self):
746 self.assertRaises(TypeError, math.tan)
747 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 Heimes53876d92008-04-19 00:31:39 +0000750 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000751 self.assertTrue(math.isnan(math.tan(INF)))
752 self.assertTrue(math.isnan(math.tan(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000753 except:
754 self.assertRaises(ValueError, math.tan, INF)
755 self.assertRaises(ValueError, math.tan, NINF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000756 self.assertTrue(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000757
Thomas Wouters89f507f2006-12-13 04:49:30 +0000758 def testTanh(self):
759 self.assertRaises(TypeError, math.tanh)
760 self.ftest('tanh(0)', math.tanh(0), 0)
761 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000762 self.ftest('tanh(inf)', math.tanh(INF), 1)
763 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000764 self.assertTrue(math.isnan(math.tanh(NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +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
Christian Heimes400adb02008-02-01 08:12:03 +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 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
795
796 # XXX Doesn't work because the method is looked up on
797 # the type only.
798 #t = TestNoTrunc()
799 #t.__trunc__ = lambda *args: args
800 #self.assertEquals((), math.trunc(t))
801 #self.assertRaises(TypeError, math.trunc, t, 0)
802
Christian Heimes072c0f12008-01-03 23:01:04 +0000803 def testCopysign(self):
804 self.assertEqual(math.copysign(1, 42), 1.0)
805 self.assertEqual(math.copysign(0., 42), 0.0)
806 self.assertEqual(math.copysign(1., -42), -1.0)
807 self.assertEqual(math.copysign(3, 0.), 3.0)
808 self.assertEqual(math.copysign(4., -0.), -4.0)
809
810 def testIsnan(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000811 self.assertTrue(math.isnan(float("nan")))
812 self.assertTrue(math.isnan(float("inf")* 0.))
813 self.assertFalse(math.isnan(float("inf")))
814 self.assertFalse(math.isnan(0.))
815 self.assertFalse(math.isnan(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000816
817 def testIsinf(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000818 self.assertTrue(math.isinf(float("inf")))
819 self.assertTrue(math.isinf(float("-inf")))
820 self.assertTrue(math.isinf(1E400))
821 self.assertTrue(math.isinf(-1E400))
822 self.assertFalse(math.isinf(float("nan")))
823 self.assertFalse(math.isinf(0.))
824 self.assertFalse(math.isinf(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000825
Thomas Wouters89f507f2006-12-13 04:49:30 +0000826 # RED_FLAG 16-Oct-2000 Tim
827 # While 2.0 is more consistent about exceptions than previous releases, it
828 # still fails this part of the test on some platforms. For now, we only
829 # *run* test_exceptions() in verbose mode, so that this isn't normally
830 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000831
Thomas Wouters89f507f2006-12-13 04:49:30 +0000832 if verbose:
833 def test_exceptions(self):
834 try:
835 x = math.exp(-1000000000)
836 except:
837 # mathmodule.c is failing to weed out underflows from libm, or
838 # we've got an fp format with huge dynamic range
839 self.fail("underflowing exp() should not have raised "
840 "an exception")
841 if x != 0:
842 self.fail("underflowing exp() should have returned 0")
843
844 # If this fails, probably using a strict IEEE-754 conforming libm, and x
845 # is +Inf afterwards. But Python wants overflows detected by default.
846 try:
847 x = math.exp(1000000000)
848 except OverflowError:
849 pass
850 else:
851 self.fail("overflowing exp() didn't trigger OverflowError")
852
853 # If this fails, it could be a puzzle. One odd possibility is that
854 # mathmodule.c's macros are getting confused while comparing
855 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
856 # as a result (and so raising OverflowError instead).
857 try:
858 x = math.sqrt(-1.0)
859 except ValueError:
860 pass
861 else:
862 self.fail("sqrt(-1) didn't raise ValueError")
863
Mark Dickinson63566232009-09-18 21:04:19 +0000864 @requires_IEEE_754
Christian Heimes53876d92008-04-19 00:31:39 +0000865 def test_testfile(self):
Christian Heimes53876d92008-04-19 00:31:39 +0000866 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)
Christian Heimesa342c012008-04-20 21:01:16 +0000875 try:
876 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +0000877 except ValueError as exc:
878 message = (("Unexpected ValueError: %s\n " +
879 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +0000880 self.fail(message)
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000881 except OverflowError:
882 message = ("Unexpected OverflowError in " +
883 "test %s:%s(%r)\n" % (id, fn, ar))
884 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +0000885 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000886
887def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +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)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000893
894if __name__ == '__main__':
895 test_main()