blob: e53bf2e3a90f8d4bacda034e176351be20b1351f [file] [log] [blame]
Guido van Rossumfcce6301996-08-08 18:26:25 +00001# Python test set -- math module
2# XXXX Should not do tests around zero only
3
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test.support import run_unittest, verbose
Thomas Wouters89f507f2006-12-13 04:49:30 +00005import unittest
6import math
Christian Heimes53876d92008-04-19 00:31:39 +00007import os
8import sys
Georg Brandlc28e1fa2008-06-10 19:20:26 +00009import random
Guido van Rossumfcce6301996-08-08 18:26:25 +000010
Christian Heimes53876d92008-04-19 00:31:39 +000011eps = 1E-05
12NAN = float('nan')
13INF = float('inf')
14NINF = float('-inf')
15
Mark Dickinson5c567082009-04-24 16:39:07 +000016# detect evidence of double-rounding: fsum is not always correctly
17# rounded on machines that suffer from double rounding.
18x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
19HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
20
Christian Heimes53876d92008-04-19 00:31:39 +000021# locate file with test values
22if __name__ == '__main__':
23 file = sys.argv[0]
24else:
25 file = __file__
26test_dir = os.path.dirname(file) or os.curdir
27test_file = os.path.join(test_dir, 'cmath_testcases.txt')
28
29def parse_testfile(fname):
30 """Parse a file with test values
31
32 Empty lines or lines starting with -- are ignored
33 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
34 """
35 with open(fname) as fp:
36 for line in fp:
37 # skip comment lines and blank lines
38 if line.startswith('--') or not line.strip():
39 continue
40
41 lhs, rhs = line.split('->')
42 id, fn, arg_real, arg_imag = lhs.split()
43 rhs_pieces = rhs.split()
44 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
45 flags = rhs_pieces[2:]
46
47 yield (id, fn,
48 float(arg_real), float(arg_imag),
49 float(exp_real), float(exp_imag),
50 flags
51 )
Guido van Rossumfcce6301996-08-08 18:26:25 +000052
Thomas Wouters89f507f2006-12-13 04:49:30 +000053class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000054
Thomas Wouters89f507f2006-12-13 04:49:30 +000055 def ftest(self, name, value, expected):
56 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +000057 # Use %r instead of %f so the error message
58 # displays full precision. Otherwise discrepancies
59 # in the last few bits will lead to very confusing
60 # error messages
61 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +000062 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000063
Thomas Wouters89f507f2006-12-13 04:49:30 +000064 def testConstants(self):
65 self.ftest('pi', math.pi, 3.1415926)
66 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000067
Thomas Wouters89f507f2006-12-13 04:49:30 +000068 def testAcos(self):
69 self.assertRaises(TypeError, math.acos)
70 self.ftest('acos(-1)', math.acos(-1), math.pi)
71 self.ftest('acos(0)', math.acos(0), math.pi/2)
72 self.ftest('acos(1)', math.acos(1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +000073 self.assertRaises(ValueError, math.acos, INF)
74 self.assertRaises(ValueError, math.acos, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +000075 self.assertTrue(math.isnan(math.acos(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +000076
77 def testAcosh(self):
78 self.assertRaises(TypeError, math.acosh)
79 self.ftest('acosh(1)', math.acosh(1), 0)
80 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
81 self.assertRaises(ValueError, math.acosh, 0)
82 self.assertRaises(ValueError, math.acosh, -1)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000083 self.assertEqual(math.acosh(INF), INF)
Christian Heimes53876d92008-04-19 00:31:39 +000084 self.assertRaises(ValueError, math.acosh, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +000085 self.assertTrue(math.isnan(math.acosh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000086
Thomas Wouters89f507f2006-12-13 04:49:30 +000087 def testAsin(self):
88 self.assertRaises(TypeError, math.asin)
89 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
90 self.ftest('asin(0)', math.asin(0), 0)
91 self.ftest('asin(1)', math.asin(1), math.pi/2)
Christian Heimes53876d92008-04-19 00:31:39 +000092 self.assertRaises(ValueError, math.asin, INF)
93 self.assertRaises(ValueError, math.asin, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +000094 self.assertTrue(math.isnan(math.asin(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +000095
96 def testAsinh(self):
97 self.assertRaises(TypeError, math.asinh)
98 self.ftest('asinh(0)', math.asinh(0), 0)
99 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
100 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000101 self.assertEqual(math.asinh(INF), INF)
102 self.assertEqual(math.asinh(NINF), NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000103 self.assertTrue(math.isnan(math.asinh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000104
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 def testAtan(self):
106 self.assertRaises(TypeError, math.atan)
107 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
108 self.ftest('atan(0)', math.atan(0), 0)
109 self.ftest('atan(1)', math.atan(1), math.pi/4)
Christian Heimes53876d92008-04-19 00:31:39 +0000110 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
Christian Heimesa342c012008-04-20 21:01:16 +0000111 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
Georg Brandlab91fde2009-08-13 08:51:18 +0000112 self.assertTrue(math.isnan(math.atan(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000113
114 def testAtanh(self):
115 self.assertRaises(TypeError, math.atan)
116 self.ftest('atanh(0)', math.atanh(0), 0)
117 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
118 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
119 self.assertRaises(ValueError, math.atanh, 1)
120 self.assertRaises(ValueError, math.atanh, -1)
121 self.assertRaises(ValueError, math.atanh, INF)
122 self.assertRaises(ValueError, math.atanh, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000123 self.assertTrue(math.isnan(math.atanh(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000124
Thomas Wouters89f507f2006-12-13 04:49:30 +0000125 def testAtan2(self):
126 self.assertRaises(TypeError, math.atan2)
127 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
128 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
129 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
130 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
131 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000132
Christian Heimese57950f2008-04-21 13:08:03 +0000133 # math.atan2(0, x)
134 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
135 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
136 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
137 self.assertEqual(math.atan2(0., 0.), 0.)
138 self.assertEqual(math.atan2(0., 2.3), 0.)
139 self.assertEqual(math.atan2(0., INF), 0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000140 self.assertTrue(math.isnan(math.atan2(0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000141 # math.atan2(-0, x)
142 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
143 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
144 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
145 self.assertEqual(math.atan2(-0., 0.), -0.)
146 self.assertEqual(math.atan2(-0., 2.3), -0.)
147 self.assertEqual(math.atan2(-0., INF), -0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000148 self.assertTrue(math.isnan(math.atan2(-0., NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000149 # math.atan2(INF, x)
150 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
151 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
152 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
153 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
154 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
155 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
Georg Brandlab91fde2009-08-13 08:51:18 +0000156 self.assertTrue(math.isnan(math.atan2(INF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000157 # math.atan2(NINF, x)
158 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
159 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
160 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
161 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
162 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
163 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
Georg Brandlab91fde2009-08-13 08:51:18 +0000164 self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000165 # math.atan2(+finite, x)
166 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
167 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
168 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
169 self.assertEqual(math.atan2(2.3, INF), 0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000170 self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000171 # math.atan2(-finite, x)
172 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
173 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
174 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
175 self.assertEqual(math.atan2(-2.3, INF), -0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000176 self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000177 # math.atan2(NAN, x)
Georg Brandlab91fde2009-08-13 08:51:18 +0000178 self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
179 self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
180 self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
181 self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
182 self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
183 self.assertTrue(math.isnan(math.atan2(NAN, INF)))
184 self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000185
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 def testCeil(self):
187 self.assertRaises(TypeError, math.ceil)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000188 self.assertEqual(int, type(math.ceil(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
190 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
191 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
192 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
193 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
194 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000195 #self.assertEqual(math.ceil(INF), INF)
196 #self.assertEqual(math.ceil(NINF), NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000197 #self.assertTrue(math.isnan(math.ceil(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000198
Guido van Rossum13e05de2007-08-23 22:56:55 +0000199 class TestCeil:
200 def __ceil__(self):
201 return 42
202 class TestNoCeil:
203 pass
204 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
205 self.assertRaises(TypeError, math.ceil, TestNoCeil())
206
207 t = TestNoCeil()
208 t.__ceil__ = lambda *args: args
209 self.assertRaises(TypeError, math.ceil, t)
210 self.assertRaises(TypeError, math.ceil, t, 0)
211
Christian Heimes53876d92008-04-19 00:31:39 +0000212 if float.__getformat__("double").startswith("IEEE"):
213 def testCopysign(self):
Mark Dickinsonf5732532010-02-06 23:18:37 +0000214 self.assertEqual(math.copysign(1, 42), 1.0)
215 self.assertEqual(math.copysign(0., 42), 0.0)
216 self.assertEqual(math.copysign(1., -42), -1.0)
217 self.assertEqual(math.copysign(3, 0.), 3.0)
218 self.assertEqual(math.copysign(4., -0.), -4.0)
219
Christian Heimes53876d92008-04-19 00:31:39 +0000220 self.assertRaises(TypeError, math.copysign)
221 # copysign should let us distinguish signs of zeros
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000222 self.assertEqual(math.copysign(1., 0.), 1.)
223 self.assertEqual(math.copysign(1., -0.), -1.)
224 self.assertEqual(math.copysign(INF, 0.), INF)
225 self.assertEqual(math.copysign(INF, -0.), NINF)
226 self.assertEqual(math.copysign(NINF, 0.), INF)
227 self.assertEqual(math.copysign(NINF, -0.), NINF)
Christian Heimes53876d92008-04-19 00:31:39 +0000228 # and of infinities
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000229 self.assertEqual(math.copysign(1., INF), 1.)
230 self.assertEqual(math.copysign(1., NINF), -1.)
231 self.assertEqual(math.copysign(INF, INF), INF)
232 self.assertEqual(math.copysign(INF, NINF), NINF)
233 self.assertEqual(math.copysign(NINF, INF), INF)
234 self.assertEqual(math.copysign(NINF, NINF), NINF)
Mark Dickinsonf5732532010-02-06 23:18:37 +0000235 self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
236 self.assertTrue(math.isnan(math.copysign(NAN, INF)))
237 self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
238 self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000239 # copysign(INF, NAN) may be INF or it may be NINF, since
240 # we don't know whether the sign bit of NAN is set on any
241 # given platform.
Mark Dickinsonf5732532010-02-06 23:18:37 +0000242 self.assertTrue(math.isinf(math.copysign(INF, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000243 # similarly, copysign(2., NAN) could be 2. or -2.
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000244 self.assertEqual(abs(math.copysign(2., NAN)), 2.)
Christian Heimes53876d92008-04-19 00:31:39 +0000245
Thomas Wouters89f507f2006-12-13 04:49:30 +0000246 def testCos(self):
247 self.assertRaises(TypeError, math.cos)
248 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
249 self.ftest('cos(0)', math.cos(0), 1)
250 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
251 self.ftest('cos(pi)', math.cos(math.pi), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000252 try:
Georg Brandlab91fde2009-08-13 08:51:18 +0000253 self.assertTrue(math.isnan(math.cos(INF)))
254 self.assertTrue(math.isnan(math.cos(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000255 except ValueError:
256 self.assertRaises(ValueError, math.cos, INF)
257 self.assertRaises(ValueError, math.cos, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000258 self.assertTrue(math.isnan(math.cos(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000259
Thomas Wouters89f507f2006-12-13 04:49:30 +0000260 def testCosh(self):
261 self.assertRaises(TypeError, math.cosh)
262 self.ftest('cosh(0)', math.cosh(0), 1)
263 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000264 self.assertEqual(math.cosh(INF), INF)
265 self.assertEqual(math.cosh(NINF), INF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000266 self.assertTrue(math.isnan(math.cosh(NAN)))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000267
Thomas Wouters89f507f2006-12-13 04:49:30 +0000268 def testDegrees(self):
269 self.assertRaises(TypeError, math.degrees)
270 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
271 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
272 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000273
Thomas Wouters89f507f2006-12-13 04:49:30 +0000274 def testExp(self):
275 self.assertRaises(TypeError, math.exp)
276 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
277 self.ftest('exp(0)', math.exp(0), 1)
278 self.ftest('exp(1)', math.exp(1), math.e)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000279 self.assertEqual(math.exp(INF), INF)
280 self.assertEqual(math.exp(NINF), 0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000281 self.assertTrue(math.isnan(math.exp(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000282
Thomas Wouters89f507f2006-12-13 04:49:30 +0000283 def testFabs(self):
284 self.assertRaises(TypeError, math.fabs)
285 self.ftest('fabs(-1)', math.fabs(-1), 1)
286 self.ftest('fabs(0)', math.fabs(0), 0)
287 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000288
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000289 def testFactorial(self):
290 def fact(n):
291 result = 1
292 for i in range(1, int(n)+1):
293 result *= i
294 return result
295 values = list(range(10)) + [50, 100, 500]
296 random.shuffle(values)
Mark Dickinson3d9fa6d2010-05-12 19:57:07 +0000297 for x in values:
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000298 for cast in (int, float):
299 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
300 self.assertRaises(ValueError, math.factorial, -1)
301 self.assertRaises(ValueError, math.factorial, math.pi)
302
Thomas Wouters89f507f2006-12-13 04:49:30 +0000303 def testFloor(self):
304 self.assertRaises(TypeError, math.floor)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000305 self.assertEqual(int, type(math.floor(0.5)))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000306 self.ftest('floor(0.5)', math.floor(0.5), 0)
307 self.ftest('floor(1.0)', math.floor(1.0), 1)
308 self.ftest('floor(1.5)', math.floor(1.5), 1)
309 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
310 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
311 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000312 # pow() relies on floor() to check for integers
313 # This fails on some platforms - so check it here
314 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
315 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000316 #self.assertEqual(math.ceil(INF), INF)
317 #self.assertEqual(math.ceil(NINF), NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000318 #self.assertTrue(math.isnan(math.floor(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000319
Guido van Rossum13e05de2007-08-23 22:56:55 +0000320 class TestFloor:
321 def __floor__(self):
322 return 42
323 class TestNoFloor:
324 pass
325 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
326 self.assertRaises(TypeError, math.floor, TestNoFloor())
327
328 t = TestNoFloor()
329 t.__floor__ = lambda *args: args
330 self.assertRaises(TypeError, math.floor, t)
331 self.assertRaises(TypeError, math.floor, t, 0)
332
Thomas Wouters89f507f2006-12-13 04:49:30 +0000333 def testFmod(self):
334 self.assertRaises(TypeError, math.fmod)
335 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
336 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
337 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
338 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
339 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
340 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000341 self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
342 self.assertTrue(math.isnan(math.fmod(1., NAN)))
343 self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000344 self.assertRaises(ValueError, math.fmod, 1., 0.)
345 self.assertRaises(ValueError, math.fmod, INF, 1.)
346 self.assertRaises(ValueError, math.fmod, NINF, 1.)
347 self.assertRaises(ValueError, math.fmod, INF, 0.)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000348 self.assertEqual(math.fmod(3.0, INF), 3.0)
349 self.assertEqual(math.fmod(-3.0, INF), -3.0)
350 self.assertEqual(math.fmod(3.0, NINF), 3.0)
351 self.assertEqual(math.fmod(-3.0, NINF), -3.0)
352 self.assertEqual(math.fmod(0.0, 3.0), 0.0)
353 self.assertEqual(math.fmod(0.0, NINF), 0.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000354
Thomas Wouters89f507f2006-12-13 04:49:30 +0000355 def testFrexp(self):
356 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000357
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000358 def testfrexp(name, result, expected):
359 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000360 if abs(mant-emant) > eps or exp != eexp:
361 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000362 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000363
Thomas Wouters89f507f2006-12-13 04:49:30 +0000364 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
365 testfrexp('frexp(0)', math.frexp(0), (0, 0))
366 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
367 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000368
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000369 self.assertEqual(math.frexp(INF)[0], INF)
370 self.assertEqual(math.frexp(NINF)[0], NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000371 self.assertTrue(math.isnan(math.frexp(NAN)[0]))
Christian Heimes53876d92008-04-19 00:31:39 +0000372
Mark Dickinson5c567082009-04-24 16:39:07 +0000373 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
374 "test requires IEEE 754 doubles")
375 @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
376 "fsum is not exact on machines with double rounding")
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000377 def testFsum(self):
378 # math.fsum relies on exact rounding for correct operation.
379 # There's a known problem with IA32 floating-point that causes
380 # inexact rounding in some situations, and will cause the
381 # math.fsum tests below to fail; see issue #2937. On non IEEE
382 # 754 platforms, and on IEEE 754 platforms that exhibit the
383 # problem described in issue #2937, we simply skip the whole
384 # test.
385
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000386 # Python version of math.fsum, for comparison. Uses a
387 # different algorithm based on frexp, ldexp and integer
388 # arithmetic.
389 from sys import float_info
390 mant_dig = float_info.mant_dig
391 etiny = float_info.min_exp - mant_dig
392
393 def msum(iterable):
394 """Full precision summation. Compute sum(iterable) without any
395 intermediate accumulation of error. Based on the 'lsum' function
396 at http://code.activestate.com/recipes/393090/
397
398 """
399 tmant, texp = 0, 0
400 for x in iterable:
401 mant, exp = math.frexp(x)
402 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
403 if texp > exp:
404 tmant <<= texp-exp
405 texp = exp
406 else:
407 mant <<= exp-texp
408 tmant += mant
409 # Round tmant * 2**texp to a float. The original recipe
410 # used float(str(tmant)) * 2.0**texp for this, but that's
411 # a little unsafe because str -> float conversion can't be
412 # relied upon to do correct rounding on all platforms.
413 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
414 if tail > 0:
415 h = 1 << (tail-1)
416 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
417 texp += tail
418 return math.ldexp(tmant, texp)
419
420 test_values = [
421 ([], 0.0),
422 ([0.0], 0.0),
423 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
424 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
425 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
426 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
427 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
428 ([1./n for n in range(1, 1001)],
429 float.fromhex('0x1.df11f45f4e61ap+2')),
430 ([(-1.)**n/n for n in range(1, 1001)],
431 float.fromhex('-0x1.62a2af1bd3624p-1')),
432 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
433 ([1e16, 1., 1e-16], 10000000000000002.0),
434 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
435 # exercise code for resizing partials array
436 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
437 [-2.**1022],
438 float.fromhex('0x1.5555555555555p+970')),
439 ]
440
441 for i, (vals, expected) in enumerate(test_values):
442 try:
443 actual = math.fsum(vals)
444 except OverflowError:
445 self.fail("test %d failed: got OverflowError, expected %r "
446 "for math.fsum(%.100r)" % (i, expected, vals))
447 except ValueError:
448 self.fail("test %d failed: got ValueError, expected %r "
449 "for math.fsum(%.100r)" % (i, expected, vals))
450 self.assertEqual(actual, expected)
451
452 from random import random, gauss, shuffle
453 for j in range(1000):
454 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
455 s = 0
456 for i in range(200):
457 v = gauss(0, random()) ** 7 - s
458 s += v
459 vals.append(v)
460 shuffle(vals)
461
462 s = msum(vals)
463 self.assertEqual(msum(vals), math.fsum(vals))
464
Thomas Wouters89f507f2006-12-13 04:49:30 +0000465 def testHypot(self):
466 self.assertRaises(TypeError, math.hypot)
467 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
468 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Christian Heimes53876d92008-04-19 00:31:39 +0000469 self.assertEqual(math.hypot(NAN, INF), INF)
470 self.assertEqual(math.hypot(INF, NAN), INF)
471 self.assertEqual(math.hypot(NAN, NINF), INF)
472 self.assertEqual(math.hypot(NINF, NAN), INF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000473 self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
474 self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000475
Thomas Wouters89f507f2006-12-13 04:49:30 +0000476 def testLdexp(self):
477 self.assertRaises(TypeError, math.ldexp)
478 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
479 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
480 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
481 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Christian Heimes53876d92008-04-19 00:31:39 +0000482 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
483 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000484 self.assertEqual(math.ldexp(1., -1000000), 0.)
485 self.assertEqual(math.ldexp(-1., -1000000), -0.)
486 self.assertEqual(math.ldexp(INF, 30), INF)
487 self.assertEqual(math.ldexp(NINF, -213), NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000488 self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000489
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000490 # large second argument
491 for n in [10**5, 10**10, 10**20, 10**40]:
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000492 self.assertEqual(math.ldexp(INF, -n), INF)
493 self.assertEqual(math.ldexp(NINF, -n), NINF)
494 self.assertEqual(math.ldexp(1., -n), 0.)
495 self.assertEqual(math.ldexp(-1., -n), -0.)
496 self.assertEqual(math.ldexp(0., -n), 0.)
497 self.assertEqual(math.ldexp(-0., -n), -0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000498 self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000499
500 self.assertRaises(OverflowError, math.ldexp, 1., n)
501 self.assertRaises(OverflowError, math.ldexp, -1., n)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000502 self.assertEqual(math.ldexp(0., n), 0.)
503 self.assertEqual(math.ldexp(-0., n), -0.)
504 self.assertEqual(math.ldexp(INF, n), INF)
505 self.assertEqual(math.ldexp(NINF, n), NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000506 self.assertTrue(math.isnan(math.ldexp(NAN, n)))
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000507
Thomas Wouters89f507f2006-12-13 04:49:30 +0000508 def testLog(self):
509 self.assertRaises(TypeError, math.log)
510 self.ftest('log(1/e)', math.log(1/math.e), -1)
511 self.ftest('log(1)', math.log(1), 0)
512 self.ftest('log(e)', math.log(math.e), 1)
513 self.ftest('log(32,2)', math.log(32,2), 5)
514 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
515 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000516 self.assertEqual(math.log(INF), INF)
Christian Heimes53876d92008-04-19 00:31:39 +0000517 self.assertRaises(ValueError, math.log, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000518 self.assertTrue(math.isnan(math.log(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000519
520 def testLog1p(self):
521 self.assertRaises(TypeError, math.log1p)
522 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
523 self.ftest('log1p(0)', math.log1p(0), 0)
524 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
525 self.ftest('log1p(1)', math.log1p(1), math.log(2))
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000526 self.assertEqual(math.log1p(INF), INF)
Christian Heimes53876d92008-04-19 00:31:39 +0000527 self.assertRaises(ValueError, math.log1p, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000528 self.assertTrue(math.isnan(math.log1p(NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000529 n= 2**90
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000530 self.assertAlmostEqual(math.log1p(n), 62.383246250395075)
531 self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000532
Thomas Wouters89f507f2006-12-13 04:49:30 +0000533 def testLog10(self):
534 self.assertRaises(TypeError, math.log10)
535 self.ftest('log10(0.1)', math.log10(0.1), -1)
536 self.ftest('log10(1)', math.log10(1), 0)
537 self.ftest('log10(10)', math.log10(10), 1)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000538 self.assertEqual(math.log(INF), INF)
Christian Heimes53876d92008-04-19 00:31:39 +0000539 self.assertRaises(ValueError, math.log10, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000540 self.assertTrue(math.isnan(math.log10(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000541
Thomas Wouters89f507f2006-12-13 04:49:30 +0000542 def testModf(self):
543 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000544
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000545 def testmodf(name, result, expected):
546 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000547 if abs(v1-e1) > eps or abs(v2-e2):
548 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000549 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000550
Thomas Wouters89f507f2006-12-13 04:49:30 +0000551 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
552 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000553
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000554 self.assertEqual(math.modf(INF), (0.0, INF))
555 self.assertEqual(math.modf(NINF), (-0.0, NINF))
Christian Heimes53876d92008-04-19 00:31:39 +0000556
557 modf_nan = math.modf(NAN)
Georg Brandlab91fde2009-08-13 08:51:18 +0000558 self.assertTrue(math.isnan(modf_nan[0]))
559 self.assertTrue(math.isnan(modf_nan[1]))
Christian Heimes53876d92008-04-19 00:31:39 +0000560
Thomas Wouters89f507f2006-12-13 04:49:30 +0000561 def testPow(self):
562 self.assertRaises(TypeError, math.pow)
563 self.ftest('pow(0,1)', math.pow(0,1), 0)
564 self.ftest('pow(1,0)', math.pow(1,0), 1)
565 self.ftest('pow(2,1)', math.pow(2,1), 2)
566 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Christian Heimes53876d92008-04-19 00:31:39 +0000567 self.assertEqual(math.pow(INF, 1), INF)
568 self.assertEqual(math.pow(NINF, 1), NINF)
569 self.assertEqual((math.pow(1, INF)), 1.)
570 self.assertEqual((math.pow(1, NINF)), 1.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000571 self.assertTrue(math.isnan(math.pow(NAN, 1)))
572 self.assertTrue(math.isnan(math.pow(2, NAN)))
573 self.assertTrue(math.isnan(math.pow(0, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000574 self.assertEqual(math.pow(1, NAN), 1)
Christian Heimesa342c012008-04-20 21:01:16 +0000575
576 # pow(0., x)
577 self.assertEqual(math.pow(0., INF), 0.)
578 self.assertEqual(math.pow(0., 3.), 0.)
579 self.assertEqual(math.pow(0., 2.3), 0.)
580 self.assertEqual(math.pow(0., 2.), 0.)
581 self.assertEqual(math.pow(0., 0.), 1.)
582 self.assertEqual(math.pow(0., -0.), 1.)
583 self.assertRaises(ValueError, math.pow, 0., -2.)
584 self.assertRaises(ValueError, math.pow, 0., -2.3)
585 self.assertRaises(ValueError, math.pow, 0., -3.)
586 self.assertRaises(ValueError, math.pow, 0., NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000587 self.assertTrue(math.isnan(math.pow(0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000588
589 # pow(INF, x)
590 self.assertEqual(math.pow(INF, INF), INF)
591 self.assertEqual(math.pow(INF, 3.), INF)
592 self.assertEqual(math.pow(INF, 2.3), INF)
593 self.assertEqual(math.pow(INF, 2.), INF)
594 self.assertEqual(math.pow(INF, 0.), 1.)
595 self.assertEqual(math.pow(INF, -0.), 1.)
596 self.assertEqual(math.pow(INF, -2.), 0.)
597 self.assertEqual(math.pow(INF, -2.3), 0.)
598 self.assertEqual(math.pow(INF, -3.), 0.)
599 self.assertEqual(math.pow(INF, NINF), 0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000600 self.assertTrue(math.isnan(math.pow(INF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000601
602 # pow(-0., x)
603 self.assertEqual(math.pow(-0., INF), 0.)
604 self.assertEqual(math.pow(-0., 3.), -0.)
605 self.assertEqual(math.pow(-0., 2.3), 0.)
606 self.assertEqual(math.pow(-0., 2.), 0.)
607 self.assertEqual(math.pow(-0., 0.), 1.)
608 self.assertEqual(math.pow(-0., -0.), 1.)
609 self.assertRaises(ValueError, math.pow, -0., -2.)
610 self.assertRaises(ValueError, math.pow, -0., -2.3)
611 self.assertRaises(ValueError, math.pow, -0., -3.)
612 self.assertRaises(ValueError, math.pow, -0., NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000613 self.assertTrue(math.isnan(math.pow(-0., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000614
615 # pow(NINF, x)
616 self.assertEqual(math.pow(NINF, INF), INF)
617 self.assertEqual(math.pow(NINF, 3.), NINF)
618 self.assertEqual(math.pow(NINF, 2.3), INF)
619 self.assertEqual(math.pow(NINF, 2.), INF)
620 self.assertEqual(math.pow(NINF, 0.), 1.)
621 self.assertEqual(math.pow(NINF, -0.), 1.)
622 self.assertEqual(math.pow(NINF, -2.), 0.)
623 self.assertEqual(math.pow(NINF, -2.3), 0.)
624 self.assertEqual(math.pow(NINF, -3.), -0.)
625 self.assertEqual(math.pow(NINF, NINF), 0.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000626 self.assertTrue(math.isnan(math.pow(NINF, NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000627
628 # pow(-1, x)
629 self.assertEqual(math.pow(-1., INF), 1.)
630 self.assertEqual(math.pow(-1., 3.), -1.)
631 self.assertRaises(ValueError, math.pow, -1., 2.3)
632 self.assertEqual(math.pow(-1., 2.), 1.)
633 self.assertEqual(math.pow(-1., 0.), 1.)
634 self.assertEqual(math.pow(-1., -0.), 1.)
635 self.assertEqual(math.pow(-1., -2.), 1.)
636 self.assertRaises(ValueError, math.pow, -1., -2.3)
637 self.assertEqual(math.pow(-1., -3.), -1.)
638 self.assertEqual(math.pow(-1., NINF), 1.)
Georg Brandlab91fde2009-08-13 08:51:18 +0000639 self.assertTrue(math.isnan(math.pow(-1., NAN)))
Christian Heimesa342c012008-04-20 21:01:16 +0000640
641 # pow(1, x)
642 self.assertEqual(math.pow(1., INF), 1.)
643 self.assertEqual(math.pow(1., 3.), 1.)
644 self.assertEqual(math.pow(1., 2.3), 1.)
645 self.assertEqual(math.pow(1., 2.), 1.)
646 self.assertEqual(math.pow(1., 0.), 1.)
647 self.assertEqual(math.pow(1., -0.), 1.)
648 self.assertEqual(math.pow(1., -2.), 1.)
649 self.assertEqual(math.pow(1., -2.3), 1.)
650 self.assertEqual(math.pow(1., -3.), 1.)
651 self.assertEqual(math.pow(1., NINF), 1.)
652 self.assertEqual(math.pow(1., NAN), 1.)
653
654 # pow(x, 0) should be 1 for any x
655 self.assertEqual(math.pow(2.3, 0.), 1.)
656 self.assertEqual(math.pow(-2.3, 0.), 1.)
657 self.assertEqual(math.pow(NAN, 0.), 1.)
658 self.assertEqual(math.pow(2.3, -0.), 1.)
659 self.assertEqual(math.pow(-2.3, -0.), 1.)
660 self.assertEqual(math.pow(NAN, -0.), 1.)
661
662 # pow(x, y) is invalid if x is negative and y is not integral
663 self.assertRaises(ValueError, math.pow, -1., 2.3)
664 self.assertRaises(ValueError, math.pow, -15., -3.1)
665
666 # pow(x, NINF)
667 self.assertEqual(math.pow(1.9, NINF), 0.)
668 self.assertEqual(math.pow(1.1, NINF), 0.)
669 self.assertEqual(math.pow(0.9, NINF), INF)
670 self.assertEqual(math.pow(0.1, NINF), INF)
671 self.assertEqual(math.pow(-0.1, NINF), INF)
672 self.assertEqual(math.pow(-0.9, NINF), INF)
673 self.assertEqual(math.pow(-1.1, NINF), 0.)
674 self.assertEqual(math.pow(-1.9, NINF), 0.)
675
676 # pow(x, INF)
677 self.assertEqual(math.pow(1.9, INF), INF)
678 self.assertEqual(math.pow(1.1, INF), INF)
679 self.assertEqual(math.pow(0.9, INF), 0.)
680 self.assertEqual(math.pow(0.1, INF), 0.)
681 self.assertEqual(math.pow(-0.1, INF), 0.)
682 self.assertEqual(math.pow(-0.9, INF), 0.)
683 self.assertEqual(math.pow(-1.1, INF), INF)
684 self.assertEqual(math.pow(-1.9, INF), INF)
685
686 # pow(x, y) should work for x negative, y an integer
687 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
688 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
689 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
690 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
691 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
692 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
693 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
694 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
695 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
696 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
697
698 # the following tests have been commented out since they don't
699 # really belong here: the implementation of ** for floats is
700 # independent of the implemention of math.pow
701 #self.assertEqual(1**NAN, 1)
702 #self.assertEqual(1**INF, 1)
703 #self.assertEqual(1**NINF, 1)
704 #self.assertEqual(1**0, 1)
705 #self.assertEqual(1.**NAN, 1)
706 #self.assertEqual(1.**INF, 1)
707 #self.assertEqual(1.**NINF, 1)
708 #self.assertEqual(1.**0, 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000709
Thomas Wouters89f507f2006-12-13 04:49:30 +0000710 def testRadians(self):
711 self.assertRaises(TypeError, math.radians)
712 self.ftest('radians(180)', math.radians(180), math.pi)
713 self.ftest('radians(90)', math.radians(90), math.pi/2)
714 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000715
Thomas Wouters89f507f2006-12-13 04:49:30 +0000716 def testSin(self):
717 self.assertRaises(TypeError, math.sin)
718 self.ftest('sin(0)', math.sin(0), 0)
719 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
720 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000721 try:
Georg Brandlab91fde2009-08-13 08:51:18 +0000722 self.assertTrue(math.isnan(math.sin(INF)))
723 self.assertTrue(math.isnan(math.sin(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000724 except ValueError:
725 self.assertRaises(ValueError, math.sin, INF)
726 self.assertRaises(ValueError, math.sin, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000727 self.assertTrue(math.isnan(math.sin(NAN)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000728
Thomas Wouters89f507f2006-12-13 04:49:30 +0000729 def testSinh(self):
730 self.assertRaises(TypeError, math.sinh)
731 self.ftest('sinh(0)', math.sinh(0), 0)
732 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
733 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000734 self.assertEqual(math.sinh(INF), INF)
735 self.assertEqual(math.sinh(NINF), NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000736 self.assertTrue(math.isnan(math.sinh(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000737
Thomas Wouters89f507f2006-12-13 04:49:30 +0000738 def testSqrt(self):
739 self.assertRaises(TypeError, math.sqrt)
740 self.ftest('sqrt(0)', math.sqrt(0), 0)
741 self.ftest('sqrt(1)', math.sqrt(1), 1)
742 self.ftest('sqrt(4)', math.sqrt(4), 2)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000743 self.assertEqual(math.sqrt(INF), INF)
Christian Heimes53876d92008-04-19 00:31:39 +0000744 self.assertRaises(ValueError, math.sqrt, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000745 self.assertTrue(math.isnan(math.sqrt(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000746
Thomas Wouters89f507f2006-12-13 04:49:30 +0000747 def testTan(self):
748 self.assertRaises(TypeError, math.tan)
749 self.ftest('tan(0)', math.tan(0), 0)
750 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
751 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Christian Heimes53876d92008-04-19 00:31:39 +0000752 try:
Georg Brandlab91fde2009-08-13 08:51:18 +0000753 self.assertTrue(math.isnan(math.tan(INF)))
754 self.assertTrue(math.isnan(math.tan(NINF)))
Christian Heimes53876d92008-04-19 00:31:39 +0000755 except:
756 self.assertRaises(ValueError, math.tan, INF)
757 self.assertRaises(ValueError, math.tan, NINF)
Georg Brandlab91fde2009-08-13 08:51:18 +0000758 self.assertTrue(math.isnan(math.tan(NAN)))
Tim Peters1d120612000-10-12 06:10:25 +0000759
Thomas Wouters89f507f2006-12-13 04:49:30 +0000760 def testTanh(self):
761 self.assertRaises(TypeError, math.tanh)
762 self.ftest('tanh(0)', math.tanh(0), 0)
763 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000764 self.ftest('tanh(inf)', math.tanh(INF), 1)
765 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000766 self.assertTrue(math.isnan(math.tanh(NAN)))
Christian Heimese57950f2008-04-21 13:08:03 +0000767 # check that tanh(-0.) == -0. on IEEE 754 systems
768 if float.__getformat__("double").startswith("IEEE"):
769 self.assertEqual(math.tanh(-0.), -0.)
770 self.assertEqual(math.copysign(1., math.tanh(-0.)),
771 math.copysign(1., -0.))
Tim Peters1d120612000-10-12 06:10:25 +0000772
Christian Heimes400adb02008-02-01 08:12:03 +0000773 def test_trunc(self):
774 self.assertEqual(math.trunc(1), 1)
775 self.assertEqual(math.trunc(-1), -1)
776 self.assertEqual(type(math.trunc(1)), int)
777 self.assertEqual(type(math.trunc(1.5)), int)
778 self.assertEqual(math.trunc(1.5), 1)
779 self.assertEqual(math.trunc(-1.5), -1)
780 self.assertEqual(math.trunc(1.999999), 1)
781 self.assertEqual(math.trunc(-1.999999), -1)
782 self.assertEqual(math.trunc(-0.999999), -0)
783 self.assertEqual(math.trunc(-100.999), -100)
784
785 class TestTrunc(object):
786 def __trunc__(self):
787 return 23
788
789 class TestNoTrunc(object):
790 pass
791
792 self.assertEqual(math.trunc(TestTrunc()), 23)
793
794 self.assertRaises(TypeError, math.trunc)
795 self.assertRaises(TypeError, math.trunc, 1, 2)
796 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
797
798 # XXX Doesn't work because the method is looked up on
799 # the type only.
800 #t = TestNoTrunc()
801 #t.__trunc__ = lambda *args: args
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000802 #self.assertEqual((), math.trunc(t))
Christian Heimes400adb02008-02-01 08:12:03 +0000803 #self.assertRaises(TypeError, math.trunc, t, 0)
804
Christian Heimes072c0f12008-01-03 23:01:04 +0000805 def testIsnan(self):
Georg Brandlab91fde2009-08-13 08:51:18 +0000806 self.assertTrue(math.isnan(float("nan")))
807 self.assertTrue(math.isnan(float("inf")* 0.))
808 self.assertFalse(math.isnan(float("inf")))
809 self.assertFalse(math.isnan(0.))
810 self.assertFalse(math.isnan(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000811
812 def testIsinf(self):
Georg Brandlab91fde2009-08-13 08:51:18 +0000813 self.assertTrue(math.isinf(float("inf")))
814 self.assertTrue(math.isinf(float("-inf")))
815 self.assertTrue(math.isinf(1E400))
816 self.assertTrue(math.isinf(-1E400))
817 self.assertFalse(math.isinf(float("nan")))
818 self.assertFalse(math.isinf(0.))
819 self.assertFalse(math.isinf(1.))
Christian Heimes072c0f12008-01-03 23:01:04 +0000820
Thomas Wouters89f507f2006-12-13 04:49:30 +0000821 # RED_FLAG 16-Oct-2000 Tim
822 # While 2.0 is more consistent about exceptions than previous releases, it
823 # still fails this part of the test on some platforms. For now, we only
824 # *run* test_exceptions() in verbose mode, so that this isn't normally
825 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000826
Thomas Wouters89f507f2006-12-13 04:49:30 +0000827 if verbose:
828 def test_exceptions(self):
829 try:
830 x = math.exp(-1000000000)
831 except:
832 # mathmodule.c is failing to weed out underflows from libm, or
833 # we've got an fp format with huge dynamic range
834 self.fail("underflowing exp() should not have raised "
835 "an exception")
836 if x != 0:
837 self.fail("underflowing exp() should have returned 0")
838
839 # If this fails, probably using a strict IEEE-754 conforming libm, and x
840 # is +Inf afterwards. But Python wants overflows detected by default.
841 try:
842 x = math.exp(1000000000)
843 except OverflowError:
844 pass
845 else:
846 self.fail("overflowing exp() didn't trigger OverflowError")
847
848 # If this fails, it could be a puzzle. One odd possibility is that
849 # mathmodule.c's macros are getting confused while comparing
850 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
851 # as a result (and so raising OverflowError instead).
852 try:
853 x = math.sqrt(-1.0)
854 except ValueError:
855 pass
856 else:
857 self.fail("sqrt(-1) didn't raise ValueError")
858
Christian Heimes53876d92008-04-19 00:31:39 +0000859 def test_testfile(self):
860 if not float.__getformat__("double").startswith("IEEE"):
861 return
862 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
863 # Skip if either the input or result is complex, or if
864 # flags is nonempty
865 if ai != 0. or ei != 0. or flags:
866 continue
867 if fn in ['rect', 'polar']:
868 # no real versions of rect, polar
869 continue
870 func = getattr(math, fn)
Christian Heimesa342c012008-04-20 21:01:16 +0000871 try:
872 result = func(ar)
Mark Dickinsona0de26c2008-04-30 23:30:57 +0000873 except ValueError as exc:
874 message = (("Unexpected ValueError: %s\n " +
875 "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
Christian Heimesa342c012008-04-20 21:01:16 +0000876 self.fail(message)
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000877 except OverflowError:
878 message = ("Unexpected OverflowError in " +
879 "test %s:%s(%r)\n" % (id, fn, ar))
880 self.fail(message)
Christian Heimes53876d92008-04-19 00:31:39 +0000881 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000882
883def test_main():
Christian Heimes53876d92008-04-19 00:31:39 +0000884 from doctest import DocFileSuite
885 suite = unittest.TestSuite()
886 suite.addTest(unittest.makeSuite(MathTests))
887 suite.addTest(DocFileSuite("ieee754.txt"))
888 run_unittest(suite)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000889
890if __name__ == '__main__':
891 test_main()