1 # Python test set -- math module
2 # XXXX Should not do tests around zero only
4 from test
.test_support
import run_unittest
, verbose
16 # detect evidence of double-rounding: fsum is not always correctly
17 # rounded on machines that suffer from double rounding.
18 x
, y
= 1e16
, 2.9999 # use temporary values to defeat peephole optimizer
19 HAVE_DOUBLE_ROUNDING
= (x
+ y
== 1e16
+ 4)
21 # locate file with test values
22 if __name__
== '__main__':
26 test_dir
= os
.path
.dirname(file) or os
.curdir
27 test_file
= os
.path
.join(test_dir
, 'cmath_testcases.txt')
29 def parse_testfile(fname
):
30 """Parse a file with test values
32 Empty lines or lines starting with -- are ignored
33 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
35 with
open(fname
) as fp
:
37 # skip comment lines and blank lines
38 if line
.startswith('--') or not line
.strip():
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:]
48 float(arg_real
), float(arg_imag
),
49 float(exp_real
), float(exp_imag
),
53 class MathTests(unittest
.TestCase
):
55 def ftest(self
, name
, value
, expected
):
56 if abs(value
-expected
) > eps
:
57 # 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
61 self
.fail('%s returned %r, expected %r' %
62 (name
, value
, expected
))
64 def testConstants(self
):
65 self
.ftest('pi', math
.pi
, 3.1415926)
66 self
.ftest('e', math
.e
, 2.7182818)
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)
73 self
.assertRaises(ValueError, math
.acos
, INF
)
74 self
.assertRaises(ValueError, math
.acos
, NINF
)
75 self
.assert_(math
.isnan(math
.acos(NAN
)))
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)
83 self
.assertEquals(math
.acosh(INF
), INF
)
84 self
.assertRaises(ValueError, math
.acosh
, NINF
)
85 self
.assert_(math
.isnan(math
.acosh(NAN
)))
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)
92 self
.assertRaises(ValueError, math
.asin
, INF
)
93 self
.assertRaises(ValueError, math
.asin
, NINF
)
94 self
.assert_(math
.isnan(math
.asin(NAN
)))
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)
101 self
.assertEquals(math
.asinh(INF
), INF
)
102 self
.assertEquals(math
.asinh(NINF
), NINF
)
103 self
.assert_(math
.isnan(math
.asinh(NAN
)))
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)
110 self
.ftest('atan(inf)', math
.atan(INF
), math
.pi
/2)
111 self
.ftest('atan(-inf)', math
.atan(NINF
), -math
.pi
/2)
112 self
.assert_(math
.isnan(math
.atan(NAN
)))
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
)
123 self
.assert_(math
.isnan(math
.atanh(NAN
)))
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)
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.)
140 self
.assert_(math
.isnan(math
.atan2(0., NAN
)))
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.)
148 self
.assert_(math
.isnan(math
.atan2(-0., NAN
)))
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)
156 self
.assert_(math
.isnan(math
.atan2(INF
, NAN
)))
157 # 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)
164 self
.assert_(math
.isnan(math
.atan2(NINF
, NAN
)))
165 # 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.)
170 self
.assert_(math
.isnan(math
.atan2(2.3, NAN
)))
171 # 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.)
176 self
.assert_(math
.isnan(math
.atan2(-2.3, NAN
)))
178 self
.assert_(math
.isnan(math
.atan2(NAN
, NINF
)))
179 self
.assert_(math
.isnan(math
.atan2(NAN
, -2.3)))
180 self
.assert_(math
.isnan(math
.atan2(NAN
, -0.)))
181 self
.assert_(math
.isnan(math
.atan2(NAN
, 0.)))
182 self
.assert_(math
.isnan(math
.atan2(NAN
, 2.3)))
183 self
.assert_(math
.isnan(math
.atan2(NAN
, INF
)))
184 self
.assert_(math
.isnan(math
.atan2(NAN
, NAN
)))
187 self
.assertRaises(TypeError, math
.ceil
)
188 # These types will be int in py3k.
189 self
.assertEquals(float, type(math
.ceil(1)))
190 self
.assertEquals(float, type(math
.ceil(1L)))
191 self
.assertEquals(float, type(math
.ceil(1.0)))
192 self
.ftest('ceil(0.5)', math
.ceil(0.5), 1)
193 self
.ftest('ceil(1.0)', math
.ceil(1.0), 1)
194 self
.ftest('ceil(1.5)', math
.ceil(1.5), 2)
195 self
.ftest('ceil(-0.5)', math
.ceil(-0.5), 0)
196 self
.ftest('ceil(-1.0)', math
.ceil(-1.0), -1)
197 self
.ftest('ceil(-1.5)', math
.ceil(-1.5), -1)
198 self
.assertEquals(math
.ceil(INF
), INF
)
199 self
.assertEquals(math
.ceil(NINF
), NINF
)
200 self
.assert_(math
.isnan(math
.ceil(NAN
)))
202 class TestCeil(object):
205 class TestNoCeil(object):
207 self
.ftest('ceil(TestCeil())', math
.ceil(TestCeil()), 42)
208 self
.assertRaises(TypeError, math
.ceil
, TestNoCeil())
211 t
.__ceil
__ = lambda *args
: args
212 self
.assertRaises(TypeError, math
.ceil
, t
)
213 self
.assertRaises(TypeError, math
.ceil
, t
, 0)
215 if float.__getformat
__("double").startswith("IEEE"):
216 def testCopysign(self
):
217 self
.assertRaises(TypeError, math
.copysign
)
218 # copysign should let us distinguish signs of zeros
219 self
.assertEquals(copysign(1., 0.), 1.)
220 self
.assertEquals(copysign(1., -0.), -1.)
221 self
.assertEquals(copysign(INF
, 0.), INF
)
222 self
.assertEquals(copysign(INF
, -0.), NINF
)
223 self
.assertEquals(copysign(NINF
, 0.), INF
)
224 self
.assertEquals(copysign(NINF
, -0.), NINF
)
226 self
.assertEquals(copysign(1., INF
), 1.)
227 self
.assertEquals(copysign(1., NINF
), -1.)
228 self
.assertEquals(copysign(INF
, INF
), INF
)
229 self
.assertEquals(copysign(INF
, NINF
), NINF
)
230 self
.assertEquals(copysign(NINF
, INF
), INF
)
231 self
.assertEquals(copysign(NINF
, NINF
), NINF
)
232 self
.assert_(math
.isnan(copysign(NAN
, 1.)))
233 self
.assert_(math
.isnan(copysign(NAN
, INF
)))
234 self
.assert_(math
.isnan(copysign(NAN
, NINF
)))
235 self
.assert_(math
.isnan(copysign(NAN
, NAN
)))
236 # copysign(INF, NAN) may be INF or it may be NINF, since
237 # we don't know whether the sign bit of NAN is set on any
239 self
.assert_(math
.isinf(copysign(INF
, NAN
)))
240 # similarly, copysign(2., NAN) could be 2. or -2.
241 self
.assertEquals(abs(copysign(2., NAN
)), 2.)
244 self
.assertRaises(TypeError, math
.cos
)
245 self
.ftest('cos(-pi/2)', math
.cos(-math
.pi
/2), 0)
246 self
.ftest('cos(0)', math
.cos(0), 1)
247 self
.ftest('cos(pi/2)', math
.cos(math
.pi
/2), 0)
248 self
.ftest('cos(pi)', math
.cos(math
.pi
), -1)
250 self
.assert_(math
.isnan(math
.cos(INF
)))
251 self
.assert_(math
.isnan(math
.cos(NINF
)))
253 self
.assertRaises(ValueError, math
.cos
, INF
)
254 self
.assertRaises(ValueError, math
.cos
, NINF
)
255 self
.assert_(math
.isnan(math
.cos(NAN
)))
258 self
.assertRaises(TypeError, math
.cosh
)
259 self
.ftest('cosh(0)', math
.cosh(0), 1)
260 self
.ftest('cosh(2)-2*cosh(1)**2', math
.cosh(2)-2*math
.cosh(1)**2, -1) # Thanks to Lambert
261 self
.assertEquals(math
.cosh(INF
), INF
)
262 self
.assertEquals(math
.cosh(NINF
), INF
)
263 self
.assert_(math
.isnan(math
.cosh(NAN
)))
265 def testDegrees(self
):
266 self
.assertRaises(TypeError, math
.degrees
)
267 self
.ftest('degrees(pi)', math
.degrees(math
.pi
), 180.0)
268 self
.ftest('degrees(pi/2)', math
.degrees(math
.pi
/2), 90.0)
269 self
.ftest('degrees(-pi/4)', math
.degrees(-math
.pi
/4), -45.0)
272 self
.assertRaises(TypeError, math
.exp
)
273 self
.ftest('exp(-1)', math
.exp(-1), 1/math
.e
)
274 self
.ftest('exp(0)', math
.exp(0), 1)
275 self
.ftest('exp(1)', math
.exp(1), math
.e
)
276 self
.assertEquals(math
.exp(INF
), INF
)
277 self
.assertEquals(math
.exp(NINF
), 0.)
278 self
.assert_(math
.isnan(math
.exp(NAN
)))
281 self
.assertRaises(TypeError, math
.fabs
)
282 self
.ftest('fabs(-1)', math
.fabs(-1), 1)
283 self
.ftest('fabs(0)', math
.fabs(0), 0)
284 self
.ftest('fabs(1)', math
.fabs(1), 1)
286 def testFactorial(self
):
289 for i
in range(1, int(n
)+1):
292 values
= range(10) + [50, 100, 500]
293 random
.shuffle(values
)
295 for cast
in (int, long, float):
296 self
.assertEqual(math
.factorial(cast(x
)), fact(x
), (x
, fact(x
), math
.factorial(x
)))
297 self
.assertRaises(ValueError, math
.factorial
, -1)
298 self
.assertRaises(ValueError, math
.factorial
, math
.pi
)
301 self
.assertRaises(TypeError, math
.floor
)
302 # These types will be int in py3k.
303 self
.assertEquals(float, type(math
.floor(1)))
304 self
.assertEquals(float, type(math
.floor(1L)))
305 self
.assertEquals(float, type(math
.floor(1.0)))
306 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)
312 # 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
)
316 self
.assertEquals(math
.ceil(INF
), INF
)
317 self
.assertEquals(math
.ceil(NINF
), NINF
)
318 self
.assert_(math
.isnan(math
.floor(NAN
)))
320 class TestFloor(object):
323 class TestNoFloor(object):
325 self
.ftest('floor(TestFloor())', math
.floor(TestFloor()), 42)
326 self
.assertRaises(TypeError, math
.floor
, TestNoFloor())
329 t
.__floor
__ = lambda *args
: args
330 self
.assertRaises(TypeError, math
.floor
, t
)
331 self
.assertRaises(TypeError, math
.floor
, t
, 0)
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)
341 self
.assert_(math
.isnan(math
.fmod(NAN
, 1.)))
342 self
.assert_(math
.isnan(math
.fmod(1., NAN
)))
343 self
.assert_(math
.isnan(math
.fmod(NAN
, NAN
)))
344 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.)
348 self
.assertEquals(math
.fmod(3.0, INF
), 3.0)
349 self
.assertEquals(math
.fmod(-3.0, INF
), -3.0)
350 self
.assertEquals(math
.fmod(3.0, NINF
), 3.0)
351 self
.assertEquals(math
.fmod(-3.0, NINF
), -3.0)
352 self
.assertEquals(math
.fmod(0.0, 3.0), 0.0)
353 self
.assertEquals(math
.fmod(0.0, NINF
), 0.0)
356 self
.assertRaises(TypeError, math
.frexp
)
358 def testfrexp(name
, (mant
, exp
), (emant
, eexp
)):
359 if abs(mant
-emant
) > eps
or exp
!= eexp
:
360 self
.fail('%s returned %r, expected %r'%\
361 (name
, (mant
, exp
), (emant
,eexp
)))
363 testfrexp('frexp(-1)', math
.frexp(-1), (-0.5, 1))
364 testfrexp('frexp(0)', math
.frexp(0), (0, 0))
365 testfrexp('frexp(1)', math
.frexp(1), (0.5, 1))
366 testfrexp('frexp(2)', math
.frexp(2), (0.5, 2))
368 self
.assertEquals(math
.frexp(INF
)[0], INF
)
369 self
.assertEquals(math
.frexp(NINF
)[0], NINF
)
370 self
.assert_(math
.isnan(math
.frexp(NAN
)[0]))
372 @unittest.skipUnless(float.__getformat
__("double").startswith("IEEE"),
373 "test requires IEEE 754 doubles")
374 @unittest.skipUnless(not HAVE_DOUBLE_ROUNDING
,
375 "fsum is not exact on machines with double rounding")
377 # math.fsum relies on exact rounding for correct operation.
378 # There's a known problem with IA32 floating-point that causes
379 # inexact rounding in some situations, and will cause the
380 # math.fsum tests below to fail; see issue #2937. On non IEEE
381 # 754 platforms, and on IEEE 754 platforms that exhibit the
382 # problem described in issue #2937, we simply skip the whole
385 # Python version of math.fsum, for comparison. Uses a
386 # different algorithm based on frexp, ldexp and integer
388 from sys
import float_info
389 mant_dig
= float_info
.mant_dig
390 etiny
= float_info
.min_exp
- mant_dig
393 """Full precision summation. Compute sum(iterable) without any
394 intermediate accumulation of error. Based on the 'lsum' function
395 at http://code.activestate.com/recipes/393090/
400 mant
, exp
= math
.frexp(x
)
401 mant
, exp
= int(math
.ldexp(mant
, mant_dig
)), exp
- mant_dig
408 # Round tmant * 2**texp to a float. The original recipe
409 # used float(str(tmant)) * 2.0**texp for this, but that's
410 # a little unsafe because str -> float conversion can't be
411 # relied upon to do correct rounding on all platforms.
412 tail
= max(len(bin(abs(tmant
)))-2 - mant_dig
, etiny
- texp
)
415 tmant
= tmant
// (2*h
) + bool(tmant
& h
and tmant
& 3*h
-1)
417 return math
.ldexp(tmant
, texp
)
422 ([1e100
, 1.0, -1e100
, 1e-100, 1e50
, -1.0, -1e50
], 1e-100),
423 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
424 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
425 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
426 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
427 ([1./n
for n
in range(1, 1001)],
428 float.fromhex('0x1.df11f45f4e61ap+2')),
429 ([(-1.)**n
/n
for n
in range(1, 1001)],
430 float.fromhex('-0x1.62a2af1bd3624p-1')),
431 ([1.7**(i
+1)-1.7**i
for i
in range(1000)] + [-1.7**1000], -1.0),
432 ([1e16
, 1., 1e-16], 10000000000000002.0),
433 ([1e16
-2., 1.-2.**-53, -(1e16
-2.), -(1.-2.**-53)], 0.0),
434 # exercise code for resizing partials array
435 ([2.**n
- 2.**(n
+50) + 2.**(n
+52) for n
in range(-1074, 972, 2)] +
437 float.fromhex('0x1.5555555555555p+970')),
440 for i
, (vals
, expected
) in enumerate(test_values
):
442 actual
= math
.fsum(vals
)
443 except OverflowError:
444 self
.fail("test %d failed: got OverflowError, expected %r "
445 "for math.fsum(%.100r)" % (i
, expected
, vals
))
447 self
.fail("test %d failed: got ValueError, expected %r "
448 "for math.fsum(%.100r)" % (i
, expected
, vals
))
449 self
.assertEqual(actual
, expected
)
451 from random
import random
, gauss
, shuffle
452 for j
in xrange(1000):
453 vals
= [7, 1e100
, -7, -1e100
, -9e-20, 8e-20] * 10
455 for i
in xrange(200):
456 v
= gauss(0, random()) ** 7 - s
462 self
.assertEqual(msum(vals
), math
.fsum(vals
))
465 self
.assertRaises(TypeError, math
.hypot
)
466 self
.ftest('hypot(0,0)', math
.hypot(0,0), 0)
467 self
.ftest('hypot(3,4)', math
.hypot(3,4), 5)
468 self
.assertEqual(math
.hypot(NAN
, INF
), INF
)
469 self
.assertEqual(math
.hypot(INF
, NAN
), INF
)
470 self
.assertEqual(math
.hypot(NAN
, NINF
), INF
)
471 self
.assertEqual(math
.hypot(NINF
, NAN
), INF
)
472 self
.assert_(math
.isnan(math
.hypot(1.0, NAN
)))
473 self
.assert_(math
.isnan(math
.hypot(NAN
, -2.0)))
476 self
.assertRaises(TypeError, math
.ldexp
)
477 self
.ftest('ldexp(0,1)', math
.ldexp(0,1), 0)
478 self
.ftest('ldexp(1,1)', math
.ldexp(1,1), 2)
479 self
.ftest('ldexp(1,-1)', math
.ldexp(1,-1), 0.5)
480 self
.ftest('ldexp(-1,1)', math
.ldexp(-1,1), -2)
481 self
.assertRaises(OverflowError, math
.ldexp
, 1., 1000000)
482 self
.assertRaises(OverflowError, math
.ldexp
, -1., 1000000)
483 self
.assertEquals(math
.ldexp(1., -1000000), 0.)
484 self
.assertEquals(math
.ldexp(-1., -1000000), -0.)
485 self
.assertEquals(math
.ldexp(INF
, 30), INF
)
486 self
.assertEquals(math
.ldexp(NINF
, -213), NINF
)
487 self
.assert_(math
.isnan(math
.ldexp(NAN
, 0)))
489 # large second argument
490 for n
in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
491 self
.assertEquals(math
.ldexp(INF
, -n
), INF
)
492 self
.assertEquals(math
.ldexp(NINF
, -n
), NINF
)
493 self
.assertEquals(math
.ldexp(1., -n
), 0.)
494 self
.assertEquals(math
.ldexp(-1., -n
), -0.)
495 self
.assertEquals(math
.ldexp(0., -n
), 0.)
496 self
.assertEquals(math
.ldexp(-0., -n
), -0.)
497 self
.assert_(math
.isnan(math
.ldexp(NAN
, -n
)))
499 self
.assertRaises(OverflowError, math
.ldexp
, 1., n
)
500 self
.assertRaises(OverflowError, math
.ldexp
, -1., n
)
501 self
.assertEquals(math
.ldexp(0., n
), 0.)
502 self
.assertEquals(math
.ldexp(-0., n
), -0.)
503 self
.assertEquals(math
.ldexp(INF
, n
), INF
)
504 self
.assertEquals(math
.ldexp(NINF
, n
), NINF
)
505 self
.assert_(math
.isnan(math
.ldexp(NAN
, n
)))
508 self
.assertRaises(TypeError, math
.log
)
509 self
.ftest('log(1/e)', math
.log(1/math
.e
), -1)
510 self
.ftest('log(1)', math
.log(1), 0)
511 self
.ftest('log(e)', math
.log(math
.e
), 1)
512 self
.ftest('log(32,2)', math
.log(32,2), 5)
513 self
.ftest('log(10**40, 10)', math
.log(10**40, 10), 40)
514 self
.ftest('log(10**40, 10**20)', math
.log(10**40, 10**20), 2)
515 self
.assertEquals(math
.log(INF
), INF
)
516 self
.assertRaises(ValueError, math
.log
, NINF
)
517 self
.assert_(math
.isnan(math
.log(NAN
)))
520 self
.assertRaises(TypeError, math
.log1p
)
521 self
.ftest('log1p(1/e -1)', math
.log1p(1/math
.e
-1), -1)
522 self
.ftest('log1p(0)', math
.log1p(0), 0)
523 self
.ftest('log1p(e-1)', math
.log1p(math
.e
-1), 1)
524 self
.ftest('log1p(1)', math
.log1p(1), math
.log(2))
525 self
.assertEquals(math
.log1p(INF
), INF
)
526 self
.assertRaises(ValueError, math
.log1p
, NINF
)
527 self
.assert_(math
.isnan(math
.log1p(NAN
)))
529 self
.assertAlmostEquals(math
.log1p(n
), 62.383246250395075)
530 self
.assertAlmostEquals(math
.log1p(n
), math
.log1p(float(n
)))
533 self
.assertRaises(TypeError, math
.log10
)
534 self
.ftest('log10(0.1)', math
.log10(0.1), -1)
535 self
.ftest('log10(1)', math
.log10(1), 0)
536 self
.ftest('log10(10)', math
.log10(10), 1)
537 self
.assertEquals(math
.log(INF
), INF
)
538 self
.assertRaises(ValueError, math
.log10
, NINF
)
539 self
.assert_(math
.isnan(math
.log10(NAN
)))
542 self
.assertRaises(TypeError, math
.modf
)
544 def testmodf(name
, (v1
, v2
), (e1
, e2
)):
545 if abs(v1
-e1
) > eps
or abs(v2
-e2
):
546 self
.fail('%s returned %r, expected %r'%\
547 (name
, (v1
,v2
), (e1
,e2
)))
549 testmodf('modf(1.5)', math
.modf(1.5), (0.5, 1.0))
550 testmodf('modf(-1.5)', math
.modf(-1.5), (-0.5, -1.0))
552 self
.assertEquals(math
.modf(INF
), (0.0, INF
))
553 self
.assertEquals(math
.modf(NINF
), (-0.0, NINF
))
555 modf_nan
= math
.modf(NAN
)
556 self
.assert_(math
.isnan(modf_nan
[0]))
557 self
.assert_(math
.isnan(modf_nan
[1]))
560 self
.assertRaises(TypeError, math
.pow)
561 self
.ftest('pow(0,1)', math
.pow(0,1), 0)
562 self
.ftest('pow(1,0)', math
.pow(1,0), 1)
563 self
.ftest('pow(2,1)', math
.pow(2,1), 2)
564 self
.ftest('pow(2,-1)', math
.pow(2,-1), 0.5)
565 self
.assertEqual(math
.pow(INF
, 1), INF
)
566 self
.assertEqual(math
.pow(NINF
, 1), NINF
)
567 self
.assertEqual((math
.pow(1, INF
)), 1.)
568 self
.assertEqual((math
.pow(1, NINF
)), 1.)
569 self
.assert_(math
.isnan(math
.pow(NAN
, 1)))
570 self
.assert_(math
.isnan(math
.pow(2, NAN
)))
571 self
.assert_(math
.isnan(math
.pow(0, NAN
)))
572 self
.assertEqual(math
.pow(1, NAN
), 1)
575 self
.assertEqual(math
.pow(0., INF
), 0.)
576 self
.assertEqual(math
.pow(0., 3.), 0.)
577 self
.assertEqual(math
.pow(0., 2.3), 0.)
578 self
.assertEqual(math
.pow(0., 2.), 0.)
579 self
.assertEqual(math
.pow(0., 0.), 1.)
580 self
.assertEqual(math
.pow(0., -0.), 1.)
581 self
.assertRaises(ValueError, math
.pow, 0., -2.)
582 self
.assertRaises(ValueError, math
.pow, 0., -2.3)
583 self
.assertRaises(ValueError, math
.pow, 0., -3.)
584 self
.assertRaises(ValueError, math
.pow, 0., NINF
)
585 self
.assert_(math
.isnan(math
.pow(0., NAN
)))
588 self
.assertEqual(math
.pow(INF
, INF
), INF
)
589 self
.assertEqual(math
.pow(INF
, 3.), INF
)
590 self
.assertEqual(math
.pow(INF
, 2.3), INF
)
591 self
.assertEqual(math
.pow(INF
, 2.), INF
)
592 self
.assertEqual(math
.pow(INF
, 0.), 1.)
593 self
.assertEqual(math
.pow(INF
, -0.), 1.)
594 self
.assertEqual(math
.pow(INF
, -2.), 0.)
595 self
.assertEqual(math
.pow(INF
, -2.3), 0.)
596 self
.assertEqual(math
.pow(INF
, -3.), 0.)
597 self
.assertEqual(math
.pow(INF
, NINF
), 0.)
598 self
.assert_(math
.isnan(math
.pow(INF
, NAN
)))
601 self
.assertEqual(math
.pow(-0., INF
), 0.)
602 self
.assertEqual(math
.pow(-0., 3.), -0.)
603 self
.assertEqual(math
.pow(-0., 2.3), 0.)
604 self
.assertEqual(math
.pow(-0., 2.), 0.)
605 self
.assertEqual(math
.pow(-0., 0.), 1.)
606 self
.assertEqual(math
.pow(-0., -0.), 1.)
607 self
.assertRaises(ValueError, math
.pow, -0., -2.)
608 self
.assertRaises(ValueError, math
.pow, -0., -2.3)
609 self
.assertRaises(ValueError, math
.pow, -0., -3.)
610 self
.assertRaises(ValueError, math
.pow, -0., NINF
)
611 self
.assert_(math
.isnan(math
.pow(-0., NAN
)))
614 self
.assertEqual(math
.pow(NINF
, INF
), INF
)
615 self
.assertEqual(math
.pow(NINF
, 3.), NINF
)
616 self
.assertEqual(math
.pow(NINF
, 2.3), INF
)
617 self
.assertEqual(math
.pow(NINF
, 2.), INF
)
618 self
.assertEqual(math
.pow(NINF
, 0.), 1.)
619 self
.assertEqual(math
.pow(NINF
, -0.), 1.)
620 self
.assertEqual(math
.pow(NINF
, -2.), 0.)
621 self
.assertEqual(math
.pow(NINF
, -2.3), 0.)
622 self
.assertEqual(math
.pow(NINF
, -3.), -0.)
623 self
.assertEqual(math
.pow(NINF
, NINF
), 0.)
624 self
.assert_(math
.isnan(math
.pow(NINF
, NAN
)))
627 self
.assertEqual(math
.pow(-1., INF
), 1.)
628 self
.assertEqual(math
.pow(-1., 3.), -1.)
629 self
.assertRaises(ValueError, math
.pow, -1., 2.3)
630 self
.assertEqual(math
.pow(-1., 2.), 1.)
631 self
.assertEqual(math
.pow(-1., 0.), 1.)
632 self
.assertEqual(math
.pow(-1., -0.), 1.)
633 self
.assertEqual(math
.pow(-1., -2.), 1.)
634 self
.assertRaises(ValueError, math
.pow, -1., -2.3)
635 self
.assertEqual(math
.pow(-1., -3.), -1.)
636 self
.assertEqual(math
.pow(-1., NINF
), 1.)
637 self
.assert_(math
.isnan(math
.pow(-1., NAN
)))
640 self
.assertEqual(math
.pow(1., INF
), 1.)
641 self
.assertEqual(math
.pow(1., 3.), 1.)
642 self
.assertEqual(math
.pow(1., 2.3), 1.)
643 self
.assertEqual(math
.pow(1., 2.), 1.)
644 self
.assertEqual(math
.pow(1., 0.), 1.)
645 self
.assertEqual(math
.pow(1., -0.), 1.)
646 self
.assertEqual(math
.pow(1., -2.), 1.)
647 self
.assertEqual(math
.pow(1., -2.3), 1.)
648 self
.assertEqual(math
.pow(1., -3.), 1.)
649 self
.assertEqual(math
.pow(1., NINF
), 1.)
650 self
.assertEqual(math
.pow(1., NAN
), 1.)
652 # pow(x, 0) should be 1 for any x
653 self
.assertEqual(math
.pow(2.3, 0.), 1.)
654 self
.assertEqual(math
.pow(-2.3, 0.), 1.)
655 self
.assertEqual(math
.pow(NAN
, 0.), 1.)
656 self
.assertEqual(math
.pow(2.3, -0.), 1.)
657 self
.assertEqual(math
.pow(-2.3, -0.), 1.)
658 self
.assertEqual(math
.pow(NAN
, -0.), 1.)
660 # pow(x, y) is invalid if x is negative and y is not integral
661 self
.assertRaises(ValueError, math
.pow, -1., 2.3)
662 self
.assertRaises(ValueError, math
.pow, -15., -3.1)
665 self
.assertEqual(math
.pow(1.9, NINF
), 0.)
666 self
.assertEqual(math
.pow(1.1, NINF
), 0.)
667 self
.assertEqual(math
.pow(0.9, NINF
), INF
)
668 self
.assertEqual(math
.pow(0.1, NINF
), INF
)
669 self
.assertEqual(math
.pow(-0.1, NINF
), INF
)
670 self
.assertEqual(math
.pow(-0.9, NINF
), INF
)
671 self
.assertEqual(math
.pow(-1.1, NINF
), 0.)
672 self
.assertEqual(math
.pow(-1.9, NINF
), 0.)
675 self
.assertEqual(math
.pow(1.9, INF
), INF
)
676 self
.assertEqual(math
.pow(1.1, INF
), INF
)
677 self
.assertEqual(math
.pow(0.9, INF
), 0.)
678 self
.assertEqual(math
.pow(0.1, INF
), 0.)
679 self
.assertEqual(math
.pow(-0.1, INF
), 0.)
680 self
.assertEqual(math
.pow(-0.9, INF
), 0.)
681 self
.assertEqual(math
.pow(-1.1, INF
), INF
)
682 self
.assertEqual(math
.pow(-1.9, INF
), INF
)
684 # pow(x, y) should work for x negative, y an integer
685 self
.ftest('(-2.)**3.', math
.pow(-2.0, 3.0), -8.0)
686 self
.ftest('(-2.)**2.', math
.pow(-2.0, 2.0), 4.0)
687 self
.ftest('(-2.)**1.', math
.pow(-2.0, 1.0), -2.0)
688 self
.ftest('(-2.)**0.', math
.pow(-2.0, 0.0), 1.0)
689 self
.ftest('(-2.)**-0.', math
.pow(-2.0, -0.0), 1.0)
690 self
.ftest('(-2.)**-1.', math
.pow(-2.0, -1.0), -0.5)
691 self
.ftest('(-2.)**-2.', math
.pow(-2.0, -2.0), 0.25)
692 self
.ftest('(-2.)**-3.', math
.pow(-2.0, -3.0), -0.125)
693 self
.assertRaises(ValueError, math
.pow, -2.0, -0.5)
694 self
.assertRaises(ValueError, math
.pow, -2.0, 0.5)
696 # the following tests have been commented out since they don't
697 # really belong here: the implementation of ** for floats is
698 # independent of the implemention of math.pow
699 #self.assertEqual(1**NAN, 1)
700 #self.assertEqual(1**INF, 1)
701 #self.assertEqual(1**NINF, 1)
702 #self.assertEqual(1**0, 1)
703 #self.assertEqual(1.**NAN, 1)
704 #self.assertEqual(1.**INF, 1)
705 #self.assertEqual(1.**NINF, 1)
706 #self.assertEqual(1.**0, 1)
708 def testRadians(self
):
709 self
.assertRaises(TypeError, math
.radians
)
710 self
.ftest('radians(180)', math
.radians(180), math
.pi
)
711 self
.ftest('radians(90)', math
.radians(90), math
.pi
/2)
712 self
.ftest('radians(-45)', math
.radians(-45), -math
.pi
/4)
715 self
.assertRaises(TypeError, math
.sin
)
716 self
.ftest('sin(0)', math
.sin(0), 0)
717 self
.ftest('sin(pi/2)', math
.sin(math
.pi
/2), 1)
718 self
.ftest('sin(-pi/2)', math
.sin(-math
.pi
/2), -1)
720 self
.assert_(math
.isnan(math
.sin(INF
)))
721 self
.assert_(math
.isnan(math
.sin(NINF
)))
723 self
.assertRaises(ValueError, math
.sin
, INF
)
724 self
.assertRaises(ValueError, math
.sin
, NINF
)
725 self
.assert_(math
.isnan(math
.sin(NAN
)))
728 self
.assertRaises(TypeError, math
.sinh
)
729 self
.ftest('sinh(0)', math
.sinh(0), 0)
730 self
.ftest('sinh(1)**2-cosh(1)**2', math
.sinh(1)**2-math
.cosh(1)**2, -1)
731 self
.ftest('sinh(1)+sinh(-1)', math
.sinh(1)+math
.sinh(-1), 0)
732 self
.assertEquals(math
.sinh(INF
), INF
)
733 self
.assertEquals(math
.sinh(NINF
), NINF
)
734 self
.assert_(math
.isnan(math
.sinh(NAN
)))
737 self
.assertRaises(TypeError, math
.sqrt
)
738 self
.ftest('sqrt(0)', math
.sqrt(0), 0)
739 self
.ftest('sqrt(1)', math
.sqrt(1), 1)
740 self
.ftest('sqrt(4)', math
.sqrt(4), 2)
741 self
.assertEquals(math
.sqrt(INF
), INF
)
742 self
.assertRaises(ValueError, math
.sqrt
, NINF
)
743 self
.assert_(math
.isnan(math
.sqrt(NAN
)))
746 self
.assertRaises(TypeError, math
.tan
)
747 self
.ftest('tan(0)', math
.tan(0), 0)
748 self
.ftest('tan(pi/4)', math
.tan(math
.pi
/4), 1)
749 self
.ftest('tan(-pi/4)', math
.tan(-math
.pi
/4), -1)
751 self
.assert_(math
.isnan(math
.tan(INF
)))
752 self
.assert_(math
.isnan(math
.tan(NINF
)))
754 self
.assertRaises(ValueError, math
.tan
, INF
)
755 self
.assertRaises(ValueError, math
.tan
, NINF
)
756 self
.assert_(math
.isnan(math
.tan(NAN
)))
759 self
.assertRaises(TypeError, math
.tanh
)
760 self
.ftest('tanh(0)', math
.tanh(0), 0)
761 self
.ftest('tanh(1)+tanh(-1)', math
.tanh(1)+math
.tanh(-1), 0)
762 self
.ftest('tanh(inf)', math
.tanh(INF
), 1)
763 self
.ftest('tanh(-inf)', math
.tanh(NINF
), -1)
764 self
.assert_(math
.isnan(math
.tanh(NAN
)))
765 # check that tanh(-0.) == -0. on IEEE 754 systems
766 if float.__getformat
__("double").startswith("IEEE"):
767 self
.assertEqual(math
.tanh(-0.), -0.)
768 self
.assertEqual(math
.copysign(1., math
.tanh(-0.)),
769 math
.copysign(1., -0.))
771 def test_trunc(self
):
772 self
.assertEqual(math
.trunc(1), 1)
773 self
.assertEqual(math
.trunc(-1), -1)
774 self
.assertEqual(type(math
.trunc(1)), int)
775 self
.assertEqual(type(math
.trunc(1.5)), int)
776 self
.assertEqual(math
.trunc(1.5), 1)
777 self
.assertEqual(math
.trunc(-1.5), -1)
778 self
.assertEqual(math
.trunc(1.999999), 1)
779 self
.assertEqual(math
.trunc(-1.999999), -1)
780 self
.assertEqual(math
.trunc(-0.999999), -0)
781 self
.assertEqual(math
.trunc(-100.999), -100)
783 class TestTrunc(object):
787 class TestNoTrunc(object):
790 self
.assertEqual(math
.trunc(TestTrunc()), 23)
792 self
.assertRaises(TypeError, math
.trunc
)
793 self
.assertRaises(TypeError, math
.trunc
, 1, 2)
794 # XXX: This is not ideal, but see the comment in math_trunc().
795 self
.assertRaises(AttributeError, math
.trunc
, TestNoTrunc())
798 t
.__trunc
__ = lambda *args
: args
799 self
.assertEquals((), math
.trunc(t
))
800 self
.assertRaises(TypeError, math
.trunc
, t
, 0)
802 def testCopysign(self
):
803 self
.assertEqual(math
.copysign(1, 42), 1.0)
804 self
.assertEqual(math
.copysign(0., 42), 0.0)
805 self
.assertEqual(math
.copysign(1., -42), -1.0)
806 self
.assertEqual(math
.copysign(3, 0.), 3.0)
807 self
.assertEqual(math
.copysign(4., -0.), -4.0)
810 self
.assert_(math
.isnan(float("nan")))
811 self
.assert_(math
.isnan(float("inf")* 0.))
812 self
.failIf(math
.isnan(float("inf")))
813 self
.failIf(math
.isnan(0.))
814 self
.failIf(math
.isnan(1.))
817 self
.assert_(math
.isinf(float("inf")))
818 self
.assert_(math
.isinf(float("-inf")))
819 self
.assert_(math
.isinf(1E400
))
820 self
.assert_(math
.isinf(-1E400
))
821 self
.failIf(math
.isinf(float("nan")))
822 self
.failIf(math
.isinf(0.))
823 self
.failIf(math
.isinf(1.))
825 # RED_FLAG 16-Oct-2000 Tim
826 # While 2.0 is more consistent about exceptions than previous releases, it
827 # still fails this part of the test on some platforms. For now, we only
828 # *run* test_exceptions() in verbose mode, so that this isn't normally
832 def test_exceptions(self
):
834 x
= math
.exp(-1000000000)
836 # mathmodule.c is failing to weed out underflows from libm, or
837 # we've got an fp format with huge dynamic range
838 self
.fail("underflowing exp() should not have raised "
841 self
.fail("underflowing exp() should have returned 0")
843 # If this fails, probably using a strict IEEE-754 conforming libm, and x
844 # is +Inf afterwards. But Python wants overflows detected by default.
846 x
= math
.exp(1000000000)
847 except OverflowError:
850 self
.fail("overflowing exp() didn't trigger OverflowError")
852 # If this fails, it could be a puzzle. One odd possibility is that
853 # mathmodule.c's macros are getting confused while comparing
854 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
855 # as a result (and so raising OverflowError instead).
861 self
.fail("sqrt(-1) didn't raise ValueError")
863 def test_testfile(self
):
864 if not float.__getformat
__("double").startswith("IEEE"):
866 for id, fn
, ar
, ai
, er
, ei
, flags
in parse_testfile(test_file
):
867 # Skip if either the input or result is complex, or if
869 if ai
!= 0. or ei
!= 0. or flags
:
871 if fn
in ['rect', 'polar']:
872 # no real versions of rect, polar
874 func
= getattr(math
, fn
)
878 message
= ("Unexpected ValueError in " +
879 "test %s:%s(%r)\n" % (id, fn
, ar
))
881 except OverflowError:
882 message
= ("Unexpected OverflowError in " +
883 "test %s:%s(%r)\n" % (id, fn
, ar
))
885 self
.ftest("%s:%s(%r)" % (id, fn
, ar
), result
, er
)
888 from doctest
import DocFileSuite
889 suite
= unittest
.TestSuite()
890 suite
.addTest(unittest
.makeSuite(MathTests
))
891 suite
.addTest(DocFileSuite("ieee754.txt"))
894 if __name__
== '__main__':