blob: 8486b0bd86ed3bb4d91a66f6793f98c0c74b5c7e [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
Mark Dickinsonb93fff02009-09-28 18:54:55 +000010import struct
Guido van Rossumfcce6301996-08-08 18:26:25 +000011
Christian Heimes6f341092008-04-18 23:13:07 +000012eps = 1E-05
13NAN = float('nan')
14INF = float('inf')
15NINF = float('-inf')
16
Mark Dickinson2985dbb2009-09-18 21:01:50 +000017# decorator for skipping tests on non-IEEE 754 platforms
18requires_IEEE_754 = unittest.skipUnless(
19 float.__getformat__("double").startswith("IEEE"),
20 "test requires IEEE 754 doubles")
21
Mark Dickinson6ab635a2009-04-24 16:34:14 +000022# detect evidence of double-rounding: fsum is not always correctly
23# rounded on machines that suffer from double rounding.
24x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
25HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
26
Christian Heimes6f341092008-04-18 23:13:07 +000027# locate file with test values
28if __name__ == '__main__':
29 file = sys.argv[0]
30else:
31 file = __file__
32test_dir = os.path.dirname(file) or os.curdir
Mark Dickinsonb93fff02009-09-28 18:54:55 +000033math_testcases = os.path.join(test_dir, 'math_testcases.txt')
Christian Heimes6f341092008-04-18 23:13:07 +000034test_file = os.path.join(test_dir, 'cmath_testcases.txt')
35
Mark Dickinsonb93fff02009-09-28 18:54:55 +000036def to_ulps(x):
37 """Convert a non-NaN float x to an integer, in such a way that
38 adjacent floats are converted to adjacent integers. Then
39 abs(ulps(x) - ulps(y)) gives the difference in ulps between two
40 floats.
41
42 The results from this function will only make sense on platforms
43 where C doubles are represented in IEEE 754 binary64 format.
44
45 """
46 n = struct.unpack('q', struct.pack('<d', x))[0]
47 if n < 0:
48 n = ~(n+2**63)
49 return n
50
51
52def parse_mtestfile(fname):
53 """Parse a file with test values
54
55 -- starts a comment
56 blank lines, or lines containing only a comment, are ignored
57 other lines are expected to have the form
58 id fn arg -> expected [flag]*
59
60 """
61 with open(fname) as fp:
62 for line in fp:
63 # strip comments, and skip blank lines
64 if '--' in line:
65 line = line[:line.index('--')]
66 if not line.strip():
67 continue
68
69 lhs, rhs = line.split('->')
70 id, fn, arg = lhs.split()
71 rhs_pieces = rhs.split()
72 exp = rhs_pieces[0]
73 flags = rhs_pieces[1:]
74
75 yield (id, fn, float(arg), float(exp), flags)
76
Christian Heimes6f341092008-04-18 23:13:07 +000077def parse_testfile(fname):
78 """Parse a file with test values
79
80 Empty lines or lines starting with -- are ignored
81 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
82 """
83 with open(fname) as fp:
84 for line in fp:
85 # skip comment lines and blank lines
86 if line.startswith('--') or not line.strip():
87 continue
88
89 lhs, rhs = line.split('->')
90 id, fn, arg_real, arg_imag = lhs.split()
91 rhs_pieces = rhs.split()
92 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
93 flags = rhs_pieces[2:]
94
95 yield (id, fn,
96 float(arg_real), float(arg_imag),
97 float(exp_real), float(exp_imag),
98 flags
99 )
Guido van Rossumfcce6301996-08-08 18:26:25 +0000100
Georg Brandl2f037602006-10-28 13:51:49 +0000101class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +0000102
Georg Brandl2f037602006-10-28 13:51:49 +0000103 def ftest(self, name, value, expected):
104 if abs(value-expected) > eps:
Nick Coghlanec2ce9b2007-07-27 10:36:30 +0000105 # Use %r instead of %f so the error message
106 # displays full precision. Otherwise discrepancies
107 # in the last few bits will lead to very confusing
108 # error messages
109 self.fail('%s returned %r, expected %r' %
Georg Brandl2f037602006-10-28 13:51:49 +0000110 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000111
Georg Brandl2f037602006-10-28 13:51:49 +0000112 def testConstants(self):
113 self.ftest('pi', math.pi, 3.1415926)
114 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000115
Georg Brandl2f037602006-10-28 13:51:49 +0000116 def testAcos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000117 self.assertRaises(TypeError, math.acos)
Georg Brandl2f037602006-10-28 13:51:49 +0000118 self.ftest('acos(-1)', math.acos(-1), math.pi)
119 self.ftest('acos(0)', math.acos(0), math.pi/2)
120 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000121 self.assertRaises(ValueError, math.acos, INF)
122 self.assertRaises(ValueError, math.acos, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000123 self.assertTrue(math.isnan(math.acos(NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000124
125 def testAcosh(self):
126 self.assertRaises(TypeError, math.acosh)
127 self.ftest('acosh(1)', math.acosh(1), 0)
128 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
129 self.assertRaises(ValueError, math.acosh, 0)
130 self.assertRaises(ValueError, math.acosh, -1)
131 self.assertEquals(math.acosh(INF), INF)
132 self.assertRaises(ValueError, math.acosh, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000133 self.assertTrue(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000134
Georg Brandl2f037602006-10-28 13:51:49 +0000135 def testAsin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000136 self.assertRaises(TypeError, math.asin)
Georg Brandl2f037602006-10-28 13:51:49 +0000137 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
138 self.ftest('asin(0)', math.asin(0), 0)
139 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes6f341092008-04-18 23:13:07 +0000140 self.assertRaises(ValueError, math.asin, INF)
141 self.assertRaises(ValueError, math.asin, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000142 self.assertTrue(math.isnan(math.asin(NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000143
144 def testAsinh(self):
145 self.assertRaises(TypeError, math.asinh)
146 self.ftest('asinh(0)', math.asinh(0), 0)
147 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
148 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
149 self.assertEquals(math.asinh(INF), INF)
150 self.assertEquals(math.asinh(NINF), NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000151 self.assertTrue(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000152
Georg Brandl2f037602006-10-28 13:51:49 +0000153 def testAtan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000154 self.assertRaises(TypeError, math.atan)
Georg Brandl2f037602006-10-28 13:51:49 +0000155 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
156 self.ftest('atan(0)', math.atan(0), 0)
157 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes6f341092008-04-18 23:13:07 +0000158 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Mark Dickinsone941d972008-04-19 18:51:48 +0000159 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000160 self.assertTrue(math.isnan(math.atan(NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000161
162 def testAtanh(self):
163 self.assertRaises(TypeError, math.atan)
164 self.ftest('atanh(0)', math.atanh(0), 0)
165 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
166 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
167 self.assertRaises(ValueError, math.atanh, 1)
168 self.assertRaises(ValueError, math.atanh, -1)
169 self.assertRaises(ValueError, math.atanh, INF)
170 self.assertRaises(ValueError, math.atanh, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000171 self.assertTrue(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000172
Georg Brandl2f037602006-10-28 13:51:49 +0000173 def testAtan2(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000174 self.assertRaises(TypeError, math.atan2)
Georg Brandl2f037602006-10-28 13:51:49 +0000175 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
176 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
177 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
178 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
179 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000180
Mark Dickinsond6d51482008-04-20 20:38:48 +0000181 # math.atan2(0, x)
182 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
183 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
184 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
185 self.assertEqual(math.atan2(0., 0.), 0.)
186 self.assertEqual(math.atan2(0., 2.3), 0.)
187 self.assertEqual(math.atan2(0., INF), 0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000188 self.assertTrue(math.isnan(math.atan2(0., NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000189 # math.atan2(-0, x)
190 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
191 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
192 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
193 self.assertEqual(math.atan2(-0., 0.), -0.)
194 self.assertEqual(math.atan2(-0., 2.3), -0.)
195 self.assertEqual(math.atan2(-0., INF), -0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000196 self.assertTrue(math.isnan(math.atan2(-0., NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000197 # math.atan2(INF, x)
198 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
199 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
200 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
201 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
202 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
203 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000204 self.assertTrue(math.isnan(math.atan2(INF, NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000205 # math.atan2(NINF, x)
206 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
207 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
208 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
209 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
210 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
211 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000212 self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000213 # math.atan2(+finite, x)
214 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
215 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
216 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
217 self.assertEqual(math.atan2(2.3, INF), 0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000218 self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000219 # math.atan2(-finite, x)
220 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
221 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
222 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
223 self.assertEqual(math.atan2(-2.3, INF), -0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000224 self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000225 # math.atan2(NAN, x)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000226 self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
227 self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
228 self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
229 self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
230 self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
231 self.assertTrue(math.isnan(math.atan2(NAN, INF)))
232 self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000233
Georg Brandl2f037602006-10-28 13:51:49 +0000234 def testCeil(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000235 self.assertRaises(TypeError, math.ceil)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000236 # These types will be int in py3k.
237 self.assertEquals(float, type(math.ceil(1)))
238 self.assertEquals(float, type(math.ceil(1L)))
239 self.assertEquals(float, type(math.ceil(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000240 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
241 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
242 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
243 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
244 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
245 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000246 self.assertEquals(math.ceil(INF), INF)
247 self.assertEquals(math.ceil(NINF), NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000248 self.assertTrue(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000249
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000250 class TestCeil(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000251 def __float__(self):
252 return 41.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000253 class TestNoCeil(object):
254 pass
255 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
256 self.assertRaises(TypeError, math.ceil, TestNoCeil())
257
258 t = TestNoCeil()
259 t.__ceil__ = lambda *args: args
260 self.assertRaises(TypeError, math.ceil, t)
261 self.assertRaises(TypeError, math.ceil, t, 0)
262
Mark Dickinson2985dbb2009-09-18 21:01:50 +0000263 @requires_IEEE_754
264 def testCopysign(self):
265 self.assertRaises(TypeError, math.copysign)
266 # copysign should let us distinguish signs of zeros
267 self.assertEquals(copysign(1., 0.), 1.)
268 self.assertEquals(copysign(1., -0.), -1.)
269 self.assertEquals(copysign(INF, 0.), INF)
270 self.assertEquals(copysign(INF, -0.), NINF)
271 self.assertEquals(copysign(NINF, 0.), INF)
272 self.assertEquals(copysign(NINF, -0.), NINF)
273 # and of infinities
274 self.assertEquals(copysign(1., INF), 1.)
275 self.assertEquals(copysign(1., NINF), -1.)
276 self.assertEquals(copysign(INF, INF), INF)
277 self.assertEquals(copysign(INF, NINF), NINF)
278 self.assertEquals(copysign(NINF, INF), INF)
279 self.assertEquals(copysign(NINF, NINF), NINF)
280 self.assertTrue(math.isnan(copysign(NAN, 1.)))
281 self.assertTrue(math.isnan(copysign(NAN, INF)))
282 self.assertTrue(math.isnan(copysign(NAN, NINF)))
283 self.assertTrue(math.isnan(copysign(NAN, NAN)))
284 # copysign(INF, NAN) may be INF or it may be NINF, since
285 # we don't know whether the sign bit of NAN is set on any
286 # given platform.
287 self.assertTrue(math.isinf(copysign(INF, NAN)))
288 # similarly, copysign(2., NAN) could be 2. or -2.
289 self.assertEquals(abs(copysign(2., NAN)), 2.)
Christian Heimes6f341092008-04-18 23:13:07 +0000290
Georg Brandl2f037602006-10-28 13:51:49 +0000291 def testCos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000292 self.assertRaises(TypeError, math.cos)
Georg Brandl2f037602006-10-28 13:51:49 +0000293 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
294 self.ftest('cos(0)', math.cos(0), 1)
295 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
296 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000297 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000298 self.assertTrue(math.isnan(math.cos(INF)))
299 self.assertTrue(math.isnan(math.cos(NINF)))
Christian Heimes6f341092008-04-18 23:13:07 +0000300 except ValueError:
301 self.assertRaises(ValueError, math.cos, INF)
302 self.assertRaises(ValueError, math.cos, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000303 self.assertTrue(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000304
Georg Brandl2f037602006-10-28 13:51:49 +0000305 def testCosh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000306 self.assertRaises(TypeError, math.cosh)
Georg Brandl2f037602006-10-28 13:51:49 +0000307 self.ftest('cosh(0)', math.cosh(0), 1)
308 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 +0000309 self.assertEquals(math.cosh(INF), INF)
310 self.assertEquals(math.cosh(NINF), INF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000311 self.assertTrue(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000312
Georg Brandl2f037602006-10-28 13:51:49 +0000313 def testDegrees(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000314 self.assertRaises(TypeError, math.degrees)
Georg Brandl2f037602006-10-28 13:51:49 +0000315 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
316 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
317 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000318
Georg Brandl2f037602006-10-28 13:51:49 +0000319 def testExp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000320 self.assertRaises(TypeError, math.exp)
Georg Brandl2f037602006-10-28 13:51:49 +0000321 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
322 self.ftest('exp(0)', math.exp(0), 1)
323 self.ftest('exp(1)', math.exp(1), math.e)
Christian Heimes6f341092008-04-18 23:13:07 +0000324 self.assertEquals(math.exp(INF), INF)
325 self.assertEquals(math.exp(NINF), 0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000326 self.assertTrue(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000327
Georg Brandl2f037602006-10-28 13:51:49 +0000328 def testFabs(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000329 self.assertRaises(TypeError, math.fabs)
Georg Brandl2f037602006-10-28 13:51:49 +0000330 self.ftest('fabs(-1)', math.fabs(-1), 1)
331 self.ftest('fabs(0)', math.fabs(0), 0)
332 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000333
Raymond Hettingerecbdd2e2008-06-09 06:54:45 +0000334 def testFactorial(self):
335 def fact(n):
336 result = 1
337 for i in range(1, int(n)+1):
338 result *= i
339 return result
340 values = range(10) + [50, 100, 500]
341 random.shuffle(values)
342 for x in range(10):
343 for cast in (int, long, float):
344 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
345 self.assertRaises(ValueError, math.factorial, -1)
346 self.assertRaises(ValueError, math.factorial, math.pi)
347
Georg Brandl2f037602006-10-28 13:51:49 +0000348 def testFloor(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000349 self.assertRaises(TypeError, math.floor)
Jeffrey Yasskin737c73f2008-01-04 08:01:23 +0000350 # These types will be int in py3k.
351 self.assertEquals(float, type(math.floor(1)))
352 self.assertEquals(float, type(math.floor(1L)))
353 self.assertEquals(float, type(math.floor(1.0)))
Georg Brandl2f037602006-10-28 13:51:49 +0000354 self.ftest('floor(0.5)', math.floor(0.5), 0)
355 self.ftest('floor(1.0)', math.floor(1.0), 1)
356 self.ftest('floor(1.5)', math.floor(1.5), 1)
357 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
358 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
359 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Nick Coghlan00f20292007-07-26 14:03:00 +0000360 # pow() relies on floor() to check for integers
361 # This fails on some platforms - so check it here
362 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
363 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Christian Heimes6f341092008-04-18 23:13:07 +0000364 self.assertEquals(math.ceil(INF), INF)
365 self.assertEquals(math.ceil(NINF), NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000366 self.assertTrue(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000367
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000368 class TestFloor(object):
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000369 def __float__(self):
370 return 42.3
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000371 class TestNoFloor(object):
372 pass
373 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
374 self.assertRaises(TypeError, math.floor, TestNoFloor())
375
376 t = TestNoFloor()
377 t.__floor__ = lambda *args: args
378 self.assertRaises(TypeError, math.floor, t)
379 self.assertRaises(TypeError, math.floor, t, 0)
380
Georg Brandl2f037602006-10-28 13:51:49 +0000381 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000382 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +0000383 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
384 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
385 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
386 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
387 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
388 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000389 self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
390 self.assertTrue(math.isnan(math.fmod(1., NAN)))
391 self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000392 self.assertRaises(ValueError, math.fmod, 1., 0.)
393 self.assertRaises(ValueError, math.fmod, INF, 1.)
394 self.assertRaises(ValueError, math.fmod, NINF, 1.)
395 self.assertRaises(ValueError, math.fmod, INF, 0.)
396 self.assertEquals(math.fmod(3.0, INF), 3.0)
397 self.assertEquals(math.fmod(-3.0, INF), -3.0)
398 self.assertEquals(math.fmod(3.0, NINF), 3.0)
399 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
400 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
401 self.assertEquals(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000402
Georg Brandl2f037602006-10-28 13:51:49 +0000403 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000404 self.assertRaises(TypeError, math.frexp)
405
Georg Brandl2f037602006-10-28 13:51:49 +0000406 def testfrexp(name, (mant, exp), (emant, eexp)):
407 if abs(mant-emant) > eps or exp != eexp:
408 self.fail('%s returned %r, expected %r'%\
409 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000410
Georg Brandl2f037602006-10-28 13:51:49 +0000411 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
412 testfrexp('frexp(0)', math.frexp(0), (0, 0))
413 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
414 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000415
Christian Heimes6f341092008-04-18 23:13:07 +0000416 self.assertEquals(math.frexp(INF)[0], INF)
417 self.assertEquals(math.frexp(NINF)[0], NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000418 self.assertTrue(math.isnan(math.frexp(NAN)[0]))
Christian Heimes6f341092008-04-18 23:13:07 +0000419
Mark Dickinson2985dbb2009-09-18 21:01:50 +0000420 @requires_IEEE_754
Mark Dickinson0badeef2009-04-24 16:37:22 +0000421 @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
Mark Dickinson6ab635a2009-04-24 16:34:14 +0000422 "fsum is not exact on machines with double rounding")
Mark Dickinson0f6414a2008-07-31 14:48:32 +0000423 def testFsum(self):
424 # math.fsum relies on exact rounding for correct operation.
425 # There's a known problem with IA32 floating-point that causes
426 # inexact rounding in some situations, and will cause the
427 # math.fsum tests below to fail; see issue #2937. On non IEEE
428 # 754 platforms, and on IEEE 754 platforms that exhibit the
429 # problem described in issue #2937, we simply skip the whole
430 # test.
431
Mark Dickinson0f6414a2008-07-31 14:48:32 +0000432 # Python version of math.fsum, for comparison. Uses a
433 # different algorithm based on frexp, ldexp and integer
434 # arithmetic.
435 from sys import float_info
436 mant_dig = float_info.mant_dig
437 etiny = float_info.min_exp - mant_dig
438
439 def msum(iterable):
440 """Full precision summation. Compute sum(iterable) without any
441 intermediate accumulation of error. Based on the 'lsum' function
442 at http://code.activestate.com/recipes/393090/
443
444 """
445 tmant, texp = 0, 0
446 for x in iterable:
447 mant, exp = math.frexp(x)
448 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
449 if texp > exp:
450 tmant <<= texp-exp
451 texp = exp
452 else:
453 mant <<= exp-texp
454 tmant += mant
455 # Round tmant * 2**texp to a float. The original recipe
456 # used float(str(tmant)) * 2.0**texp for this, but that's
457 # a little unsafe because str -> float conversion can't be
458 # relied upon to do correct rounding on all platforms.
459 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
460 if tail > 0:
461 h = 1 << (tail-1)
462 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
463 texp += tail
464 return math.ldexp(tmant, texp)
465
466 test_values = [
467 ([], 0.0),
468 ([0.0], 0.0),
469 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
470 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
471 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
472 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
473 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
474 ([1./n for n in range(1, 1001)],
475 float.fromhex('0x1.df11f45f4e61ap+2')),
476 ([(-1.)**n/n for n in range(1, 1001)],
477 float.fromhex('-0x1.62a2af1bd3624p-1')),
478 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
479 ([1e16, 1., 1e-16], 10000000000000002.0),
480 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
481 # exercise code for resizing partials array
482 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
483 [-2.**1022],
484 float.fromhex('0x1.5555555555555p+970')),
485 ]
486
487 for i, (vals, expected) in enumerate(test_values):
488 try:
489 actual = math.fsum(vals)
490 except OverflowError:
491 self.fail("test %d failed: got OverflowError, expected %r "
492 "for math.fsum(%.100r)" % (i, expected, vals))
493 except ValueError:
494 self.fail("test %d failed: got ValueError, expected %r "
495 "for math.fsum(%.100r)" % (i, expected, vals))
496 self.assertEqual(actual, expected)
497
498 from random import random, gauss, shuffle
499 for j in xrange(1000):
500 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
501 s = 0
502 for i in xrange(200):
503 v = gauss(0, random()) ** 7 - s
504 s += v
505 vals.append(v)
506 shuffle(vals)
507
508 s = msum(vals)
509 self.assertEqual(msum(vals), math.fsum(vals))
510
Georg Brandl2f037602006-10-28 13:51:49 +0000511 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000512 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000513 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
514 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes6f341092008-04-18 23:13:07 +0000515 self.assertEqual(math.hypot(NAN, INF), INF)
516 self.assertEqual(math.hypot(INF, NAN), INF)
517 self.assertEqual(math.hypot(NAN, NINF), INF)
518 self.assertEqual(math.hypot(NINF, NAN), INF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000519 self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
520 self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000521
Georg Brandl2f037602006-10-28 13:51:49 +0000522 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000523 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000524 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
525 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
526 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
527 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes6f341092008-04-18 23:13:07 +0000528 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
529 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
530 self.assertEquals(math.ldexp(1., -1000000), 0.)
531 self.assertEquals(math.ldexp(-1., -1000000), -0.)
532 self.assertEquals(math.ldexp(INF, 30), INF)
533 self.assertEquals(math.ldexp(NINF, -213), NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000534 self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000535
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000536 # large second argument
537 for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
538 self.assertEquals(math.ldexp(INF, -n), INF)
539 self.assertEquals(math.ldexp(NINF, -n), NINF)
540 self.assertEquals(math.ldexp(1., -n), 0.)
541 self.assertEquals(math.ldexp(-1., -n), -0.)
542 self.assertEquals(math.ldexp(0., -n), 0.)
543 self.assertEquals(math.ldexp(-0., -n), -0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000544 self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000545
546 self.assertRaises(OverflowError, math.ldexp, 1., n)
547 self.assertRaises(OverflowError, math.ldexp, -1., n)
548 self.assertEquals(math.ldexp(0., n), 0.)
549 self.assertEquals(math.ldexp(-0., n), -0.)
550 self.assertEquals(math.ldexp(INF, n), INF)
551 self.assertEquals(math.ldexp(NINF, n), NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000552 self.assertTrue(math.isnan(math.ldexp(NAN, n)))
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000553
Georg Brandl2f037602006-10-28 13:51:49 +0000554 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000555 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000556 self.ftest('log(1/e)', math.log(1/math.e), -1)
557 self.ftest('log(1)', math.log(1), 0)
558 self.ftest('log(e)', math.log(math.e), 1)
559 self.ftest('log(32,2)', math.log(32,2), 5)
560 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
561 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000562 self.assertEquals(math.log(INF), INF)
563 self.assertRaises(ValueError, math.log, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000564 self.assertTrue(math.isnan(math.log(NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000565
566 def testLog1p(self):
567 self.assertRaises(TypeError, math.log1p)
568 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
569 self.ftest('log1p(0)', math.log1p(0), 0)
570 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
571 self.ftest('log1p(1)', math.log1p(1), math.log(2))
572 self.assertEquals(math.log1p(INF), INF)
573 self.assertRaises(ValueError, math.log1p, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000574 self.assertTrue(math.isnan(math.log1p(NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000575 n= 2**90
576 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
577 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000578
Georg Brandl2f037602006-10-28 13:51:49 +0000579 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000580 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000581 self.ftest('log10(0.1)', math.log10(0.1), -1)
582 self.ftest('log10(1)', math.log10(1), 0)
583 self.ftest('log10(10)', math.log10(10), 1)
Christian Heimes6f341092008-04-18 23:13:07 +0000584 self.assertEquals(math.log(INF), INF)
585 self.assertRaises(ValueError, math.log10, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000586 self.assertTrue(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000587
Georg Brandl2f037602006-10-28 13:51:49 +0000588 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000589 self.assertRaises(TypeError, math.modf)
590
Georg Brandl2f037602006-10-28 13:51:49 +0000591 def testmodf(name, (v1, v2), (e1, e2)):
592 if abs(v1-e1) > eps or abs(v2-e2):
593 self.fail('%s returned %r, expected %r'%\
594 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000595
Georg Brandl2f037602006-10-28 13:51:49 +0000596 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
597 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Tim Petersabd8a332006-11-03 02:32:46 +0000598
Christian Heimes6f341092008-04-18 23:13:07 +0000599 self.assertEquals(math.modf(INF), (0.0, INF))
600 self.assertEquals(math.modf(NINF), (-0.0, NINF))
601
602 modf_nan = math.modf(NAN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000603 self.assertTrue(math.isnan(modf_nan[0]))
604 self.assertTrue(math.isnan(modf_nan[1]))
Christian Heimes6f341092008-04-18 23:13:07 +0000605
Georg Brandl2f037602006-10-28 13:51:49 +0000606 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000607 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000608 self.ftest('pow(0,1)', math.pow(0,1), 0)
609 self.ftest('pow(1,0)', math.pow(1,0), 1)
610 self.ftest('pow(2,1)', math.pow(2,1), 2)
611 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes6f341092008-04-18 23:13:07 +0000612 self.assertEqual(math.pow(INF, 1), INF)
613 self.assertEqual(math.pow(NINF, 1), NINF)
614 self.assertEqual((math.pow(1, INF)), 1.)
615 self.assertEqual((math.pow(1, NINF)), 1.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000616 self.assertTrue(math.isnan(math.pow(NAN, 1)))
617 self.assertTrue(math.isnan(math.pow(2, NAN)))
618 self.assertTrue(math.isnan(math.pow(0, NAN)))
Christian Heimes6f341092008-04-18 23:13:07 +0000619 self.assertEqual(math.pow(1, NAN), 1)
Mark Dickinsone941d972008-04-19 18:51:48 +0000620
621 # pow(0., x)
622 self.assertEqual(math.pow(0., INF), 0.)
623 self.assertEqual(math.pow(0., 3.), 0.)
624 self.assertEqual(math.pow(0., 2.3), 0.)
625 self.assertEqual(math.pow(0., 2.), 0.)
626 self.assertEqual(math.pow(0., 0.), 1.)
627 self.assertEqual(math.pow(0., -0.), 1.)
628 self.assertRaises(ValueError, math.pow, 0., -2.)
629 self.assertRaises(ValueError, math.pow, 0., -2.3)
630 self.assertRaises(ValueError, math.pow, 0., -3.)
631 self.assertRaises(ValueError, math.pow, 0., NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000632 self.assertTrue(math.isnan(math.pow(0., NAN)))
Mark Dickinsone941d972008-04-19 18:51:48 +0000633
634 # pow(INF, x)
635 self.assertEqual(math.pow(INF, INF), INF)
636 self.assertEqual(math.pow(INF, 3.), INF)
637 self.assertEqual(math.pow(INF, 2.3), INF)
638 self.assertEqual(math.pow(INF, 2.), INF)
639 self.assertEqual(math.pow(INF, 0.), 1.)
640 self.assertEqual(math.pow(INF, -0.), 1.)
641 self.assertEqual(math.pow(INF, -2.), 0.)
642 self.assertEqual(math.pow(INF, -2.3), 0.)
643 self.assertEqual(math.pow(INF, -3.), 0.)
644 self.assertEqual(math.pow(INF, NINF), 0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000645 self.assertTrue(math.isnan(math.pow(INF, NAN)))
Mark Dickinsone941d972008-04-19 18:51:48 +0000646
647 # pow(-0., x)
648 self.assertEqual(math.pow(-0., INF), 0.)
649 self.assertEqual(math.pow(-0., 3.), -0.)
650 self.assertEqual(math.pow(-0., 2.3), 0.)
651 self.assertEqual(math.pow(-0., 2.), 0.)
652 self.assertEqual(math.pow(-0., 0.), 1.)
653 self.assertEqual(math.pow(-0., -0.), 1.)
654 self.assertRaises(ValueError, math.pow, -0., -2.)
655 self.assertRaises(ValueError, math.pow, -0., -2.3)
656 self.assertRaises(ValueError, math.pow, -0., -3.)
657 self.assertRaises(ValueError, math.pow, -0., NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000658 self.assertTrue(math.isnan(math.pow(-0., NAN)))
Mark Dickinsone941d972008-04-19 18:51:48 +0000659
660 # pow(NINF, x)
661 self.assertEqual(math.pow(NINF, INF), INF)
662 self.assertEqual(math.pow(NINF, 3.), NINF)
663 self.assertEqual(math.pow(NINF, 2.3), INF)
664 self.assertEqual(math.pow(NINF, 2.), INF)
665 self.assertEqual(math.pow(NINF, 0.), 1.)
666 self.assertEqual(math.pow(NINF, -0.), 1.)
667 self.assertEqual(math.pow(NINF, -2.), 0.)
668 self.assertEqual(math.pow(NINF, -2.3), 0.)
669 self.assertEqual(math.pow(NINF, -3.), -0.)
670 self.assertEqual(math.pow(NINF, NINF), 0.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000671 self.assertTrue(math.isnan(math.pow(NINF, NAN)))
Mark Dickinsone941d972008-04-19 18:51:48 +0000672
673 # pow(-1, x)
674 self.assertEqual(math.pow(-1., INF), 1.)
675 self.assertEqual(math.pow(-1., 3.), -1.)
676 self.assertRaises(ValueError, math.pow, -1., 2.3)
677 self.assertEqual(math.pow(-1., 2.), 1.)
678 self.assertEqual(math.pow(-1., 0.), 1.)
679 self.assertEqual(math.pow(-1., -0.), 1.)
680 self.assertEqual(math.pow(-1., -2.), 1.)
681 self.assertRaises(ValueError, math.pow, -1., -2.3)
682 self.assertEqual(math.pow(-1., -3.), -1.)
683 self.assertEqual(math.pow(-1., NINF), 1.)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000684 self.assertTrue(math.isnan(math.pow(-1., NAN)))
Mark Dickinsone941d972008-04-19 18:51:48 +0000685
686 # pow(1, x)
687 self.assertEqual(math.pow(1., INF), 1.)
688 self.assertEqual(math.pow(1., 3.), 1.)
689 self.assertEqual(math.pow(1., 2.3), 1.)
690 self.assertEqual(math.pow(1., 2.), 1.)
691 self.assertEqual(math.pow(1., 0.), 1.)
692 self.assertEqual(math.pow(1., -0.), 1.)
693 self.assertEqual(math.pow(1., -2.), 1.)
694 self.assertEqual(math.pow(1., -2.3), 1.)
695 self.assertEqual(math.pow(1., -3.), 1.)
696 self.assertEqual(math.pow(1., NINF), 1.)
697 self.assertEqual(math.pow(1., NAN), 1.)
698
699 # pow(x, 0) should be 1 for any x
700 self.assertEqual(math.pow(2.3, 0.), 1.)
701 self.assertEqual(math.pow(-2.3, 0.), 1.)
702 self.assertEqual(math.pow(NAN, 0.), 1.)
703 self.assertEqual(math.pow(2.3, -0.), 1.)
704 self.assertEqual(math.pow(-2.3, -0.), 1.)
705 self.assertEqual(math.pow(NAN, -0.), 1.)
706
707 # pow(x, y) is invalid if x is negative and y is not integral
708 self.assertRaises(ValueError, math.pow, -1., 2.3)
709 self.assertRaises(ValueError, math.pow, -15., -3.1)
710
711 # pow(x, NINF)
712 self.assertEqual(math.pow(1.9, NINF), 0.)
713 self.assertEqual(math.pow(1.1, NINF), 0.)
714 self.assertEqual(math.pow(0.9, NINF), INF)
715 self.assertEqual(math.pow(0.1, NINF), INF)
716 self.assertEqual(math.pow(-0.1, NINF), INF)
717 self.assertEqual(math.pow(-0.9, NINF), INF)
718 self.assertEqual(math.pow(-1.1, NINF), 0.)
719 self.assertEqual(math.pow(-1.9, NINF), 0.)
720
721 # pow(x, INF)
722 self.assertEqual(math.pow(1.9, INF), INF)
723 self.assertEqual(math.pow(1.1, INF), INF)
724 self.assertEqual(math.pow(0.9, INF), 0.)
725 self.assertEqual(math.pow(0.1, INF), 0.)
726 self.assertEqual(math.pow(-0.1, INF), 0.)
727 self.assertEqual(math.pow(-0.9, INF), 0.)
728 self.assertEqual(math.pow(-1.1, INF), INF)
729 self.assertEqual(math.pow(-1.9, INF), INF)
730
Mark Dickinsoncec3f132008-04-20 04:13:13 +0000731 # pow(x, y) should work for x negative, y an integer
732 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
733 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
734 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
735 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
736 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
737 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
738 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
739 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
740 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
741 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
742
Mark Dickinsone941d972008-04-19 18:51:48 +0000743 # the following tests have been commented out since they don't
744 # really belong here: the implementation of ** for floats is
745 # independent of the implemention of math.pow
746 #self.assertEqual(1**NAN, 1)
747 #self.assertEqual(1**INF, 1)
748 #self.assertEqual(1**NINF, 1)
749 #self.assertEqual(1**0, 1)
750 #self.assertEqual(1.**NAN, 1)
751 #self.assertEqual(1.**INF, 1)
752 #self.assertEqual(1.**NINF, 1)
753 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000754
Georg Brandl2f037602006-10-28 13:51:49 +0000755 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000756 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000757 self.ftest('radians(180)', math.radians(180), math.pi)
758 self.ftest('radians(90)', math.radians(90), math.pi/2)
759 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000760
Georg Brandl2f037602006-10-28 13:51:49 +0000761 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000762 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000763 self.ftest('sin(0)', math.sin(0), 0)
764 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
765 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000766 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000767 self.assertTrue(math.isnan(math.sin(INF)))
768 self.assertTrue(math.isnan(math.sin(NINF)))
Christian Heimes6f341092008-04-18 23:13:07 +0000769 except ValueError:
770 self.assertRaises(ValueError, math.sin, INF)
771 self.assertRaises(ValueError, math.sin, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000772 self.assertTrue(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000773
Georg Brandl2f037602006-10-28 13:51:49 +0000774 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000775 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000776 self.ftest('sinh(0)', math.sinh(0), 0)
777 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
778 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000779 self.assertEquals(math.sinh(INF), INF)
Mark Dickinsone941d972008-04-19 18:51:48 +0000780 self.assertEquals(math.sinh(NINF), NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000781 self.assertTrue(math.isnan(math.sinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000782
Georg Brandl2f037602006-10-28 13:51:49 +0000783 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000784 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000785 self.ftest('sqrt(0)', math.sqrt(0), 0)
786 self.ftest('sqrt(1)', math.sqrt(1), 1)
787 self.ftest('sqrt(4)', math.sqrt(4), 2)
Christian Heimes6f341092008-04-18 23:13:07 +0000788 self.assertEquals(math.sqrt(INF), INF)
789 self.assertRaises(ValueError, math.sqrt, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000790 self.assertTrue(math.isnan(math.sqrt(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000791
Georg Brandl2f037602006-10-28 13:51:49 +0000792 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000793 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000794 self.ftest('tan(0)', math.tan(0), 0)
795 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
796 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes6f341092008-04-18 23:13:07 +0000797 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000798 self.assertTrue(math.isnan(math.tan(INF)))
799 self.assertTrue(math.isnan(math.tan(NINF)))
Christian Heimes6f341092008-04-18 23:13:07 +0000800 except:
801 self.assertRaises(ValueError, math.tan, INF)
802 self.assertRaises(ValueError, math.tan, NINF)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000803 self.assertTrue(math.isnan(math.tan(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000804
Georg Brandl2f037602006-10-28 13:51:49 +0000805 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000806 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000807 self.ftest('tanh(0)', math.tanh(0), 0)
808 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000809 self.ftest('tanh(inf)', math.tanh(INF), 1)
810 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000811 self.assertTrue(math.isnan(math.tanh(NAN)))
Mark Dickinsond6d51482008-04-20 20:38:48 +0000812 # check that tanh(-0.) == -0. on IEEE 754 systems
813 if float.__getformat__("double").startswith("IEEE"):
814 self.assertEqual(math.tanh(-0.), -0.)
815 self.assertEqual(math.copysign(1., math.tanh(-0.)),
816 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000817
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000818 def test_trunc(self):
819 self.assertEqual(math.trunc(1), 1)
820 self.assertEqual(math.trunc(-1), -1)
821 self.assertEqual(type(math.trunc(1)), int)
822 self.assertEqual(type(math.trunc(1.5)), int)
823 self.assertEqual(math.trunc(1.5), 1)
824 self.assertEqual(math.trunc(-1.5), -1)
825 self.assertEqual(math.trunc(1.999999), 1)
826 self.assertEqual(math.trunc(-1.999999), -1)
827 self.assertEqual(math.trunc(-0.999999), -0)
828 self.assertEqual(math.trunc(-100.999), -100)
829
830 class TestTrunc(object):
831 def __trunc__(self):
832 return 23
833
834 class TestNoTrunc(object):
835 pass
836
837 self.assertEqual(math.trunc(TestTrunc()), 23)
838
839 self.assertRaises(TypeError, math.trunc)
840 self.assertRaises(TypeError, math.trunc, 1, 2)
841 # XXX: This is not ideal, but see the comment in math_trunc().
842 self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
843
844 t = TestNoTrunc()
845 t.__trunc__ = lambda *args: args
846 self.assertEquals((), math.trunc(t))
847 self.assertRaises(TypeError, math.trunc, t, 0)
848
Christian Heimeseebb79c2008-01-03 22:32:26 +0000849 def testCopysign(self):
850 self.assertEqual(math.copysign(1, 42), 1.0)
851 self.assertEqual(math.copysign(0., 42), 0.0)
852 self.assertEqual(math.copysign(1., -42), -1.0)
853 self.assertEqual(math.copysign(3, 0.), 3.0)
854 self.assertEqual(math.copysign(4., -0.), -4.0)
855
Christian Heimese2ca4242008-01-03 20:23:15 +0000856 def testIsnan(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000857 self.assertTrue(math.isnan(float("nan")))
858 self.assertTrue(math.isnan(float("inf")* 0.))
859 self.assertFalse(math.isnan(float("inf")))
860 self.assertFalse(math.isnan(0.))
861 self.assertFalse(math.isnan(1.))
Christian Heimese2ca4242008-01-03 20:23:15 +0000862
863 def testIsinf(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000864 self.assertTrue(math.isinf(float("inf")))
865 self.assertTrue(math.isinf(float("-inf")))
866 self.assertTrue(math.isinf(1E400))
867 self.assertTrue(math.isinf(-1E400))
868 self.assertFalse(math.isinf(float("nan")))
869 self.assertFalse(math.isinf(0.))
870 self.assertFalse(math.isinf(1.))
Christian Heimese2ca4242008-01-03 20:23:15 +0000871
Georg Brandl2f037602006-10-28 13:51:49 +0000872 # RED_FLAG 16-Oct-2000 Tim
873 # While 2.0 is more consistent about exceptions than previous releases, it
874 # still fails this part of the test on some platforms. For now, we only
875 # *run* test_exceptions() in verbose mode, so that this isn't normally
876 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000877
Georg Brandl2f037602006-10-28 13:51:49 +0000878 if verbose:
879 def test_exceptions(self):
880 try:
881 x = math.exp(-1000000000)
882 except:
883 # mathmodule.c is failing to weed out underflows from libm, or
884 # we've got an fp format with huge dynamic range
885 self.fail("underflowing exp() should not have raised "
886 "an exception")
887 if x != 0:
888 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000889
Georg Brandl2f037602006-10-28 13:51:49 +0000890 # If this fails, probably using a strict IEEE-754 conforming libm, and x
891 # is +Inf afterwards. But Python wants overflows detected by default.
892 try:
893 x = math.exp(1000000000)
894 except OverflowError:
895 pass
896 else:
897 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000898
Georg Brandl2f037602006-10-28 13:51:49 +0000899 # If this fails, it could be a puzzle. One odd possibility is that
900 # mathmodule.c's macros are getting confused while comparing
901 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
902 # as a result (and so raising OverflowError instead).
903 try:
904 x = math.sqrt(-1.0)
905 except ValueError:
906 pass
907 else:
908 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000909
Mark Dickinson2985dbb2009-09-18 21:01:50 +0000910 @requires_IEEE_754
Christian Heimes6f341092008-04-18 23:13:07 +0000911 def test_testfile(self):
Christian Heimes6f341092008-04-18 23:13:07 +0000912 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
913 # Skip if either the input or result is complex, or if
914 # flags is nonempty
915 if ai != 0. or ei != 0. or flags:
916 continue
917 if fn in ['rect', 'polar']:
918 # no real versions of rect, polar
919 continue
920 func = getattr(math, fn)
Mark Dickinson9f99d702008-04-20 01:22:30 +0000921 try:
922 result = func(ar)
923 except ValueError:
924 message = ("Unexpected ValueError in " +
925 "test %s:%s(%r)\n" % (id, fn, ar))
926 self.fail(message)
Mark Dickinsond0558352008-05-23 03:30:01 +0000927 except OverflowError:
928 message = ("Unexpected OverflowError in " +
929 "test %s:%s(%r)\n" % (id, fn, ar))
930 self.fail(message)
Christian Heimes6f341092008-04-18 23:13:07 +0000931 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Georg Brandl2f037602006-10-28 13:51:49 +0000932
Mark Dickinsonb93fff02009-09-28 18:54:55 +0000933 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
934 "test requires IEEE 754 doubles")
935 def test_mtestfile(self):
936 ALLOWED_ERROR = 20 # permitted error, in ulps
937 fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}"
938
939 failures = []
940 for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
941 func = getattr(math, fn)
942
943 if 'invalid' in flags or 'divide-by-zero' in flags:
944 expected = 'ValueError'
945 elif 'overflow' in flags:
946 expected = 'OverflowError'
947
948 try:
949 got = func(arg)
950 except ValueError:
951 got = 'ValueError'
952 except OverflowError:
953 got = 'OverflowError'
954
955 diff_ulps = None
956 if isinstance(got, float) and isinstance(expected, float):
957 if math.isnan(expected) and math.isnan(got):
958 continue
959 if not math.isnan(expected) and not math.isnan(got):
960 diff_ulps = to_ulps(expected) - to_ulps(got)
961 if diff_ulps <= ALLOWED_ERROR:
962 continue
963
964 if isinstance(got, str) and isinstance(expected, str):
965 if got == expected:
966 continue
967
968 fail_msg = fail_fmt.format(id, fn, arg, expected, got)
969 if diff_ulps is not None:
970 fail_msg += ' ({} ulps)'.format(diff_ulps)
971 failures.append(fail_msg)
972
973 if failures:
974 self.fail('Failures in test_mtestfile:\n ' +
975 '\n '.join(failures))
976
977
Georg Brandl2f037602006-10-28 13:51:49 +0000978def test_main():
Christian Heimes6f341092008-04-18 23:13:07 +0000979 from doctest import DocFileSuite
980 suite = unittest.TestSuite()
981 suite.addTest(unittest.makeSuite(MathTests))
982 suite.addTest(DocFileSuite("ieee754.txt"))
983 run_unittest(suite)
Georg Brandl2f037602006-10-28 13:51:49 +0000984
985if __name__ == '__main__':
986 test_main()