blob: 4a54a6ed9ed3360a599182e31b202a270e8ee4f2 [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
Guido van Rossumfcce6301996-08-08 18:26:25 +00009
Christian Heimes6f341092008-04-18 23:13:07 +000010eps = 1E-05
11NAN = float('nan')
12INF = float('inf')
13NINF = float('-inf')
14
15# locate file with test values
16if __name__ == '__main__':
17 file = sys.argv[0]
18else:
19 file = __file__
20test_dir = os.path.dirname(file) or os.curdir
21test_file = os.path.join(test_dir, 'cmath_testcases.txt')
22
23def parse_testfile(fname):
24 """Parse a file with test values
25
26 Empty lines or lines starting with -- are ignored
27 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
28 """
29 with open(fname) as fp:
30 for line in fp:
31 # skip comment lines and blank lines
32 if line.startswith('--') or not line.strip():
33 continue
34
35 lhs, rhs = line.split('->')
36 id, fn, arg_real, arg_imag = lhs.split()
37 rhs_pieces = rhs.split()
38 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
39 flags = rhs_pieces[2:]
40
41 yield (id, fn,
42 float(arg_real), float(arg_imag),
43 float(exp_real), float(exp_imag),
44 flags
45 )
Guido van Rossumfcce6301996-08-08 18:26:25 +000046
Georg Brandl2f037602006-10-28 13:51:49 +000047class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000048
Georg Brandl2f037602006-10-28 13:51:49 +000049 def ftest(self, name, value, expected):
50 if abs(value-expected) > eps:
Nick Coghlanec2ce9b2007-07-27 10:36:30 +000051 # Use %r instead of %f so the error message
52 # displays full precision. Otherwise discrepancies
53 # in the last few bits will lead to very confusing
54 # error messages
55 self.fail('%s returned %r, expected %r' %
Georg Brandl2f037602006-10-28 13:51:49 +000056 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000057
Georg Brandl2f037602006-10-28 13:51:49 +000058 def testConstants(self):
59 self.ftest('pi', math.pi, 3.1415926)
60 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000061
Georg Brandl2f037602006-10-28 13:51:49 +000062 def testAcos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000063 self.assertRaises(TypeError, math.acos)
Georg Brandl2f037602006-10-28 13:51:49 +000064 self.ftest('acos(-1)', math.acos(-1), math.pi)
65 self.ftest('acos(0)', math.acos(0), math.pi/2)
66 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +000067 self.assertRaises(ValueError, math.acos, INF)
68 self.assertRaises(ValueError, math.acos, NINF)
69 self.assert_(math.isnan(math.acos(NAN)))
70
71 def testAcosh(self):
72 self.assertRaises(TypeError, math.acosh)
73 self.ftest('acosh(1)', math.acosh(1), 0)
74 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
75 self.assertRaises(ValueError, math.acosh, 0)
76 self.assertRaises(ValueError, math.acosh, -1)
77 self.assertEquals(math.acosh(INF), INF)
78 self.assertRaises(ValueError, math.acosh, NINF)
79 self.assert_(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000080
Georg Brandl2f037602006-10-28 13:51:49 +000081 def testAsin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000082 self.assertRaises(TypeError, math.asin)
Georg Brandl2f037602006-10-28 13:51:49 +000083 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
84 self.ftest('asin(0)', math.asin(0), 0)
85 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes6f341092008-04-18 23:13:07 +000086 self.assertRaises(ValueError, math.asin, INF)
87 self.assertRaises(ValueError, math.asin, NINF)
88 self.assert_(math.isnan(math.asin(NAN)))
89
90 def testAsinh(self):
91 self.assertRaises(TypeError, math.asinh)
92 self.ftest('asinh(0)', math.asinh(0), 0)
93 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
94 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
95 self.assertEquals(math.asinh(INF), INF)
96 self.assertEquals(math.asinh(NINF), NINF)
97 self.assert_(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000098
Georg Brandl2f037602006-10-28 13:51:49 +000099 def testAtan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000100 self.assertRaises(TypeError, math.atan)
Georg Brandl2f037602006-10-28 13:51:49 +0000101 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
102 self.ftest('atan(0)', math.atan(0), 0)
103 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes6f341092008-04-18 23:13:07 +0000104 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Mark Dickinsone941d972008-04-19 18:51:48 +0000105 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Christian Heimes6f341092008-04-18 23:13:07 +0000106 self.assert_(math.isnan(math.atan(NAN)))
107
108 def testAtanh(self):
109 self.assertRaises(TypeError, math.atan)
110 self.ftest('atanh(0)', math.atanh(0), 0)
111 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
112 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
113 self.assertRaises(ValueError, math.atanh, 1)
114 self.assertRaises(ValueError, math.atanh, -1)
115 self.assertRaises(ValueError, math.atanh, INF)
116 self.assertRaises(ValueError, math.atanh, NINF)
117 self.assert_(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000118
Georg Brandl2f037602006-10-28 13:51:49 +0000119 def testAtan2(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000120 self.assertRaises(TypeError, math.atan2)
Georg Brandl2f037602006-10-28 13:51:49 +0000121 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
122 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
123 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
124 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
125 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000126
Georg Brandl2f037602006-10-28 13:51:49 +0000127 def testCeil(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000128 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000129 # These types will be int in py3k.
130 self.assertEquals(float, type(math.ceil(1)))
131 self.assertEquals(float, type(math.ceil(1L)))
132 self.assertEquals(float, type(math.ceil(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000133 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
134 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
135 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
136 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
137 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
138 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000139 self.assertEquals(math.ceil(INF), INF)
140 self.assertEquals(math.ceil(NINF), NINF)
141 self.assert_(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000142
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000143 class TestCeil(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000144 def __float__(self):
145 return 41.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000146 class TestNoCeil(object):
147 pass
148 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
149 self.assertRaises(TypeError, math.ceil, TestNoCeil())
150
151 t = TestNoCeil()
152 t.__ceil__ = lambda *args: args
153 self.assertRaises(TypeError, math.ceil, t)
154 self.assertRaises(TypeError, math.ceil, t, 0)
155
Christian Heimes6f341092008-04-18 23:13:07 +0000156 if float.__getformat__("double").startswith("IEEE"):
157 def testCopysign(self):
158 self.assertRaises(TypeError, math.copysign)
159 # copysign should let us distinguish signs of zeros
160 self.assertEquals(copysign(1., 0.), 1.)
161 self.assertEquals(copysign(1., -0.), -1.)
162 self.assertEquals(copysign(INF, 0.), INF)
163 self.assertEquals(copysign(INF, -0.), NINF)
164 self.assertEquals(copysign(NINF, 0.), INF)
165 self.assertEquals(copysign(NINF, -0.), NINF)
166 # and of infinities
167 self.assertEquals(copysign(1., INF), 1.)
168 self.assertEquals(copysign(1., NINF), -1.)
169 self.assertEquals(copysign(INF, INF), INF)
170 self.assertEquals(copysign(INF, NINF), NINF)
171 self.assertEquals(copysign(NINF, INF), INF)
172 self.assertEquals(copysign(NINF, NINF), NINF)
173 self.assert_(math.isnan(copysign(NAN, 1.)))
174 self.assert_(math.isnan(copysign(NAN, INF)))
175 self.assert_(math.isnan(copysign(NAN, NINF)))
176 self.assert_(math.isnan(copysign(NAN, NAN)))
177 # copysign(INF, NAN) may be INF or it may be NINF, since
178 # we don't know whether the sign bit of NAN is set on any
179 # given platform.
180 self.assert_(math.isinf(copysign(INF, NAN)))
181 # similarly, copysign(2., NAN) could be 2. or -2.
182 self.assertEquals(abs(copysign(2., NAN)), 2.)
183
Georg Brandl2f037602006-10-28 13:51:49 +0000184 def testCos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000185 self.assertRaises(TypeError, math.cos)
Georg Brandl2f037602006-10-28 13:51:49 +0000186 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
187 self.ftest('cos(0)', math.cos(0), 1)
188 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
189 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000190 try:
191 self.assert_(math.isnan(math.cos(INF)))
192 self.assert_(math.isnan(math.cos(NINF)))
193 except ValueError:
194 self.assertRaises(ValueError, math.cos, INF)
195 self.assertRaises(ValueError, math.cos, NINF)
196 self.assert_(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000197
Georg Brandl2f037602006-10-28 13:51:49 +0000198 def testCosh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000199 self.assertRaises(TypeError, math.cosh)
Georg Brandl2f037602006-10-28 13:51:49 +0000200 self.ftest('cosh(0)', math.cosh(0), 1)
201 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 +0000202 self.assertEquals(math.cosh(INF), INF)
203 self.assertEquals(math.cosh(NINF), INF)
204 self.assert_(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000205
Georg Brandl2f037602006-10-28 13:51:49 +0000206 def testDegrees(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000207 self.assertRaises(TypeError, math.degrees)
Georg Brandl2f037602006-10-28 13:51:49 +0000208 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
209 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
210 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000211
Georg Brandl2f037602006-10-28 13:51:49 +0000212 def testExp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000213 self.assertRaises(TypeError, math.exp)
Georg Brandl2f037602006-10-28 13:51:49 +0000214 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
215 self.ftest('exp(0)', math.exp(0), 1)
216 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes6f341092008-04-18 23:13:07 +0000217 self.assertEquals(math.exp(INF), INF)
218 self.assertEquals(math.exp(NINF), 0.)
219 self.assert_(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000220
Georg Brandl2f037602006-10-28 13:51:49 +0000221 def testFabs(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000222 self.assertRaises(TypeError, math.fabs)
Georg Brandl2f037602006-10-28 13:51:49 +0000223 self.ftest('fabs(-1)', math.fabs(-1), 1)
224 self.ftest('fabs(0)', math.fabs(0), 0)
225 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000226
Georg Brandl2f037602006-10-28 13:51:49 +0000227 def testFloor(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000228 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000229 # These types will be int in py3k.
230 self.assertEquals(float, type(math.floor(1)))
231 self.assertEquals(float, type(math.floor(1L)))
232 self.assertEquals(float, type(math.floor(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000233 self.ftest('floor(0.5)', math.floor(0.5), 0)
234 self.ftest('floor(1.0)', math.floor(1.0), 1)
235 self.ftest('floor(1.5)', math.floor(1.5), 1)
236 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
237 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
238 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Nick Coghlan00f20292007-07-26 14:03:00 +0000239 # pow() relies on floor() to check for integers
240 # This fails on some platforms - so check it here
241 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
242 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes6f341092008-04-18 23:13:07 +0000243 self.assertEquals(math.ceil(INF), INF)
244 self.assertEquals(math.ceil(NINF), NINF)
245 self.assert_(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000246
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000247 class TestFloor(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000248 def __float__(self):
249 return 42.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000250 class TestNoFloor(object):
251 pass
252 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
253 self.assertRaises(TypeError, math.floor, TestNoFloor())
254
255 t = TestNoFloor()
256 t.__floor__ = lambda *args: args
257 self.assertRaises(TypeError, math.floor, t)
258 self.assertRaises(TypeError, math.floor, t, 0)
259
Georg Brandl2f037602006-10-28 13:51:49 +0000260 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000261 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +0000262 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
263 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
264 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
265 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
266 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
267 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000268 self.assert_(math.isnan(math.fmod(NAN, 1.)))
269 self.assert_(math.isnan(math.fmod(1., NAN)))
270 self.assert_(math.isnan(math.fmod(NAN, NAN)))
271 self.assertRaises(ValueError, math.fmod, 1., 0.)
272 self.assertRaises(ValueError, math.fmod, INF, 1.)
273 self.assertRaises(ValueError, math.fmod, NINF, 1.)
274 self.assertRaises(ValueError, math.fmod, INF, 0.)
275 self.assertEquals(math.fmod(3.0, INF), 3.0)
276 self.assertEquals(math.fmod(-3.0, INF), -3.0)
277 self.assertEquals(math.fmod(3.0, NINF), 3.0)
278 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
279 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
280 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000281
Georg Brandl2f037602006-10-28 13:51:49 +0000282 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000283 self.assertRaises(TypeError, math.frexp)
284
Georg Brandl2f037602006-10-28 13:51:49 +0000285 def testfrexp(name, (mant, exp), (emant, eexp)):
286 if abs(mant-emant) > eps or exp != eexp:
287 self.fail('%s returned %r, expected %r'%\
288 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000289
Georg Brandl2f037602006-10-28 13:51:49 +0000290 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
291 testfrexp('frexp(0)', math.frexp(0), (0, 0))
292 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
293 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000294
Christian Heimes6f341092008-04-18 23:13:07 +0000295 self.assertEquals(math.frexp(INF)[0], INF)
296 self.assertEquals(math.frexp(NINF)[0], NINF)
297 self.assert_(math.isnan(math.frexp(NAN)[0]))
298
Georg Brandl2f037602006-10-28 13:51:49 +0000299 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000300 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000301 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
302 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes6f341092008-04-18 23:13:07 +0000303 self.assertEqual(math.hypot(NAN, INF), INF)
304 self.assertEqual(math.hypot(INF, NAN), INF)
305 self.assertEqual(math.hypot(NAN, NINF), INF)
306 self.assertEqual(math.hypot(NINF, NAN), INF)
307 self.assert_(math.isnan(math.hypot(1.0, NAN)))
308 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000309
Georg Brandl2f037602006-10-28 13:51:49 +0000310 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000311 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000312 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
313 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
314 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
315 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes6f341092008-04-18 23:13:07 +0000316 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
317 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
318 self.assertEquals(math.ldexp(1., -1000000), 0.)
319 self.assertEquals(math.ldexp(-1., -1000000), -0.)
320 self.assertEquals(math.ldexp(INF, 30), INF)
321 self.assertEquals(math.ldexp(NINF, -213), NINF)
322 self.assert_(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000323
Georg Brandl2f037602006-10-28 13:51:49 +0000324 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000325 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000326 self.ftest('log(1/e)', math.log(1/math.e), -1)
327 self.ftest('log(1)', math.log(1), 0)
328 self.ftest('log(e)', math.log(math.e), 1)
329 self.ftest('log(32,2)', math.log(32,2), 5)
330 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
331 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000332 self.assertEquals(math.log(INF), INF)
333 self.assertRaises(ValueError, math.log, NINF)
334 self.assert_(math.isnan(math.log(NAN)))
335
336 def testLog1p(self):
337 self.assertRaises(TypeError, math.log1p)
338 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
339 self.ftest('log1p(0)', math.log1p(0), 0)
340 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
341 self.ftest('log1p(1)', math.log1p(1), math.log(2))
342 self.assertEquals(math.log1p(INF), INF)
343 self.assertRaises(ValueError, math.log1p, NINF)
344 self.assert_(math.isnan(math.log1p(NAN)))
345 n= 2**90
346 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
347 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000348
Georg Brandl2f037602006-10-28 13:51:49 +0000349 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000350 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000351 self.ftest('log10(0.1)', math.log10(0.1), -1)
352 self.ftest('log10(1)', math.log10(1), 0)
353 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes6f341092008-04-18 23:13:07 +0000354 self.assertEquals(math.log(INF), INF)
355 self.assertRaises(ValueError, math.log10, NINF)
356 self.assert_(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000357
Georg Brandl2f037602006-10-28 13:51:49 +0000358 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000359 self.assertRaises(TypeError, math.modf)
360
Georg Brandl2f037602006-10-28 13:51:49 +0000361 def testmodf(name, (v1, v2), (e1, e2)):
362 if abs(v1-e1) > eps or abs(v2-e2):
363 self.fail('%s returned %r, expected %r'%\
364 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000365
Georg Brandl2f037602006-10-28 13:51:49 +0000366 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
367 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Tim Petersabd8a332006-11-03 02:32:46 +0000368
Christian Heimes6f341092008-04-18 23:13:07 +0000369 self.assertEquals(math.modf(INF), (0.0, INF))
370 self.assertEquals(math.modf(NINF), (-0.0, NINF))
371
372 modf_nan = math.modf(NAN)
373 self.assert_(math.isnan(modf_nan[0]))
374 self.assert_(math.isnan(modf_nan[1]))
375
Georg Brandl2f037602006-10-28 13:51:49 +0000376 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000377 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000378 self.ftest('pow(0,1)', math.pow(0,1), 0)
379 self.ftest('pow(1,0)', math.pow(1,0), 1)
380 self.ftest('pow(2,1)', math.pow(2,1), 2)
381 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes6f341092008-04-18 23:13:07 +0000382 self.assertEqual(math.pow(INF, 1), INF)
383 self.assertEqual(math.pow(NINF, 1), NINF)
384 self.assertEqual((math.pow(1, INF)), 1.)
385 self.assertEqual((math.pow(1, NINF)), 1.)
386 self.assert_(math.isnan(math.pow(NAN, 1)))
387 self.assert_(math.isnan(math.pow(2, NAN)))
388 self.assert_(math.isnan(math.pow(0, NAN)))
389 self.assertEqual(math.pow(1, NAN), 1)
Mark Dickinsone941d972008-04-19 18:51:48 +0000390
391 # pow(0., x)
392 self.assertEqual(math.pow(0., INF), 0.)
393 self.assertEqual(math.pow(0., 3.), 0.)
394 self.assertEqual(math.pow(0., 2.3), 0.)
395 self.assertEqual(math.pow(0., 2.), 0.)
396 self.assertEqual(math.pow(0., 0.), 1.)
397 self.assertEqual(math.pow(0., -0.), 1.)
398 self.assertRaises(ValueError, math.pow, 0., -2.)
399 self.assertRaises(ValueError, math.pow, 0., -2.3)
400 self.assertRaises(ValueError, math.pow, 0., -3.)
401 self.assertRaises(ValueError, math.pow, 0., NINF)
402 self.assert_(math.isnan(math.pow(0., NAN)))
403
404 # pow(INF, x)
405 self.assertEqual(math.pow(INF, INF), INF)
406 self.assertEqual(math.pow(INF, 3.), INF)
407 self.assertEqual(math.pow(INF, 2.3), INF)
408 self.assertEqual(math.pow(INF, 2.), INF)
409 self.assertEqual(math.pow(INF, 0.), 1.)
410 self.assertEqual(math.pow(INF, -0.), 1.)
411 self.assertEqual(math.pow(INF, -2.), 0.)
412 self.assertEqual(math.pow(INF, -2.3), 0.)
413 self.assertEqual(math.pow(INF, -3.), 0.)
414 self.assertEqual(math.pow(INF, NINF), 0.)
415 self.assert_(math.isnan(math.pow(INF, NAN)))
416
417 # pow(-0., x)
418 self.assertEqual(math.pow(-0., INF), 0.)
419 self.assertEqual(math.pow(-0., 3.), -0.)
420 self.assertEqual(math.pow(-0., 2.3), 0.)
421 self.assertEqual(math.pow(-0., 2.), 0.)
422 self.assertEqual(math.pow(-0., 0.), 1.)
423 self.assertEqual(math.pow(-0., -0.), 1.)
424 self.assertRaises(ValueError, math.pow, -0., -2.)
425 self.assertRaises(ValueError, math.pow, -0., -2.3)
426 self.assertRaises(ValueError, math.pow, -0., -3.)
427 self.assertRaises(ValueError, math.pow, -0., NINF)
428 self.assert_(math.isnan(math.pow(-0., NAN)))
429
430 # pow(NINF, x)
431 self.assertEqual(math.pow(NINF, INF), INF)
432 self.assertEqual(math.pow(NINF, 3.), NINF)
433 self.assertEqual(math.pow(NINF, 2.3), INF)
434 self.assertEqual(math.pow(NINF, 2.), INF)
435 self.assertEqual(math.pow(NINF, 0.), 1.)
436 self.assertEqual(math.pow(NINF, -0.), 1.)
437 self.assertEqual(math.pow(NINF, -2.), 0.)
438 self.assertEqual(math.pow(NINF, -2.3), 0.)
439 self.assertEqual(math.pow(NINF, -3.), -0.)
440 self.assertEqual(math.pow(NINF, NINF), 0.)
441 self.assert_(math.isnan(math.pow(NINF, NAN)))
442
443 # pow(-1, x)
444 self.assertEqual(math.pow(-1., INF), 1.)
445 self.assertEqual(math.pow(-1., 3.), -1.)
446 self.assertRaises(ValueError, math.pow, -1., 2.3)
447 self.assertEqual(math.pow(-1., 2.), 1.)
448 self.assertEqual(math.pow(-1., 0.), 1.)
449 self.assertEqual(math.pow(-1., -0.), 1.)
450 self.assertEqual(math.pow(-1., -2.), 1.)
451 self.assertRaises(ValueError, math.pow, -1., -2.3)
452 self.assertEqual(math.pow(-1., -3.), -1.)
453 self.assertEqual(math.pow(-1., NINF), 1.)
454 self.assert_(math.isnan(math.pow(-1., NAN)))
455
456 # pow(1, x)
457 self.assertEqual(math.pow(1., INF), 1.)
458 self.assertEqual(math.pow(1., 3.), 1.)
459 self.assertEqual(math.pow(1., 2.3), 1.)
460 self.assertEqual(math.pow(1., 2.), 1.)
461 self.assertEqual(math.pow(1., 0.), 1.)
462 self.assertEqual(math.pow(1., -0.), 1.)
463 self.assertEqual(math.pow(1., -2.), 1.)
464 self.assertEqual(math.pow(1., -2.3), 1.)
465 self.assertEqual(math.pow(1., -3.), 1.)
466 self.assertEqual(math.pow(1., NINF), 1.)
467 self.assertEqual(math.pow(1., NAN), 1.)
468
469 # pow(x, 0) should be 1 for any x
470 self.assertEqual(math.pow(2.3, 0.), 1.)
471 self.assertEqual(math.pow(-2.3, 0.), 1.)
472 self.assertEqual(math.pow(NAN, 0.), 1.)
473 self.assertEqual(math.pow(2.3, -0.), 1.)
474 self.assertEqual(math.pow(-2.3, -0.), 1.)
475 self.assertEqual(math.pow(NAN, -0.), 1.)
476
477 # pow(x, y) is invalid if x is negative and y is not integral
478 self.assertRaises(ValueError, math.pow, -1., 2.3)
479 self.assertRaises(ValueError, math.pow, -15., -3.1)
480
481 # pow(x, NINF)
482 self.assertEqual(math.pow(1.9, NINF), 0.)
483 self.assertEqual(math.pow(1.1, NINF), 0.)
484 self.assertEqual(math.pow(0.9, NINF), INF)
485 self.assertEqual(math.pow(0.1, NINF), INF)
486 self.assertEqual(math.pow(-0.1, NINF), INF)
487 self.assertEqual(math.pow(-0.9, NINF), INF)
488 self.assertEqual(math.pow(-1.1, NINF), 0.)
489 self.assertEqual(math.pow(-1.9, NINF), 0.)
490
491 # pow(x, INF)
492 self.assertEqual(math.pow(1.9, INF), INF)
493 self.assertEqual(math.pow(1.1, INF), INF)
494 self.assertEqual(math.pow(0.9, INF), 0.)
495 self.assertEqual(math.pow(0.1, INF), 0.)
496 self.assertEqual(math.pow(-0.1, INF), 0.)
497 self.assertEqual(math.pow(-0.9, INF), 0.)
498 self.assertEqual(math.pow(-1.1, INF), INF)
499 self.assertEqual(math.pow(-1.9, INF), INF)
500
501 # the following tests have been commented out since they don't
502 # really belong here: the implementation of ** for floats is
503 # independent of the implemention of math.pow
504 #self.assertEqual(1**NAN, 1)
505 #self.assertEqual(1**INF, 1)
506 #self.assertEqual(1**NINF, 1)
507 #self.assertEqual(1**0, 1)
508 #self.assertEqual(1.**NAN, 1)
509 #self.assertEqual(1.**INF, 1)
510 #self.assertEqual(1.**NINF, 1)
511 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000512
Georg Brandl2f037602006-10-28 13:51:49 +0000513 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000514 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000515 self.ftest('radians(180)', math.radians(180), math.pi)
516 self.ftest('radians(90)', math.radians(90), math.pi/2)
517 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000518
Georg Brandl2f037602006-10-28 13:51:49 +0000519 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000520 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000521 self.ftest('sin(0)', math.sin(0), 0)
522 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
523 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000524 try:
525 self.assert_(math.isnan(math.sin(INF)))
526 self.assert_(math.isnan(math.sin(NINF)))
527 except ValueError:
528 self.assertRaises(ValueError, math.sin, INF)
529 self.assertRaises(ValueError, math.sin, NINF)
530 self.assert_(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000531
Georg Brandl2f037602006-10-28 13:51:49 +0000532 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000533 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000534 self.ftest('sinh(0)', math.sinh(0), 0)
535 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
536 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000537 self.assertEquals(math.sinh(INF), INF)
Mark Dickinsone941d972008-04-19 18:51:48 +0000538 self.assertEquals(math.sinh(NINF), NINF)
Christian Heimes6f341092008-04-18 23:13:07 +0000539 self.assert_(math.isnan(math.sinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000540
Georg Brandl2f037602006-10-28 13:51:49 +0000541 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000542 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000543 self.ftest('sqrt(0)', math.sqrt(0), 0)
544 self.ftest('sqrt(1)', math.sqrt(1), 1)
545 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000546 self.assertEquals(math.sqrt(INF), INF)
547 self.assertRaises(ValueError, math.sqrt, NINF)
548 self.assert_(math.isnan(math.sqrt(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000549
Georg Brandl2f037602006-10-28 13:51:49 +0000550 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000551 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000552 self.ftest('tan(0)', math.tan(0), 0)
553 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
554 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000555 try:
556 self.assert_(math.isnan(math.tan(INF)))
557 self.assert_(math.isnan(math.tan(NINF)))
558 except:
559 self.assertRaises(ValueError, math.tan, INF)
560 self.assertRaises(ValueError, math.tan, NINF)
561 self.assert_(math.isnan(math.tan(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000562
Georg Brandl2f037602006-10-28 13:51:49 +0000563 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000564 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000565 self.ftest('tanh(0)', math.tanh(0), 0)
566 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000567 self.ftest('tanh(inf)', math.tanh(INF), 1)
568 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
569 self.assert_(math.isnan(math.tanh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000570
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000571 def test_trunc(self):
572 self.assertEqual(math.trunc(1), 1)
573 self.assertEqual(math.trunc(-1), -1)
574 self.assertEqual(type(math.trunc(1)), int)
575 self.assertEqual(type(math.trunc(1.5)), int)
576 self.assertEqual(math.trunc(1.5), 1)
577 self.assertEqual(math.trunc(-1.5), -1)
578 self.assertEqual(math.trunc(1.999999), 1)
579 self.assertEqual(math.trunc(-1.999999), -1)
580 self.assertEqual(math.trunc(-0.999999), -0)
581 self.assertEqual(math.trunc(-100.999), -100)
582
583 class TestTrunc(object):
584 def __trunc__(self):
585 return 23
586
587 class TestNoTrunc(object):
588 pass
589
590 self.assertEqual(math.trunc(TestTrunc()), 23)
591
592 self.assertRaises(TypeError, math.trunc)
593 self.assertRaises(TypeError, math.trunc, 1, 2)
594 # XXX: This is not ideal, but see the comment in math_trunc().
595 self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
596
597 t = TestNoTrunc()
598 t.__trunc__ = lambda *args: args
599 self.assertEquals((), math.trunc(t))
600 self.assertRaises(TypeError, math.trunc, t, 0)
601
Christian Heimeseebb79c2008-01-03 22:32:26 +0000602 def testCopysign(self):
603 self.assertEqual(math.copysign(1, 42), 1.0)
604 self.assertEqual(math.copysign(0., 42), 0.0)
605 self.assertEqual(math.copysign(1., -42), -1.0)
606 self.assertEqual(math.copysign(3, 0.), 3.0)
607 self.assertEqual(math.copysign(4., -0.), -4.0)
608
Christian Heimese2ca4242008-01-03 20:23:15 +0000609 def testIsnan(self):
610 self.assert_(math.isnan(float("nan")))
611 self.assert_(math.isnan(float("inf")* 0.))
612 self.failIf(math.isnan(float("inf")))
613 self.failIf(math.isnan(0.))
614 self.failIf(math.isnan(1.))
615
616 def testIsinf(self):
617 self.assert_(math.isinf(float("inf")))
618 self.assert_(math.isinf(float("-inf")))
619 self.assert_(math.isinf(1E400))
620 self.assert_(math.isinf(-1E400))
621 self.failIf(math.isinf(float("nan")))
622 self.failIf(math.isinf(0.))
623 self.failIf(math.isinf(1.))
624
Georg Brandl2f037602006-10-28 13:51:49 +0000625 # RED_FLAG 16-Oct-2000 Tim
626 # While 2.0 is more consistent about exceptions than previous releases, it
627 # still fails this part of the test on some platforms. For now, we only
628 # *run* test_exceptions() in verbose mode, so that this isn't normally
629 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000630
Georg Brandl2f037602006-10-28 13:51:49 +0000631 if verbose:
632 def test_exceptions(self):
633 try:
634 x = math.exp(-1000000000)
635 except:
636 # mathmodule.c is failing to weed out underflows from libm, or
637 # we've got an fp format with huge dynamic range
638 self.fail("underflowing exp() should not have raised "
639 "an exception")
640 if x != 0:
641 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000642
Georg Brandl2f037602006-10-28 13:51:49 +0000643 # If this fails, probably using a strict IEEE-754 conforming libm, and x
644 # is +Inf afterwards. But Python wants overflows detected by default.
645 try:
646 x = math.exp(1000000000)
647 except OverflowError:
648 pass
649 else:
650 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000651
Georg Brandl2f037602006-10-28 13:51:49 +0000652 # If this fails, it could be a puzzle. One odd possibility is that
653 # mathmodule.c's macros are getting confused while comparing
654 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
655 # as a result (and so raising OverflowError instead).
656 try:
657 x = math.sqrt(-1.0)
658 except ValueError:
659 pass
660 else:
661 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000662
Christian Heimes6f341092008-04-18 23:13:07 +0000663 def test_testfile(self):
664 if not float.__getformat__("double").startswith("IEEE"):
665 return
666 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
667 # Skip if either the input or result is complex, or if
668 # flags is nonempty
669 if ai != 0. or ei != 0. or flags:
670 continue
671 if fn in ['rect', 'polar']:
672 # no real versions of rect, polar
673 continue
674 func = getattr(math, fn)
Mark Dickinson9f99d702008-04-20 01:22:30 +0000675 try:
676 result = func(ar)
677 except ValueError:
678 message = ("Unexpected ValueError in " +
679 "test %s:%s(%r)\n" % (id, fn, ar))
680 self.fail(message)
Christian Heimes6f341092008-04-18 23:13:07 +0000681 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Georg Brandl2f037602006-10-28 13:51:49 +0000682
683def test_main():
Christian Heimes6f341092008-04-18 23:13:07 +0000684 from doctest import DocFileSuite
685 suite = unittest.TestSuite()
686 suite.addTest(unittest.makeSuite(MathTests))
687 suite.addTest(DocFileSuite("ieee754.txt"))
688 run_unittest(suite)
Georg Brandl2f037602006-10-28 13:51:49 +0000689
690if __name__ == '__main__':
691 test_main()