blob: 9089013eb798fd062823c9c0779131055a6c61ab [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):
Mark Dickinson3f2c03d2010-02-06 23:14:49 +0000212 self.assertEqual(math.copysign(1, 42), 1.0)
213 self.assertEqual(math.copysign(0., 42), 0.0)
214 self.assertEqual(math.copysign(1., -42), -1.0)
215 self.assertEqual(math.copysign(3, 0.), 3.0)
216 self.assertEqual(math.copysign(4., -0.), -4.0)
217
Christian Heimes6f341092008-04-18 23:13:07 +0000218 self.assertRaises(TypeError, math.copysign)
219 # copysign should let us distinguish signs of zeros
Mark Dickinson3f2c03d2010-02-06 23:14:49 +0000220 self.assertEquals(math.copysign(1., 0.), 1.)
221 self.assertEquals(math.copysign(1., -0.), -1.)
222 self.assertEquals(math.copysign(INF, 0.), INF)
223 self.assertEquals(math.copysign(INF, -0.), NINF)
224 self.assertEquals(math.copysign(NINF, 0.), INF)
225 self.assertEquals(math.copysign(NINF, -0.), NINF)
Christian Heimes6f341092008-04-18 23:13:07 +0000226 # and of infinities
Mark Dickinson3f2c03d2010-02-06 23:14:49 +0000227 self.assertEquals(math.copysign(1., INF), 1.)
228 self.assertEquals(math.copysign(1., NINF), -1.)
229 self.assertEquals(math.copysign(INF, INF), INF)
230 self.assertEquals(math.copysign(INF, NINF), NINF)
231 self.assertEquals(math.copysign(NINF, INF), INF)
232 self.assertEquals(math.copysign(NINF, NINF), NINF)
233 self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
234 self.assertTrue(math.isnan(math.copysign(NAN, INF)))
235 self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
236 self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000237 # copysign(INF, NAN) may be INF or it may be NINF, since
238 # we don't know whether the sign bit of NAN is set on any
239 # given platform.
Mark Dickinson3f2c03d2010-02-06 23:14:49 +0000240 self.assertTrue(math.isinf(math.copysign(INF, NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000241 # similarly, copysign(2., NAN) could be 2. or -2.
Mark Dickinson3f2c03d2010-02-06 23:14:49 +0000242 self.assertEquals(abs(math.copysign(2., NAN)), 2.)
Christian Heimes6f341092008-04-18 23:13:07 +0000243
Georg Brandl2f037602006-10-28 13:51:49 +0000244 def testCos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000245 self.assertRaises(TypeError, math.cos)
Georg Brandl2f037602006-10-28 13:51:49 +0000246 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
247 self.ftest('cos(0)', math.cos(0), 1)
248 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
249 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000250 try:
251 self.assert_(math.isnan(math.cos(INF)))
252 self.assert_(math.isnan(math.cos(NINF)))
253 except ValueError:
254 self.assertRaises(ValueError, math.cos, INF)
255 self.assertRaises(ValueError, math.cos, NINF)
256 self.assert_(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000257
Georg Brandl2f037602006-10-28 13:51:49 +0000258 def testCosh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000259 self.assertRaises(TypeError, math.cosh)
Georg Brandl2f037602006-10-28 13:51:49 +0000260 self.ftest('cosh(0)', math.cosh(0), 1)
261 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 +0000262 self.assertEquals(math.cosh(INF), INF)
263 self.assertEquals(math.cosh(NINF), INF)
264 self.assert_(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000265
Georg Brandl2f037602006-10-28 13:51:49 +0000266 def testDegrees(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000267 self.assertRaises(TypeError, math.degrees)
Georg Brandl2f037602006-10-28 13:51:49 +0000268 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
269 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
270 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000271
Georg Brandl2f037602006-10-28 13:51:49 +0000272 def testExp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000273 self.assertRaises(TypeError, math.exp)
Georg Brandl2f037602006-10-28 13:51:49 +0000274 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
275 self.ftest('exp(0)', math.exp(0), 1)
276 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes6f341092008-04-18 23:13:07 +0000277 self.assertEquals(math.exp(INF), INF)
278 self.assertEquals(math.exp(NINF), 0.)
279 self.assert_(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000280
Georg Brandl2f037602006-10-28 13:51:49 +0000281 def testFabs(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000282 self.assertRaises(TypeError, math.fabs)
Georg Brandl2f037602006-10-28 13:51:49 +0000283 self.ftest('fabs(-1)', math.fabs(-1), 1)
284 self.ftest('fabs(0)', math.fabs(0), 0)
285 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000286
Raymond Hettingerecbdd2e2008-06-09 06:54:45 +0000287 def testFactorial(self):
288 def fact(n):
289 result = 1
290 for i in range(1, int(n)+1):
291 result *= i
292 return result
293 values = range(10) + [50, 100, 500]
294 random.shuffle(values)
Mark Dickinson838edd82010-05-12 19:56:10 +0000295 for x in values:
Raymond Hettingerecbdd2e2008-06-09 06:54:45 +0000296 for cast in (int, long, float):
297 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
298 self.assertRaises(ValueError, math.factorial, -1)
299 self.assertRaises(ValueError, math.factorial, math.pi)
300
Georg Brandl2f037602006-10-28 13:51:49 +0000301 def testFloor(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000302 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000303 # These types will be int in py3k.
304 self.assertEquals(float, type(math.floor(1)))
305 self.assertEquals(float, type(math.floor(1L)))
306 self.assertEquals(float, type(math.floor(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000307 self.ftest('floor(0.5)', math.floor(0.5), 0)
308 self.ftest('floor(1.0)', math.floor(1.0), 1)
309 self.ftest('floor(1.5)', math.floor(1.5), 1)
310 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
311 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
312 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Nick Coghlan00f20292007-07-26 14:03:00 +0000313 # pow() relies on floor() to check for integers
314 # This fails on some platforms - so check it here
315 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
316 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes6f341092008-04-18 23:13:07 +0000317 self.assertEquals(math.ceil(INF), INF)
318 self.assertEquals(math.ceil(NINF), NINF)
319 self.assert_(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000320
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000321 class TestFloor(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000322 def __float__(self):
323 return 42.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000324 class TestNoFloor(object):
325 pass
326 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
327 self.assertRaises(TypeError, math.floor, TestNoFloor())
328
329 t = TestNoFloor()
330 t.__floor__ = lambda *args: args
331 self.assertRaises(TypeError, math.floor, t)
332 self.assertRaises(TypeError, math.floor, t, 0)
333
Georg Brandl2f037602006-10-28 13:51:49 +0000334 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000335 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +0000336 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
337 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
338 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
339 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
340 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
341 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000342 self.assert_(math.isnan(math.fmod(NAN, 1.)))
343 self.assert_(math.isnan(math.fmod(1., NAN)))
344 self.assert_(math.isnan(math.fmod(NAN, NAN)))
345 self.assertRaises(ValueError, math.fmod, 1., 0.)
346 self.assertRaises(ValueError, math.fmod, INF, 1.)
347 self.assertRaises(ValueError, math.fmod, NINF, 1.)
348 self.assertRaises(ValueError, math.fmod, INF, 0.)
349 self.assertEquals(math.fmod(3.0, INF), 3.0)
350 self.assertEquals(math.fmod(-3.0, INF), -3.0)
351 self.assertEquals(math.fmod(3.0, NINF), 3.0)
352 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
353 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
354 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000355
Georg Brandl2f037602006-10-28 13:51:49 +0000356 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000357 self.assertRaises(TypeError, math.frexp)
358
Mark Dickinson828ce4a2009-12-20 20:23:49 +0000359 def testfrexp(name, result, expected):
360 (mant, exp), (emant, eexp) = result, expected
Georg Brandl2f037602006-10-28 13:51:49 +0000361 if abs(mant-emant) > eps or exp != eexp:
362 self.fail('%s returned %r, expected %r'%\
363 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000364
Georg Brandl2f037602006-10-28 13:51:49 +0000365 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
366 testfrexp('frexp(0)', math.frexp(0), (0, 0))
367 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
368 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000369
Christian Heimes6f341092008-04-18 23:13:07 +0000370 self.assertEquals(math.frexp(INF)[0], INF)
371 self.assertEquals(math.frexp(NINF)[0], NINF)
372 self.assert_(math.isnan(math.frexp(NAN)[0]))
373
Mark Dickinson0f6414a2008-07-31 14:48:32 +0000374 def testFsum(self):
375 # math.fsum relies on exact rounding for correct operation.
376 # There's a known problem with IA32 floating-point that causes
377 # inexact rounding in some situations, and will cause the
378 # math.fsum tests below to fail; see issue #2937. On non IEEE
379 # 754 platforms, and on IEEE 754 platforms that exhibit the
380 # problem described in issue #2937, we simply skip the whole
381 # test.
382
383 if not float.__getformat__("double").startswith("IEEE"):
384 return
385
386 # on IEEE 754 compliant machines, both of the expressions
387 # below should round to 10000000000000002.0.
388 if 1e16+2.0 != 1e16+2.9999:
389 return
390
391 # Python version of math.fsum, for comparison. Uses a
392 # different algorithm based on frexp, ldexp and integer
393 # arithmetic.
394 from sys import float_info
395 mant_dig = float_info.mant_dig
396 etiny = float_info.min_exp - mant_dig
397
398 def msum(iterable):
399 """Full precision summation. Compute sum(iterable) without any
400 intermediate accumulation of error. Based on the 'lsum' function
401 at http://code.activestate.com/recipes/393090/
402
403 """
404 tmant, texp = 0, 0
405 for x in iterable:
406 mant, exp = math.frexp(x)
407 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
408 if texp > exp:
409 tmant <<= texp-exp
410 texp = exp
411 else:
412 mant <<= exp-texp
413 tmant += mant
414 # Round tmant * 2**texp to a float. The original recipe
415 # used float(str(tmant)) * 2.0**texp for this, but that's
416 # a little unsafe because str -> float conversion can't be
417 # relied upon to do correct rounding on all platforms.
418 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
419 if tail > 0:
420 h = 1 << (tail-1)
421 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
422 texp += tail
423 return math.ldexp(tmant, texp)
424
425 test_values = [
426 ([], 0.0),
427 ([0.0], 0.0),
428 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
429 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
430 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
431 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
432 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
433 ([1./n for n in range(1, 1001)],
434 float.fromhex('0x1.df11f45f4e61ap+2')),
435 ([(-1.)**n/n for n in range(1, 1001)],
436 float.fromhex('-0x1.62a2af1bd3624p-1')),
437 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
438 ([1e16, 1., 1e-16], 10000000000000002.0),
439 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
440 # exercise code for resizing partials array
441 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
442 [-2.**1022],
443 float.fromhex('0x1.5555555555555p+970')),
444 ]
445
446 for i, (vals, expected) in enumerate(test_values):
447 try:
448 actual = math.fsum(vals)
449 except OverflowError:
450 self.fail("test %d failed: got OverflowError, expected %r "
451 "for math.fsum(%.100r)" % (i, expected, vals))
452 except ValueError:
453 self.fail("test %d failed: got ValueError, expected %r "
454 "for math.fsum(%.100r)" % (i, expected, vals))
455 self.assertEqual(actual, expected)
456
457 from random import random, gauss, shuffle
458 for j in xrange(1000):
459 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
460 s = 0
461 for i in xrange(200):
462 v = gauss(0, random()) ** 7 - s
463 s += v
464 vals.append(v)
465 shuffle(vals)
466
467 s = msum(vals)
468 self.assertEqual(msum(vals), math.fsum(vals))
469
Georg Brandl2f037602006-10-28 13:51:49 +0000470 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000471 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000472 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
473 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes6f341092008-04-18 23:13:07 +0000474 self.assertEqual(math.hypot(NAN, INF), INF)
475 self.assertEqual(math.hypot(INF, NAN), INF)
476 self.assertEqual(math.hypot(NAN, NINF), INF)
477 self.assertEqual(math.hypot(NINF, NAN), INF)
478 self.assert_(math.isnan(math.hypot(1.0, NAN)))
479 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000480
Georg Brandl2f037602006-10-28 13:51:49 +0000481 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000482 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000483 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
484 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
485 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
486 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes6f341092008-04-18 23:13:07 +0000487 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
488 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
489 self.assertEquals(math.ldexp(1., -1000000), 0.)
490 self.assertEquals(math.ldexp(-1., -1000000), -0.)
491 self.assertEquals(math.ldexp(INF, 30), INF)
492 self.assertEquals(math.ldexp(NINF, -213), NINF)
493 self.assert_(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000494
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000495 # large second argument
496 for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
497 self.assertEquals(math.ldexp(INF, -n), INF)
498 self.assertEquals(math.ldexp(NINF, -n), NINF)
499 self.assertEquals(math.ldexp(1., -n), 0.)
500 self.assertEquals(math.ldexp(-1., -n), -0.)
501 self.assertEquals(math.ldexp(0., -n), 0.)
502 self.assertEquals(math.ldexp(-0., -n), -0.)
503 self.assert_(math.isnan(math.ldexp(NAN, -n)))
504
505 self.assertRaises(OverflowError, math.ldexp, 1., n)
506 self.assertRaises(OverflowError, math.ldexp, -1., n)
507 self.assertEquals(math.ldexp(0., n), 0.)
508 self.assertEquals(math.ldexp(-0., n), -0.)
509 self.assertEquals(math.ldexp(INF, n), INF)
510 self.assertEquals(math.ldexp(NINF, n), NINF)
511 self.assert_(math.isnan(math.ldexp(NAN, n)))
512
Georg Brandl2f037602006-10-28 13:51:49 +0000513 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000514 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000515 self.ftest('log(1/e)', math.log(1/math.e), -1)
516 self.ftest('log(1)', math.log(1), 0)
517 self.ftest('log(e)', math.log(math.e), 1)
518 self.ftest('log(32,2)', math.log(32,2), 5)
519 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
520 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000521 self.assertEquals(math.log(INF), INF)
522 self.assertRaises(ValueError, math.log, NINF)
523 self.assert_(math.isnan(math.log(NAN)))
524
525 def testLog1p(self):
526 self.assertRaises(TypeError, math.log1p)
527 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
528 self.ftest('log1p(0)', math.log1p(0), 0)
529 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
530 self.ftest('log1p(1)', math.log1p(1), math.log(2))
531 self.assertEquals(math.log1p(INF), INF)
532 self.assertRaises(ValueError, math.log1p, NINF)
533 self.assert_(math.isnan(math.log1p(NAN)))
534 n= 2**90
535 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
536 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000537
Georg Brandl2f037602006-10-28 13:51:49 +0000538 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000539 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000540 self.ftest('log10(0.1)', math.log10(0.1), -1)
541 self.ftest('log10(1)', math.log10(1), 0)
542 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes6f341092008-04-18 23:13:07 +0000543 self.assertEquals(math.log(INF), INF)
544 self.assertRaises(ValueError, math.log10, NINF)
545 self.assert_(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000546
Georg Brandl2f037602006-10-28 13:51:49 +0000547 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000548 self.assertRaises(TypeError, math.modf)
549
Mark Dickinson828ce4a2009-12-20 20:23:49 +0000550 def testmodf(name, result, expected):
551 (v1, v2), (e1, e2) = result, expected
Georg Brandl2f037602006-10-28 13:51:49 +0000552 if abs(v1-e1) > eps or abs(v2-e2):
553 self.fail('%s returned %r, expected %r'%\
554 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000555
Georg Brandl2f037602006-10-28 13:51:49 +0000556 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
557 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Tim Petersabd8a332006-11-03 02:32:46 +0000558
Christian Heimes6f341092008-04-18 23:13:07 +0000559 self.assertEquals(math.modf(INF), (0.0, INF))
560 self.assertEquals(math.modf(NINF), (-0.0, NINF))
561
562 modf_nan = math.modf(NAN)
563 self.assert_(math.isnan(modf_nan[0]))
564 self.assert_(math.isnan(modf_nan[1]))
565
Georg Brandl2f037602006-10-28 13:51:49 +0000566 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000567 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000568 self.ftest('pow(0,1)', math.pow(0,1), 0)
569 self.ftest('pow(1,0)', math.pow(1,0), 1)
570 self.ftest('pow(2,1)', math.pow(2,1), 2)
571 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes6f341092008-04-18 23:13:07 +0000572 self.assertEqual(math.pow(INF, 1), INF)
573 self.assertEqual(math.pow(NINF, 1), NINF)
574 self.assertEqual((math.pow(1, INF)), 1.)
575 self.assertEqual((math.pow(1, NINF)), 1.)
576 self.assert_(math.isnan(math.pow(NAN, 1)))
577 self.assert_(math.isnan(math.pow(2, NAN)))
578 self.assert_(math.isnan(math.pow(0, NAN)))
579 self.assertEqual(math.pow(1, NAN), 1)
Mark Dickinsone941d972008-04-19 18:51:48 +0000580
581 # pow(0., x)
582 self.assertEqual(math.pow(0., INF), 0.)
583 self.assertEqual(math.pow(0., 3.), 0.)
584 self.assertEqual(math.pow(0., 2.3), 0.)
585 self.assertEqual(math.pow(0., 2.), 0.)
586 self.assertEqual(math.pow(0., 0.), 1.)
587 self.assertEqual(math.pow(0., -0.), 1.)
588 self.assertRaises(ValueError, math.pow, 0., -2.)
589 self.assertRaises(ValueError, math.pow, 0., -2.3)
590 self.assertRaises(ValueError, math.pow, 0., -3.)
591 self.assertRaises(ValueError, math.pow, 0., NINF)
592 self.assert_(math.isnan(math.pow(0., NAN)))
593
594 # pow(INF, x)
595 self.assertEqual(math.pow(INF, INF), INF)
596 self.assertEqual(math.pow(INF, 3.), INF)
597 self.assertEqual(math.pow(INF, 2.3), INF)
598 self.assertEqual(math.pow(INF, 2.), INF)
599 self.assertEqual(math.pow(INF, 0.), 1.)
600 self.assertEqual(math.pow(INF, -0.), 1.)
601 self.assertEqual(math.pow(INF, -2.), 0.)
602 self.assertEqual(math.pow(INF, -2.3), 0.)
603 self.assertEqual(math.pow(INF, -3.), 0.)
604 self.assertEqual(math.pow(INF, NINF), 0.)
605 self.assert_(math.isnan(math.pow(INF, NAN)))
606
607 # pow(-0., x)
608 self.assertEqual(math.pow(-0., INF), 0.)
609 self.assertEqual(math.pow(-0., 3.), -0.)
610 self.assertEqual(math.pow(-0., 2.3), 0.)
611 self.assertEqual(math.pow(-0., 2.), 0.)
612 self.assertEqual(math.pow(-0., 0.), 1.)
613 self.assertEqual(math.pow(-0., -0.), 1.)
614 self.assertRaises(ValueError, math.pow, -0., -2.)
615 self.assertRaises(ValueError, math.pow, -0., -2.3)
616 self.assertRaises(ValueError, math.pow, -0., -3.)
617 self.assertRaises(ValueError, math.pow, -0., NINF)
618 self.assert_(math.isnan(math.pow(-0., NAN)))
619
620 # pow(NINF, x)
621 self.assertEqual(math.pow(NINF, INF), INF)
622 self.assertEqual(math.pow(NINF, 3.), NINF)
623 self.assertEqual(math.pow(NINF, 2.3), INF)
624 self.assertEqual(math.pow(NINF, 2.), INF)
625 self.assertEqual(math.pow(NINF, 0.), 1.)
626 self.assertEqual(math.pow(NINF, -0.), 1.)
627 self.assertEqual(math.pow(NINF, -2.), 0.)
628 self.assertEqual(math.pow(NINF, -2.3), 0.)
629 self.assertEqual(math.pow(NINF, -3.), -0.)
630 self.assertEqual(math.pow(NINF, NINF), 0.)
631 self.assert_(math.isnan(math.pow(NINF, NAN)))
632
633 # pow(-1, x)
634 self.assertEqual(math.pow(-1., INF), 1.)
635 self.assertEqual(math.pow(-1., 3.), -1.)
636 self.assertRaises(ValueError, math.pow, -1., 2.3)
637 self.assertEqual(math.pow(-1., 2.), 1.)
638 self.assertEqual(math.pow(-1., 0.), 1.)
639 self.assertEqual(math.pow(-1., -0.), 1.)
640 self.assertEqual(math.pow(-1., -2.), 1.)
641 self.assertRaises(ValueError, math.pow, -1., -2.3)
642 self.assertEqual(math.pow(-1., -3.), -1.)
643 self.assertEqual(math.pow(-1., NINF), 1.)
644 self.assert_(math.isnan(math.pow(-1., NAN)))
645
646 # pow(1, x)
647 self.assertEqual(math.pow(1., INF), 1.)
648 self.assertEqual(math.pow(1., 3.), 1.)
649 self.assertEqual(math.pow(1., 2.3), 1.)
650 self.assertEqual(math.pow(1., 2.), 1.)
651 self.assertEqual(math.pow(1., 0.), 1.)
652 self.assertEqual(math.pow(1., -0.), 1.)
653 self.assertEqual(math.pow(1., -2.), 1.)
654 self.assertEqual(math.pow(1., -2.3), 1.)
655 self.assertEqual(math.pow(1., -3.), 1.)
656 self.assertEqual(math.pow(1., NINF), 1.)
657 self.assertEqual(math.pow(1., NAN), 1.)
658
659 # pow(x, 0) should be 1 for any x
660 self.assertEqual(math.pow(2.3, 0.), 1.)
661 self.assertEqual(math.pow(-2.3, 0.), 1.)
662 self.assertEqual(math.pow(NAN, 0.), 1.)
663 self.assertEqual(math.pow(2.3, -0.), 1.)
664 self.assertEqual(math.pow(-2.3, -0.), 1.)
665 self.assertEqual(math.pow(NAN, -0.), 1.)
666
667 # pow(x, y) is invalid if x is negative and y is not integral
668 self.assertRaises(ValueError, math.pow, -1., 2.3)
669 self.assertRaises(ValueError, math.pow, -15., -3.1)
670
671 # pow(x, NINF)
672 self.assertEqual(math.pow(1.9, NINF), 0.)
673 self.assertEqual(math.pow(1.1, NINF), 0.)
674 self.assertEqual(math.pow(0.9, NINF), INF)
675 self.assertEqual(math.pow(0.1, NINF), INF)
676 self.assertEqual(math.pow(-0.1, NINF), INF)
677 self.assertEqual(math.pow(-0.9, NINF), INF)
678 self.assertEqual(math.pow(-1.1, NINF), 0.)
679 self.assertEqual(math.pow(-1.9, NINF), 0.)
680
681 # pow(x, INF)
682 self.assertEqual(math.pow(1.9, INF), INF)
683 self.assertEqual(math.pow(1.1, INF), INF)
684 self.assertEqual(math.pow(0.9, INF), 0.)
685 self.assertEqual(math.pow(0.1, INF), 0.)
686 self.assertEqual(math.pow(-0.1, INF), 0.)
687 self.assertEqual(math.pow(-0.9, INF), 0.)
688 self.assertEqual(math.pow(-1.1, INF), INF)
689 self.assertEqual(math.pow(-1.9, INF), INF)
690
Mark Dickinsoncec3f132008-04-20 04:13:13 +0000691 # pow(x, y) should work for x negative, y an integer
692 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
693 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
694 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
695 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
696 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
697 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
698 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
699 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
700 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
701 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
702
Mark Dickinsone941d972008-04-19 18:51:48 +0000703 # the following tests have been commented out since they don't
704 # really belong here: the implementation of ** for floats is
705 # independent of the implemention of math.pow
706 #self.assertEqual(1**NAN, 1)
707 #self.assertEqual(1**INF, 1)
708 #self.assertEqual(1**NINF, 1)
709 #self.assertEqual(1**0, 1)
710 #self.assertEqual(1.**NAN, 1)
711 #self.assertEqual(1.**INF, 1)
712 #self.assertEqual(1.**NINF, 1)
713 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000714
Georg Brandl2f037602006-10-28 13:51:49 +0000715 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000716 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000717 self.ftest('radians(180)', math.radians(180), math.pi)
718 self.ftest('radians(90)', math.radians(90), math.pi/2)
719 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000720
Georg Brandl2f037602006-10-28 13:51:49 +0000721 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000722 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000723 self.ftest('sin(0)', math.sin(0), 0)
724 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
725 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000726 try:
727 self.assert_(math.isnan(math.sin(INF)))
728 self.assert_(math.isnan(math.sin(NINF)))
729 except ValueError:
730 self.assertRaises(ValueError, math.sin, INF)
731 self.assertRaises(ValueError, math.sin, NINF)
732 self.assert_(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000733
Georg Brandl2f037602006-10-28 13:51:49 +0000734 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000735 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000736 self.ftest('sinh(0)', math.sinh(0), 0)
737 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
738 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000739 self.assertEquals(math.sinh(INF), INF)
Mark Dickinsone941d972008-04-19 18:51:48 +0000740 self.assertEquals(math.sinh(NINF), NINF)
Christian Heimes6f341092008-04-18 23:13:07 +0000741 self.assert_(math.isnan(math.sinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000742
Georg Brandl2f037602006-10-28 13:51:49 +0000743 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000744 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000745 self.ftest('sqrt(0)', math.sqrt(0), 0)
746 self.ftest('sqrt(1)', math.sqrt(1), 1)
747 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000748 self.assertEquals(math.sqrt(INF), INF)
749 self.assertRaises(ValueError, math.sqrt, NINF)
750 self.assert_(math.isnan(math.sqrt(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000751
Georg Brandl2f037602006-10-28 13:51:49 +0000752 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000753 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000754 self.ftest('tan(0)', math.tan(0), 0)
755 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
756 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000757 try:
758 self.assert_(math.isnan(math.tan(INF)))
759 self.assert_(math.isnan(math.tan(NINF)))
760 except:
761 self.assertRaises(ValueError, math.tan, INF)
762 self.assertRaises(ValueError, math.tan, NINF)
763 self.assert_(math.isnan(math.tan(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000764
Georg Brandl2f037602006-10-28 13:51:49 +0000765 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000766 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000767 self.ftest('tanh(0)', math.tanh(0), 0)
768 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000769 self.ftest('tanh(inf)', math.tanh(INF), 1)
770 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
771 self.assert_(math.isnan(math.tanh(NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000772 # check that tanh(-0.) == -0. on IEEE 754 systems
773 if float.__getformat__("double").startswith("IEEE"):
774 self.assertEqual(math.tanh(-0.), -0.)
775 self.assertEqual(math.copysign(1., math.tanh(-0.)),
776 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000777
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000778 def test_trunc(self):
779 self.assertEqual(math.trunc(1), 1)
780 self.assertEqual(math.trunc(-1), -1)
781 self.assertEqual(type(math.trunc(1)), int)
782 self.assertEqual(type(math.trunc(1.5)), int)
783 self.assertEqual(math.trunc(1.5), 1)
784 self.assertEqual(math.trunc(-1.5), -1)
785 self.assertEqual(math.trunc(1.999999), 1)
786 self.assertEqual(math.trunc(-1.999999), -1)
787 self.assertEqual(math.trunc(-0.999999), -0)
788 self.assertEqual(math.trunc(-100.999), -100)
789
790 class TestTrunc(object):
791 def __trunc__(self):
792 return 23
793
794 class TestNoTrunc(object):
795 pass
796
797 self.assertEqual(math.trunc(TestTrunc()), 23)
798
799 self.assertRaises(TypeError, math.trunc)
800 self.assertRaises(TypeError, math.trunc, 1, 2)
801 # XXX: This is not ideal, but see the comment in math_trunc().
802 self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
803
804 t = TestNoTrunc()
805 t.__trunc__ = lambda *args: args
806 self.assertEquals((), math.trunc(t))
807 self.assertRaises(TypeError, math.trunc, t, 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()