1996-08-08 15:26:25 -03:00
|
|
|
# Python test set -- math module
|
|
|
|
# XXXX Should not do tests around zero only
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
from test.test_support import run_unittest, verbose
|
|
|
|
import unittest
|
|
|
|
import math
|
2008-04-18 20:13:07 -03:00
|
|
|
import os
|
|
|
|
import sys
|
2008-06-09 03:54:45 -03:00
|
|
|
import random
|
2009-09-28 15:54:55 -03:00
|
|
|
import struct
|
1996-08-08 15:26:25 -03:00
|
|
|
|
2008-04-18 20:13:07 -03:00
|
|
|
eps = 1E-05
|
|
|
|
NAN = float('nan')
|
|
|
|
INF = float('inf')
|
|
|
|
NINF = float('-inf')
|
|
|
|
|
2009-09-18 18:01:50 -03:00
|
|
|
# decorator for skipping tests on non-IEEE 754 platforms
|
|
|
|
requires_IEEE_754 = unittest.skipUnless(
|
|
|
|
float.__getformat__("double").startswith("IEEE"),
|
|
|
|
"test requires IEEE 754 doubles")
|
|
|
|
|
2009-04-24 13:34:14 -03:00
|
|
|
# detect evidence of double-rounding: fsum is not always correctly
|
|
|
|
# rounded on machines that suffer from double rounding.
|
|
|
|
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
|
|
|
|
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
|
|
|
|
|
2008-04-18 20:13:07 -03:00
|
|
|
# locate file with test values
|
|
|
|
if __name__ == '__main__':
|
|
|
|
file = sys.argv[0]
|
|
|
|
else:
|
|
|
|
file = __file__
|
|
|
|
test_dir = os.path.dirname(file) or os.curdir
|
2009-09-28 15:54:55 -03:00
|
|
|
math_testcases = os.path.join(test_dir, 'math_testcases.txt')
|
2008-04-18 20:13:07 -03:00
|
|
|
test_file = os.path.join(test_dir, 'cmath_testcases.txt')
|
|
|
|
|
2009-09-28 15:54:55 -03:00
|
|
|
def to_ulps(x):
|
|
|
|
"""Convert a non-NaN float x to an integer, in such a way that
|
|
|
|
adjacent floats are converted to adjacent integers. Then
|
|
|
|
abs(ulps(x) - ulps(y)) gives the difference in ulps between two
|
|
|
|
floats.
|
|
|
|
|
|
|
|
The results from this function will only make sense on platforms
|
|
|
|
where C doubles are represented in IEEE 754 binary64 format.
|
|
|
|
|
|
|
|
"""
|
2009-10-17 04:06:37 -03:00
|
|
|
n = struct.unpack('<q', struct.pack('<d', x))[0]
|
2009-09-28 15:54:55 -03:00
|
|
|
if n < 0:
|
|
|
|
n = ~(n+2**63)
|
|
|
|
return n
|
|
|
|
|
2009-12-11 13:29:33 -04:00
|
|
|
def ulps_check(expected, got, ulps=20):
|
|
|
|
"""Given non-NaN floats `expected` and `got`,
|
|
|
|
check that they're equal to within the given number of ulps.
|
|
|
|
|
|
|
|
Returns None on success and an error message on failure."""
|
|
|
|
|
|
|
|
ulps_error = to_ulps(got) - to_ulps(expected)
|
|
|
|
if abs(ulps_error) <= ulps:
|
|
|
|
return None
|
|
|
|
return "error = {} ulps; permitted error = {} ulps".format(ulps_error,
|
|
|
|
ulps)
|
|
|
|
|
|
|
|
def acc_check(expected, got, rel_err=2e-15, abs_err = 5e-323):
|
|
|
|
"""Determine whether non-NaN floats a and b are equal to within a
|
|
|
|
(small) rounding error. The default values for rel_err and
|
|
|
|
abs_err are chosen to be suitable for platforms where a float is
|
|
|
|
represented by an IEEE 754 double. They allow an error of between
|
|
|
|
9 and 19 ulps."""
|
|
|
|
|
|
|
|
# need to special case infinities, since inf - inf gives nan
|
|
|
|
if math.isinf(expected) and got == expected:
|
|
|
|
return None
|
|
|
|
|
|
|
|
error = got - expected
|
|
|
|
|
|
|
|
permitted_error = max(abs_err, rel_err * abs(expected))
|
|
|
|
if abs(error) < permitted_error:
|
|
|
|
return None
|
|
|
|
return "error = {}; permitted error = {}".format(error,
|
|
|
|
permitted_error)
|
2009-09-28 15:54:55 -03:00
|
|
|
|
|
|
|
def parse_mtestfile(fname):
|
|
|
|
"""Parse a file with test values
|
|
|
|
|
|
|
|
-- starts a comment
|
|
|
|
blank lines, or lines containing only a comment, are ignored
|
|
|
|
other lines are expected to have the form
|
|
|
|
id fn arg -> expected [flag]*
|
|
|
|
|
|
|
|
"""
|
|
|
|
with open(fname) as fp:
|
|
|
|
for line in fp:
|
|
|
|
# strip comments, and skip blank lines
|
|
|
|
if '--' in line:
|
|
|
|
line = line[:line.index('--')]
|
|
|
|
if not line.strip():
|
|
|
|
continue
|
|
|
|
|
|
|
|
lhs, rhs = line.split('->')
|
|
|
|
id, fn, arg = lhs.split()
|
|
|
|
rhs_pieces = rhs.split()
|
|
|
|
exp = rhs_pieces[0]
|
|
|
|
flags = rhs_pieces[1:]
|
|
|
|
|
|
|
|
yield (id, fn, float(arg), float(exp), flags)
|
|
|
|
|
2008-04-18 20:13:07 -03:00
|
|
|
def parse_testfile(fname):
|
|
|
|
"""Parse a file with test values
|
|
|
|
|
|
|
|
Empty lines or lines starting with -- are ignored
|
|
|
|
yields id, fn, arg_real, arg_imag, exp_real, exp_imag
|
|
|
|
"""
|
|
|
|
with open(fname) as fp:
|
|
|
|
for line in fp:
|
|
|
|
# skip comment lines and blank lines
|
|
|
|
if line.startswith('--') or not line.strip():
|
|
|
|
continue
|
|
|
|
|
|
|
|
lhs, rhs = line.split('->')
|
|
|
|
id, fn, arg_real, arg_imag = lhs.split()
|
|
|
|
rhs_pieces = rhs.split()
|
|
|
|
exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
|
|
|
|
flags = rhs_pieces[2:]
|
|
|
|
|
|
|
|
yield (id, fn,
|
|
|
|
float(arg_real), float(arg_imag),
|
|
|
|
float(exp_real), float(exp_imag),
|
|
|
|
flags
|
|
|
|
)
|
1996-08-08 15:26:25 -03:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
class MathTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def ftest(self, name, value, expected):
|
|
|
|
if abs(value-expected) > eps:
|
2007-07-27 07:36:30 -03:00
|
|
|
# Use %r instead of %f so the error message
|
|
|
|
# displays full precision. Otherwise discrepancies
|
|
|
|
# in the last few bits will lead to very confusing
|
|
|
|
# error messages
|
|
|
|
self.fail('%s returned %r, expected %r' %
|
2006-10-28 10:51:49 -03:00
|
|
|
(name, value, expected))
|
|
|
|
|
|
|
|
def testConstants(self):
|
|
|
|
self.ftest('pi', math.pi, 3.1415926)
|
|
|
|
self.ftest('e', math.e, 2.7182818)
|
|
|
|
|
|
|
|
def testAcos(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.acos)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('acos(-1)', math.acos(-1), math.pi)
|
|
|
|
self.ftest('acos(0)', math.acos(0), math.pi/2)
|
|
|
|
self.ftest('acos(1)', math.acos(1), 0)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertRaises(ValueError, math.acos, INF)
|
|
|
|
self.assertRaises(ValueError, math.acos, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.acos(NAN)))
|
2008-04-18 20:13:07 -03:00
|
|
|
|
|
|
|
def testAcosh(self):
|
|
|
|
self.assertRaises(TypeError, math.acosh)
|
|
|
|
self.ftest('acosh(1)', math.acosh(1), 0)
|
|
|
|
self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
|
|
|
|
self.assertRaises(ValueError, math.acosh, 0)
|
|
|
|
self.assertRaises(ValueError, math.acosh, -1)
|
|
|
|
self.assertEquals(math.acosh(INF), INF)
|
|
|
|
self.assertRaises(ValueError, math.acosh, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.acosh(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testAsin(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.asin)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
|
|
|
|
self.ftest('asin(0)', math.asin(0), 0)
|
|
|
|
self.ftest('asin(1)', math.asin(1), math.pi/2)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertRaises(ValueError, math.asin, INF)
|
|
|
|
self.assertRaises(ValueError, math.asin, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.asin(NAN)))
|
2008-04-18 20:13:07 -03:00
|
|
|
|
|
|
|
def testAsinh(self):
|
|
|
|
self.assertRaises(TypeError, math.asinh)
|
|
|
|
self.ftest('asinh(0)', math.asinh(0), 0)
|
|
|
|
self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
|
|
|
|
self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
|
|
|
|
self.assertEquals(math.asinh(INF), INF)
|
|
|
|
self.assertEquals(math.asinh(NINF), NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.asinh(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testAtan(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.atan)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
|
|
|
|
self.ftest('atan(0)', math.atan(0), 0)
|
|
|
|
self.ftest('atan(1)', math.atan(1), math.pi/4)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.ftest('atan(inf)', math.atan(INF), math.pi/2)
|
2008-04-19 15:51:48 -03:00
|
|
|
self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan(NAN)))
|
2008-04-18 20:13:07 -03:00
|
|
|
|
|
|
|
def testAtanh(self):
|
|
|
|
self.assertRaises(TypeError, math.atan)
|
|
|
|
self.ftest('atanh(0)', math.atanh(0), 0)
|
|
|
|
self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
|
|
|
|
self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
|
|
|
|
self.assertRaises(ValueError, math.atanh, 1)
|
|
|
|
self.assertRaises(ValueError, math.atanh, -1)
|
|
|
|
self.assertRaises(ValueError, math.atanh, INF)
|
|
|
|
self.assertRaises(ValueError, math.atanh, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atanh(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testAtan2(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.atan2)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
|
|
|
|
self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
|
|
|
|
self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
|
|
|
|
self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
|
|
|
|
self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
|
|
|
|
|
2008-04-20 17:38:48 -03:00
|
|
|
# math.atan2(0, x)
|
|
|
|
self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
|
|
|
|
self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
|
|
|
|
self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
|
|
|
|
self.assertEqual(math.atan2(0., 0.), 0.)
|
|
|
|
self.assertEqual(math.atan2(0., 2.3), 0.)
|
|
|
|
self.assertEqual(math.atan2(0., INF), 0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan2(0., NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
# math.atan2(-0, x)
|
|
|
|
self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
|
|
|
|
self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
|
|
|
|
self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
|
|
|
|
self.assertEqual(math.atan2(-0., 0.), -0.)
|
|
|
|
self.assertEqual(math.atan2(-0., 2.3), -0.)
|
|
|
|
self.assertEqual(math.atan2(-0., INF), -0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan2(-0., NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
# math.atan2(INF, x)
|
|
|
|
self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
|
|
|
|
self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
|
|
|
|
self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
|
|
|
|
self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
|
|
|
|
self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
|
|
|
|
self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan2(INF, NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
# math.atan2(NINF, x)
|
|
|
|
self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
|
|
|
|
self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
|
|
|
|
self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
|
|
|
|
self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
|
|
|
|
self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
|
|
|
|
self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
# math.atan2(+finite, x)
|
|
|
|
self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
|
|
|
|
self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
|
|
|
|
self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
|
|
|
|
self.assertEqual(math.atan2(2.3, INF), 0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
# math.atan2(-finite, x)
|
|
|
|
self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
|
|
|
|
self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
|
|
|
|
self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
|
|
|
|
self.assertEqual(math.atan2(-2.3, INF), -0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
# math.atan2(NAN, x)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
|
|
|
|
self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
|
|
|
|
self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
|
|
|
|
self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
|
|
|
|
self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
|
|
|
|
self.assertTrue(math.isnan(math.atan2(NAN, INF)))
|
|
|
|
self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testCeil(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.ceil)
|
2008-01-04 04:01:23 -04:00
|
|
|
# These types will be int in py3k.
|
|
|
|
self.assertEquals(float, type(math.ceil(1)))
|
|
|
|
self.assertEquals(float, type(math.ceil(1L)))
|
|
|
|
self.assertEquals(float, type(math.ceil(1.0)))
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('ceil(0.5)', math.ceil(0.5), 1)
|
|
|
|
self.ftest('ceil(1.0)', math.ceil(1.0), 1)
|
|
|
|
self.ftest('ceil(1.5)', math.ceil(1.5), 2)
|
|
|
|
self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
|
|
|
|
self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
|
|
|
|
self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.ceil(INF), INF)
|
|
|
|
self.assertEquals(math.ceil(NINF), NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.ceil(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
class TestCeil(object):
|
2008-01-05 04:47:13 -04:00
|
|
|
def __float__(self):
|
|
|
|
return 41.3
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
class TestNoCeil(object):
|
|
|
|
pass
|
|
|
|
self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
|
|
|
|
self.assertRaises(TypeError, math.ceil, TestNoCeil())
|
|
|
|
|
|
|
|
t = TestNoCeil()
|
|
|
|
t.__ceil__ = lambda *args: args
|
|
|
|
self.assertRaises(TypeError, math.ceil, t)
|
|
|
|
self.assertRaises(TypeError, math.ceil, t, 0)
|
|
|
|
|
2009-09-18 18:01:50 -03:00
|
|
|
@requires_IEEE_754
|
|
|
|
def testCopysign(self):
|
|
|
|
self.assertRaises(TypeError, math.copysign)
|
|
|
|
# copysign should let us distinguish signs of zeros
|
|
|
|
self.assertEquals(copysign(1., 0.), 1.)
|
|
|
|
self.assertEquals(copysign(1., -0.), -1.)
|
|
|
|
self.assertEquals(copysign(INF, 0.), INF)
|
|
|
|
self.assertEquals(copysign(INF, -0.), NINF)
|
|
|
|
self.assertEquals(copysign(NINF, 0.), INF)
|
|
|
|
self.assertEquals(copysign(NINF, -0.), NINF)
|
|
|
|
# and of infinities
|
|
|
|
self.assertEquals(copysign(1., INF), 1.)
|
|
|
|
self.assertEquals(copysign(1., NINF), -1.)
|
|
|
|
self.assertEquals(copysign(INF, INF), INF)
|
|
|
|
self.assertEquals(copysign(INF, NINF), NINF)
|
|
|
|
self.assertEquals(copysign(NINF, INF), INF)
|
|
|
|
self.assertEquals(copysign(NINF, NINF), NINF)
|
|
|
|
self.assertTrue(math.isnan(copysign(NAN, 1.)))
|
|
|
|
self.assertTrue(math.isnan(copysign(NAN, INF)))
|
|
|
|
self.assertTrue(math.isnan(copysign(NAN, NINF)))
|
|
|
|
self.assertTrue(math.isnan(copysign(NAN, NAN)))
|
|
|
|
# copysign(INF, NAN) may be INF or it may be NINF, since
|
|
|
|
# we don't know whether the sign bit of NAN is set on any
|
|
|
|
# given platform.
|
|
|
|
self.assertTrue(math.isinf(copysign(INF, NAN)))
|
|
|
|
# similarly, copysign(2., NAN) could be 2. or -2.
|
|
|
|
self.assertEquals(abs(copysign(2., NAN)), 2.)
|
2008-04-18 20:13:07 -03:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testCos(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.cos)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
|
|
|
|
self.ftest('cos(0)', math.cos(0), 1)
|
|
|
|
self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
|
|
|
|
self.ftest('cos(pi)', math.cos(math.pi), -1)
|
2008-04-18 20:13:07 -03:00
|
|
|
try:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.cos(INF)))
|
|
|
|
self.assertTrue(math.isnan(math.cos(NINF)))
|
2008-04-18 20:13:07 -03:00
|
|
|
except ValueError:
|
|
|
|
self.assertRaises(ValueError, math.cos, INF)
|
|
|
|
self.assertRaises(ValueError, math.cos, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.cos(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testCosh(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.cosh)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('cosh(0)', math.cosh(0), 1)
|
|
|
|
self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.cosh(INF), INF)
|
|
|
|
self.assertEquals(math.cosh(NINF), INF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.cosh(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testDegrees(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.degrees)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
|
|
|
|
self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
|
|
|
|
self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
|
|
|
|
|
|
|
|
def testExp(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.exp)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('exp(-1)', math.exp(-1), 1/math.e)
|
|
|
|
self.ftest('exp(0)', math.exp(0), 1)
|
|
|
|
self.ftest('exp(1)', math.exp(1), math.e)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.exp(INF), INF)
|
|
|
|
self.assertEquals(math.exp(NINF), 0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.exp(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testFabs(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.fabs)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('fabs(-1)', math.fabs(-1), 1)
|
|
|
|
self.ftest('fabs(0)', math.fabs(0), 0)
|
|
|
|
self.ftest('fabs(1)', math.fabs(1), 1)
|
|
|
|
|
2008-06-09 03:54:45 -03:00
|
|
|
def testFactorial(self):
|
|
|
|
def fact(n):
|
|
|
|
result = 1
|
|
|
|
for i in range(1, int(n)+1):
|
|
|
|
result *= i
|
|
|
|
return result
|
|
|
|
values = range(10) + [50, 100, 500]
|
|
|
|
random.shuffle(values)
|
|
|
|
for x in range(10):
|
|
|
|
for cast in (int, long, float):
|
|
|
|
self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
|
|
|
|
self.assertRaises(ValueError, math.factorial, -1)
|
|
|
|
self.assertRaises(ValueError, math.factorial, math.pi)
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testFloor(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.floor)
|
2008-01-04 04:01:23 -04:00
|
|
|
# These types will be int in py3k.
|
|
|
|
self.assertEquals(float, type(math.floor(1)))
|
|
|
|
self.assertEquals(float, type(math.floor(1L)))
|
|
|
|
self.assertEquals(float, type(math.floor(1.0)))
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('floor(0.5)', math.floor(0.5), 0)
|
|
|
|
self.ftest('floor(1.0)', math.floor(1.0), 1)
|
|
|
|
self.ftest('floor(1.5)', math.floor(1.5), 1)
|
|
|
|
self.ftest('floor(-0.5)', math.floor(-0.5), -1)
|
|
|
|
self.ftest('floor(-1.0)', math.floor(-1.0), -1)
|
|
|
|
self.ftest('floor(-1.5)', math.floor(-1.5), -2)
|
2007-07-26 11:03:00 -03:00
|
|
|
# pow() relies on floor() to check for integers
|
|
|
|
# This fails on some platforms - so check it here
|
|
|
|
self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
|
|
|
|
self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.ceil(INF), INF)
|
|
|
|
self.assertEquals(math.ceil(NINF), NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.floor(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
class TestFloor(object):
|
2008-01-05 04:47:13 -04:00
|
|
|
def __float__(self):
|
|
|
|
return 42.3
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
class TestNoFloor(object):
|
|
|
|
pass
|
|
|
|
self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
|
|
|
|
self.assertRaises(TypeError, math.floor, TestNoFloor())
|
|
|
|
|
|
|
|
t = TestNoFloor()
|
|
|
|
t.__floor__ = lambda *args: args
|
|
|
|
self.assertRaises(TypeError, math.floor, t)
|
|
|
|
self.assertRaises(TypeError, math.floor, t, 0)
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testFmod(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.fmod)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('fmod(10,1)', math.fmod(10,1), 0)
|
|
|
|
self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
|
|
|
|
self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
|
|
|
|
self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
|
|
|
|
self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
|
|
|
|
self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
|
|
|
|
self.assertTrue(math.isnan(math.fmod(1., NAN)))
|
|
|
|
self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertRaises(ValueError, math.fmod, 1., 0.)
|
|
|
|
self.assertRaises(ValueError, math.fmod, INF, 1.)
|
|
|
|
self.assertRaises(ValueError, math.fmod, NINF, 1.)
|
|
|
|
self.assertRaises(ValueError, math.fmod, INF, 0.)
|
|
|
|
self.assertEquals(math.fmod(3.0, INF), 3.0)
|
|
|
|
self.assertEquals(math.fmod(-3.0, INF), -3.0)
|
|
|
|
self.assertEquals(math.fmod(3.0, NINF), 3.0)
|
|
|
|
self.assertEquals(math.fmod(-3.0, NINF), -3.0)
|
|
|
|
self.assertEquals(math.fmod(0.0, 3.0), 0.0)
|
|
|
|
self.assertEquals(math.fmod(0.0, NINF), 0.0)
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testFrexp(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.frexp)
|
|
|
|
|
2009-12-20 16:23:01 -04:00
|
|
|
def testfrexp(name, result, expected):
|
|
|
|
(mant, exp), (emant, eexp) = result, expected
|
2006-10-28 10:51:49 -03:00
|
|
|
if abs(mant-emant) > eps or exp != eexp:
|
|
|
|
self.fail('%s returned %r, expected %r'%\
|
|
|
|
(name, (mant, exp), (emant,eexp)))
|
|
|
|
|
|
|
|
testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
|
|
|
|
testfrexp('frexp(0)', math.frexp(0), (0, 0))
|
|
|
|
testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
|
|
|
|
testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
|
|
|
|
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.frexp(INF)[0], INF)
|
|
|
|
self.assertEquals(math.frexp(NINF)[0], NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.frexp(NAN)[0]))
|
2008-04-18 20:13:07 -03:00
|
|
|
|
2009-09-18 18:01:50 -03:00
|
|
|
@requires_IEEE_754
|
2009-04-24 13:37:22 -03:00
|
|
|
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
|
2009-04-24 13:34:14 -03:00
|
|
|
"fsum is not exact on machines with double rounding")
|
2008-07-31 11:48:32 -03:00
|
|
|
def testFsum(self):
|
|
|
|
# math.fsum relies on exact rounding for correct operation.
|
|
|
|
# There's a known problem with IA32 floating-point that causes
|
|
|
|
# inexact rounding in some situations, and will cause the
|
|
|
|
# math.fsum tests below to fail; see issue #2937. On non IEEE
|
|
|
|
# 754 platforms, and on IEEE 754 platforms that exhibit the
|
|
|
|
# problem described in issue #2937, we simply skip the whole
|
|
|
|
# test.
|
|
|
|
|
|
|
|
# Python version of math.fsum, for comparison. Uses a
|
|
|
|
# different algorithm based on frexp, ldexp and integer
|
|
|
|
# arithmetic.
|
|
|
|
from sys import float_info
|
|
|
|
mant_dig = float_info.mant_dig
|
|
|
|
etiny = float_info.min_exp - mant_dig
|
|
|
|
|
|
|
|
def msum(iterable):
|
|
|
|
"""Full precision summation. Compute sum(iterable) without any
|
|
|
|
intermediate accumulation of error. Based on the 'lsum' function
|
|
|
|
at http://code.activestate.com/recipes/393090/
|
|
|
|
|
|
|
|
"""
|
|
|
|
tmant, texp = 0, 0
|
|
|
|
for x in iterable:
|
|
|
|
mant, exp = math.frexp(x)
|
|
|
|
mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
|
|
|
|
if texp > exp:
|
|
|
|
tmant <<= texp-exp
|
|
|
|
texp = exp
|
|
|
|
else:
|
|
|
|
mant <<= exp-texp
|
|
|
|
tmant += mant
|
|
|
|
# Round tmant * 2**texp to a float. The original recipe
|
|
|
|
# used float(str(tmant)) * 2.0**texp for this, but that's
|
|
|
|
# a little unsafe because str -> float conversion can't be
|
|
|
|
# relied upon to do correct rounding on all platforms.
|
|
|
|
tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
|
|
|
|
if tail > 0:
|
|
|
|
h = 1 << (tail-1)
|
|
|
|
tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
|
|
|
|
texp += tail
|
|
|
|
return math.ldexp(tmant, texp)
|
|
|
|
|
|
|
|
test_values = [
|
|
|
|
([], 0.0),
|
|
|
|
([0.0], 0.0),
|
|
|
|
([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
|
|
|
|
([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
|
|
|
|
([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
|
|
|
|
([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
|
|
|
|
([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
|
|
|
|
([1./n for n in range(1, 1001)],
|
|
|
|
float.fromhex('0x1.df11f45f4e61ap+2')),
|
|
|
|
([(-1.)**n/n for n in range(1, 1001)],
|
|
|
|
float.fromhex('-0x1.62a2af1bd3624p-1')),
|
|
|
|
([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
|
|
|
|
([1e16, 1., 1e-16], 10000000000000002.0),
|
|
|
|
([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
|
|
|
|
# exercise code for resizing partials array
|
|
|
|
([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
|
|
|
|
[-2.**1022],
|
|
|
|
float.fromhex('0x1.5555555555555p+970')),
|
|
|
|
]
|
|
|
|
|
|
|
|
for i, (vals, expected) in enumerate(test_values):
|
|
|
|
try:
|
|
|
|
actual = math.fsum(vals)
|
|
|
|
except OverflowError:
|
|
|
|
self.fail("test %d failed: got OverflowError, expected %r "
|
|
|
|
"for math.fsum(%.100r)" % (i, expected, vals))
|
|
|
|
except ValueError:
|
|
|
|
self.fail("test %d failed: got ValueError, expected %r "
|
|
|
|
"for math.fsum(%.100r)" % (i, expected, vals))
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
from random import random, gauss, shuffle
|
|
|
|
for j in xrange(1000):
|
|
|
|
vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
|
|
|
|
s = 0
|
|
|
|
for i in xrange(200):
|
|
|
|
v = gauss(0, random()) ** 7 - s
|
|
|
|
s += v
|
|
|
|
vals.append(v)
|
|
|
|
shuffle(vals)
|
|
|
|
|
|
|
|
s = msum(vals)
|
|
|
|
self.assertEqual(msum(vals), math.fsum(vals))
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testHypot(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.hypot)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('hypot(0,0)', math.hypot(0,0), 0)
|
|
|
|
self.ftest('hypot(3,4)', math.hypot(3,4), 5)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEqual(math.hypot(NAN, INF), INF)
|
|
|
|
self.assertEqual(math.hypot(INF, NAN), INF)
|
|
|
|
self.assertEqual(math.hypot(NAN, NINF), INF)
|
|
|
|
self.assertEqual(math.hypot(NINF, NAN), INF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
|
|
|
|
self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testLdexp(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.ldexp)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
|
|
|
|
self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
|
|
|
|
self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
|
|
|
|
self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
|
|
|
|
self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
|
|
|
|
self.assertEquals(math.ldexp(1., -1000000), 0.)
|
|
|
|
self.assertEquals(math.ldexp(-1., -1000000), -0.)
|
|
|
|
self.assertEquals(math.ldexp(INF, 30), INF)
|
|
|
|
self.assertEquals(math.ldexp(NINF, -213), NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
2008-05-09 14:54:23 -03:00
|
|
|
# large second argument
|
|
|
|
for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
|
|
|
|
self.assertEquals(math.ldexp(INF, -n), INF)
|
|
|
|
self.assertEquals(math.ldexp(NINF, -n), NINF)
|
|
|
|
self.assertEquals(math.ldexp(1., -n), 0.)
|
|
|
|
self.assertEquals(math.ldexp(-1., -n), -0.)
|
|
|
|
self.assertEquals(math.ldexp(0., -n), 0.)
|
|
|
|
self.assertEquals(math.ldexp(-0., -n), -0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
|
2008-05-09 14:54:23 -03:00
|
|
|
|
|
|
|
self.assertRaises(OverflowError, math.ldexp, 1., n)
|
|
|
|
self.assertRaises(OverflowError, math.ldexp, -1., n)
|
|
|
|
self.assertEquals(math.ldexp(0., n), 0.)
|
|
|
|
self.assertEquals(math.ldexp(-0., n), -0.)
|
|
|
|
self.assertEquals(math.ldexp(INF, n), INF)
|
|
|
|
self.assertEquals(math.ldexp(NINF, n), NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.ldexp(NAN, n)))
|
2008-05-09 14:54:23 -03:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testLog(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.log)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('log(1/e)', math.log(1/math.e), -1)
|
|
|
|
self.ftest('log(1)', math.log(1), 0)
|
|
|
|
self.ftest('log(e)', math.log(math.e), 1)
|
|
|
|
self.ftest('log(32,2)', math.log(32,2), 5)
|
|
|
|
self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
|
|
|
|
self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.log(INF), INF)
|
|
|
|
self.assertRaises(ValueError, math.log, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.log(NAN)))
|
2008-04-18 20:13:07 -03:00
|
|
|
|
|
|
|
def testLog1p(self):
|
|
|
|
self.assertRaises(TypeError, math.log1p)
|
|
|
|
self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
|
|
|
|
self.ftest('log1p(0)', math.log1p(0), 0)
|
|
|
|
self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
|
|
|
|
self.ftest('log1p(1)', math.log1p(1), math.log(2))
|
|
|
|
self.assertEquals(math.log1p(INF), INF)
|
|
|
|
self.assertRaises(ValueError, math.log1p, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.log1p(NAN)))
|
2008-04-18 20:13:07 -03:00
|
|
|
n= 2**90
|
|
|
|
self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
|
|
|
|
self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testLog10(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.log10)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('log10(0.1)', math.log10(0.1), -1)
|
|
|
|
self.ftest('log10(1)', math.log10(1), 0)
|
|
|
|
self.ftest('log10(10)', math.log10(10), 1)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.log(INF), INF)
|
|
|
|
self.assertRaises(ValueError, math.log10, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.log10(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testModf(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.modf)
|
|
|
|
|
2009-12-20 16:23:01 -04:00
|
|
|
def testmodf(name, result, expected):
|
|
|
|
(v1, v2), (e1, e2) = result, expected
|
2006-10-28 10:51:49 -03:00
|
|
|
if abs(v1-e1) > eps or abs(v2-e2):
|
|
|
|
self.fail('%s returned %r, expected %r'%\
|
|
|
|
(name, (v1,v2), (e1,e2)))
|
|
|
|
|
|
|
|
testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
|
|
|
|
testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
|
2006-11-02 22:32:46 -04:00
|
|
|
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.modf(INF), (0.0, INF))
|
|
|
|
self.assertEquals(math.modf(NINF), (-0.0, NINF))
|
|
|
|
|
|
|
|
modf_nan = math.modf(NAN)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(modf_nan[0]))
|
|
|
|
self.assertTrue(math.isnan(modf_nan[1]))
|
2008-04-18 20:13:07 -03:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def testPow(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.pow)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('pow(0,1)', math.pow(0,1), 0)
|
|
|
|
self.ftest('pow(1,0)', math.pow(1,0), 1)
|
|
|
|
self.ftest('pow(2,1)', math.pow(2,1), 2)
|
|
|
|
self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEqual(math.pow(INF, 1), INF)
|
|
|
|
self.assertEqual(math.pow(NINF, 1), NINF)
|
|
|
|
self.assertEqual((math.pow(1, INF)), 1.)
|
|
|
|
self.assertEqual((math.pow(1, NINF)), 1.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.pow(NAN, 1)))
|
|
|
|
self.assertTrue(math.isnan(math.pow(2, NAN)))
|
|
|
|
self.assertTrue(math.isnan(math.pow(0, NAN)))
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEqual(math.pow(1, NAN), 1)
|
2008-04-19 15:51:48 -03:00
|
|
|
|
|
|
|
# pow(0., x)
|
|
|
|
self.assertEqual(math.pow(0., INF), 0.)
|
|
|
|
self.assertEqual(math.pow(0., 3.), 0.)
|
|
|
|
self.assertEqual(math.pow(0., 2.3), 0.)
|
|
|
|
self.assertEqual(math.pow(0., 2.), 0.)
|
|
|
|
self.assertEqual(math.pow(0., 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(0., -0.), 1.)
|
|
|
|
self.assertRaises(ValueError, math.pow, 0., -2.)
|
|
|
|
self.assertRaises(ValueError, math.pow, 0., -2.3)
|
|
|
|
self.assertRaises(ValueError, math.pow, 0., -3.)
|
|
|
|
self.assertRaises(ValueError, math.pow, 0., NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.pow(0., NAN)))
|
2008-04-19 15:51:48 -03:00
|
|
|
|
|
|
|
# pow(INF, x)
|
|
|
|
self.assertEqual(math.pow(INF, INF), INF)
|
|
|
|
self.assertEqual(math.pow(INF, 3.), INF)
|
|
|
|
self.assertEqual(math.pow(INF, 2.3), INF)
|
|
|
|
self.assertEqual(math.pow(INF, 2.), INF)
|
|
|
|
self.assertEqual(math.pow(INF, 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(INF, -0.), 1.)
|
|
|
|
self.assertEqual(math.pow(INF, -2.), 0.)
|
|
|
|
self.assertEqual(math.pow(INF, -2.3), 0.)
|
|
|
|
self.assertEqual(math.pow(INF, -3.), 0.)
|
|
|
|
self.assertEqual(math.pow(INF, NINF), 0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.pow(INF, NAN)))
|
2008-04-19 15:51:48 -03:00
|
|
|
|
|
|
|
# pow(-0., x)
|
|
|
|
self.assertEqual(math.pow(-0., INF), 0.)
|
|
|
|
self.assertEqual(math.pow(-0., 3.), -0.)
|
|
|
|
self.assertEqual(math.pow(-0., 2.3), 0.)
|
|
|
|
self.assertEqual(math.pow(-0., 2.), 0.)
|
|
|
|
self.assertEqual(math.pow(-0., 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(-0., -0.), 1.)
|
|
|
|
self.assertRaises(ValueError, math.pow, -0., -2.)
|
|
|
|
self.assertRaises(ValueError, math.pow, -0., -2.3)
|
|
|
|
self.assertRaises(ValueError, math.pow, -0., -3.)
|
|
|
|
self.assertRaises(ValueError, math.pow, -0., NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.pow(-0., NAN)))
|
2008-04-19 15:51:48 -03:00
|
|
|
|
|
|
|
# pow(NINF, x)
|
|
|
|
self.assertEqual(math.pow(NINF, INF), INF)
|
|
|
|
self.assertEqual(math.pow(NINF, 3.), NINF)
|
|
|
|
self.assertEqual(math.pow(NINF, 2.3), INF)
|
|
|
|
self.assertEqual(math.pow(NINF, 2.), INF)
|
|
|
|
self.assertEqual(math.pow(NINF, 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(NINF, -0.), 1.)
|
|
|
|
self.assertEqual(math.pow(NINF, -2.), 0.)
|
|
|
|
self.assertEqual(math.pow(NINF, -2.3), 0.)
|
|
|
|
self.assertEqual(math.pow(NINF, -3.), -0.)
|
|
|
|
self.assertEqual(math.pow(NINF, NINF), 0.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.pow(NINF, NAN)))
|
2008-04-19 15:51:48 -03:00
|
|
|
|
|
|
|
# pow(-1, x)
|
|
|
|
self.assertEqual(math.pow(-1., INF), 1.)
|
|
|
|
self.assertEqual(math.pow(-1., 3.), -1.)
|
|
|
|
self.assertRaises(ValueError, math.pow, -1., 2.3)
|
|
|
|
self.assertEqual(math.pow(-1., 2.), 1.)
|
|
|
|
self.assertEqual(math.pow(-1., 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(-1., -0.), 1.)
|
|
|
|
self.assertEqual(math.pow(-1., -2.), 1.)
|
|
|
|
self.assertRaises(ValueError, math.pow, -1., -2.3)
|
|
|
|
self.assertEqual(math.pow(-1., -3.), -1.)
|
|
|
|
self.assertEqual(math.pow(-1., NINF), 1.)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.pow(-1., NAN)))
|
2008-04-19 15:51:48 -03:00
|
|
|
|
|
|
|
# pow(1, x)
|
|
|
|
self.assertEqual(math.pow(1., INF), 1.)
|
|
|
|
self.assertEqual(math.pow(1., 3.), 1.)
|
|
|
|
self.assertEqual(math.pow(1., 2.3), 1.)
|
|
|
|
self.assertEqual(math.pow(1., 2.), 1.)
|
|
|
|
self.assertEqual(math.pow(1., 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(1., -0.), 1.)
|
|
|
|
self.assertEqual(math.pow(1., -2.), 1.)
|
|
|
|
self.assertEqual(math.pow(1., -2.3), 1.)
|
|
|
|
self.assertEqual(math.pow(1., -3.), 1.)
|
|
|
|
self.assertEqual(math.pow(1., NINF), 1.)
|
|
|
|
self.assertEqual(math.pow(1., NAN), 1.)
|
|
|
|
|
|
|
|
# pow(x, 0) should be 1 for any x
|
|
|
|
self.assertEqual(math.pow(2.3, 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(-2.3, 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(NAN, 0.), 1.)
|
|
|
|
self.assertEqual(math.pow(2.3, -0.), 1.)
|
|
|
|
self.assertEqual(math.pow(-2.3, -0.), 1.)
|
|
|
|
self.assertEqual(math.pow(NAN, -0.), 1.)
|
|
|
|
|
|
|
|
# pow(x, y) is invalid if x is negative and y is not integral
|
|
|
|
self.assertRaises(ValueError, math.pow, -1., 2.3)
|
|
|
|
self.assertRaises(ValueError, math.pow, -15., -3.1)
|
|
|
|
|
|
|
|
# pow(x, NINF)
|
|
|
|
self.assertEqual(math.pow(1.9, NINF), 0.)
|
|
|
|
self.assertEqual(math.pow(1.1, NINF), 0.)
|
|
|
|
self.assertEqual(math.pow(0.9, NINF), INF)
|
|
|
|
self.assertEqual(math.pow(0.1, NINF), INF)
|
|
|
|
self.assertEqual(math.pow(-0.1, NINF), INF)
|
|
|
|
self.assertEqual(math.pow(-0.9, NINF), INF)
|
|
|
|
self.assertEqual(math.pow(-1.1, NINF), 0.)
|
|
|
|
self.assertEqual(math.pow(-1.9, NINF), 0.)
|
|
|
|
|
|
|
|
# pow(x, INF)
|
|
|
|
self.assertEqual(math.pow(1.9, INF), INF)
|
|
|
|
self.assertEqual(math.pow(1.1, INF), INF)
|
|
|
|
self.assertEqual(math.pow(0.9, INF), 0.)
|
|
|
|
self.assertEqual(math.pow(0.1, INF), 0.)
|
|
|
|
self.assertEqual(math.pow(-0.1, INF), 0.)
|
|
|
|
self.assertEqual(math.pow(-0.9, INF), 0.)
|
|
|
|
self.assertEqual(math.pow(-1.1, INF), INF)
|
|
|
|
self.assertEqual(math.pow(-1.9, INF), INF)
|
|
|
|
|
2008-04-20 01:13:13 -03:00
|
|
|
# pow(x, y) should work for x negative, y an integer
|
|
|
|
self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
|
|
|
|
self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
|
|
|
|
self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
|
|
|
|
self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
|
|
|
|
self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
|
|
|
|
self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
|
|
|
|
self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
|
|
|
|
self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
|
|
|
|
self.assertRaises(ValueError, math.pow, -2.0, -0.5)
|
|
|
|
self.assertRaises(ValueError, math.pow, -2.0, 0.5)
|
|
|
|
|
2008-04-19 15:51:48 -03:00
|
|
|
# the following tests have been commented out since they don't
|
|
|
|
# really belong here: the implementation of ** for floats is
|
|
|
|
# independent of the implemention of math.pow
|
|
|
|
#self.assertEqual(1**NAN, 1)
|
|
|
|
#self.assertEqual(1**INF, 1)
|
|
|
|
#self.assertEqual(1**NINF, 1)
|
|
|
|
#self.assertEqual(1**0, 1)
|
|
|
|
#self.assertEqual(1.**NAN, 1)
|
|
|
|
#self.assertEqual(1.**INF, 1)
|
|
|
|
#self.assertEqual(1.**NINF, 1)
|
|
|
|
#self.assertEqual(1.**0, 1)
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testRadians(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.radians)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('radians(180)', math.radians(180), math.pi)
|
|
|
|
self.ftest('radians(90)', math.radians(90), math.pi/2)
|
|
|
|
self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
|
|
|
|
|
|
|
|
def testSin(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.sin)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('sin(0)', math.sin(0), 0)
|
|
|
|
self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
|
|
|
|
self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
|
2008-04-18 20:13:07 -03:00
|
|
|
try:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.sin(INF)))
|
|
|
|
self.assertTrue(math.isnan(math.sin(NINF)))
|
2008-04-18 20:13:07 -03:00
|
|
|
except ValueError:
|
|
|
|
self.assertRaises(ValueError, math.sin, INF)
|
|
|
|
self.assertRaises(ValueError, math.sin, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.sin(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testSinh(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.sinh)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('sinh(0)', math.sinh(0), 0)
|
|
|
|
self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
|
|
|
|
self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.sinh(INF), INF)
|
2008-04-19 15:51:48 -03:00
|
|
|
self.assertEquals(math.sinh(NINF), NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.sinh(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testSqrt(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.sqrt)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('sqrt(0)', math.sqrt(0), 0)
|
|
|
|
self.ftest('sqrt(1)', math.sqrt(1), 1)
|
|
|
|
self.ftest('sqrt(4)', math.sqrt(4), 2)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.assertEquals(math.sqrt(INF), INF)
|
|
|
|
self.assertRaises(ValueError, math.sqrt, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.sqrt(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testTan(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.tan)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('tan(0)', math.tan(0), 0)
|
|
|
|
self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
|
|
|
|
self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
|
2008-04-18 20:13:07 -03:00
|
|
|
try:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.tan(INF)))
|
|
|
|
self.assertTrue(math.isnan(math.tan(NINF)))
|
2008-04-18 20:13:07 -03:00
|
|
|
except:
|
|
|
|
self.assertRaises(ValueError, math.tan, INF)
|
|
|
|
self.assertRaises(ValueError, math.tan, NINF)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.tan(NAN)))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
def testTanh(self):
|
2006-10-29 18:06:28 -04:00
|
|
|
self.assertRaises(TypeError, math.tanh)
|
2006-10-28 10:51:49 -03:00
|
|
|
self.ftest('tanh(0)', math.tanh(0), 0)
|
|
|
|
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.ftest('tanh(inf)', math.tanh(INF), 1)
|
|
|
|
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(math.tanh(NAN)))
|
2008-04-20 17:38:48 -03:00
|
|
|
# check that tanh(-0.) == -0. on IEEE 754 systems
|
|
|
|
if float.__getformat__("double").startswith("IEEE"):
|
|
|
|
self.assertEqual(math.tanh(-0.), -0.)
|
|
|
|
self.assertEqual(math.copysign(1., math.tanh(-0.)),
|
|
|
|
math.copysign(1., -0.))
|
2006-10-28 10:51:49 -03:00
|
|
|
|
2008-02-01 02:22:46 -04:00
|
|
|
def test_trunc(self):
|
|
|
|
self.assertEqual(math.trunc(1), 1)
|
|
|
|
self.assertEqual(math.trunc(-1), -1)
|
|
|
|
self.assertEqual(type(math.trunc(1)), int)
|
|
|
|
self.assertEqual(type(math.trunc(1.5)), int)
|
|
|
|
self.assertEqual(math.trunc(1.5), 1)
|
|
|
|
self.assertEqual(math.trunc(-1.5), -1)
|
|
|
|
self.assertEqual(math.trunc(1.999999), 1)
|
|
|
|
self.assertEqual(math.trunc(-1.999999), -1)
|
|
|
|
self.assertEqual(math.trunc(-0.999999), -0)
|
|
|
|
self.assertEqual(math.trunc(-100.999), -100)
|
|
|
|
|
|
|
|
class TestTrunc(object):
|
|
|
|
def __trunc__(self):
|
|
|
|
return 23
|
|
|
|
|
|
|
|
class TestNoTrunc(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.assertEqual(math.trunc(TestTrunc()), 23)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, math.trunc)
|
|
|
|
self.assertRaises(TypeError, math.trunc, 1, 2)
|
|
|
|
# XXX: This is not ideal, but see the comment in math_trunc().
|
|
|
|
self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
|
|
|
|
|
|
|
|
t = TestNoTrunc()
|
|
|
|
t.__trunc__ = lambda *args: args
|
|
|
|
self.assertEquals((), math.trunc(t))
|
|
|
|
self.assertRaises(TypeError, math.trunc, t, 0)
|
|
|
|
|
2008-01-03 18:32:26 -04:00
|
|
|
def testCopysign(self):
|
|
|
|
self.assertEqual(math.copysign(1, 42), 1.0)
|
|
|
|
self.assertEqual(math.copysign(0., 42), 0.0)
|
|
|
|
self.assertEqual(math.copysign(1., -42), -1.0)
|
|
|
|
self.assertEqual(math.copysign(3, 0.), 3.0)
|
|
|
|
self.assertEqual(math.copysign(4., -0.), -4.0)
|
|
|
|
|
2008-01-03 16:23:15 -04:00
|
|
|
def testIsnan(self):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isnan(float("nan")))
|
|
|
|
self.assertTrue(math.isnan(float("inf")* 0.))
|
|
|
|
self.assertFalse(math.isnan(float("inf")))
|
|
|
|
self.assertFalse(math.isnan(0.))
|
|
|
|
self.assertFalse(math.isnan(1.))
|
2008-01-03 16:23:15 -04:00
|
|
|
|
|
|
|
def testIsinf(self):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(math.isinf(float("inf")))
|
|
|
|
self.assertTrue(math.isinf(float("-inf")))
|
|
|
|
self.assertTrue(math.isinf(1E400))
|
|
|
|
self.assertTrue(math.isinf(-1E400))
|
|
|
|
self.assertFalse(math.isinf(float("nan")))
|
|
|
|
self.assertFalse(math.isinf(0.))
|
|
|
|
self.assertFalse(math.isinf(1.))
|
2008-01-03 16:23:15 -04:00
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
# RED_FLAG 16-Oct-2000 Tim
|
|
|
|
# While 2.0 is more consistent about exceptions than previous releases, it
|
|
|
|
# still fails this part of the test on some platforms. For now, we only
|
|
|
|
# *run* test_exceptions() in verbose mode, so that this isn't normally
|
|
|
|
# tested.
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
def test_exceptions(self):
|
|
|
|
try:
|
|
|
|
x = math.exp(-1000000000)
|
|
|
|
except:
|
|
|
|
# mathmodule.c is failing to weed out underflows from libm, or
|
|
|
|
# we've got an fp format with huge dynamic range
|
|
|
|
self.fail("underflowing exp() should not have raised "
|
|
|
|
"an exception")
|
|
|
|
if x != 0:
|
|
|
|
self.fail("underflowing exp() should have returned 0")
|
|
|
|
|
|
|
|
# If this fails, probably using a strict IEEE-754 conforming libm, and x
|
|
|
|
# is +Inf afterwards. But Python wants overflows detected by default.
|
|
|
|
try:
|
|
|
|
x = math.exp(1000000000)
|
|
|
|
except OverflowError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("overflowing exp() didn't trigger OverflowError")
|
|
|
|
|
|
|
|
# If this fails, it could be a puzzle. One odd possibility is that
|
|
|
|
# mathmodule.c's macros are getting confused while comparing
|
|
|
|
# Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
|
|
|
|
# as a result (and so raising OverflowError instead).
|
|
|
|
try:
|
|
|
|
x = math.sqrt(-1.0)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("sqrt(-1) didn't raise ValueError")
|
|
|
|
|
2009-09-18 18:01:50 -03:00
|
|
|
@requires_IEEE_754
|
2008-04-18 20:13:07 -03:00
|
|
|
def test_testfile(self):
|
|
|
|
for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
|
|
|
|
# Skip if either the input or result is complex, or if
|
|
|
|
# flags is nonempty
|
|
|
|
if ai != 0. or ei != 0. or flags:
|
|
|
|
continue
|
|
|
|
if fn in ['rect', 'polar']:
|
|
|
|
# no real versions of rect, polar
|
|
|
|
continue
|
|
|
|
func = getattr(math, fn)
|
2008-04-19 22:22:30 -03:00
|
|
|
try:
|
|
|
|
result = func(ar)
|
|
|
|
except ValueError:
|
|
|
|
message = ("Unexpected ValueError in " +
|
|
|
|
"test %s:%s(%r)\n" % (id, fn, ar))
|
|
|
|
self.fail(message)
|
2008-05-23 00:30:01 -03:00
|
|
|
except OverflowError:
|
|
|
|
message = ("Unexpected OverflowError in " +
|
|
|
|
"test %s:%s(%r)\n" % (id, fn, ar))
|
|
|
|
self.fail(message)
|
2008-04-18 20:13:07 -03:00
|
|
|
self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
|
2006-10-28 10:51:49 -03:00
|
|
|
|
2009-09-28 15:54:55 -03:00
|
|
|
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
|
|
|
|
"test requires IEEE 754 doubles")
|
|
|
|
def test_mtestfile(self):
|
|
|
|
ALLOWED_ERROR = 20 # permitted error, in ulps
|
|
|
|
fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}"
|
|
|
|
|
|
|
|
failures = []
|
|
|
|
for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
|
|
|
|
func = getattr(math, fn)
|
|
|
|
|
|
|
|
if 'invalid' in flags or 'divide-by-zero' in flags:
|
|
|
|
expected = 'ValueError'
|
|
|
|
elif 'overflow' in flags:
|
|
|
|
expected = 'OverflowError'
|
|
|
|
|
|
|
|
try:
|
|
|
|
got = func(arg)
|
|
|
|
except ValueError:
|
|
|
|
got = 'ValueError'
|
|
|
|
except OverflowError:
|
|
|
|
got = 'OverflowError'
|
|
|
|
|
2009-12-11 13:29:33 -04:00
|
|
|
accuracy_failure = None
|
2009-09-28 15:54:55 -03:00
|
|
|
if isinstance(got, float) and isinstance(expected, float):
|
|
|
|
if math.isnan(expected) and math.isnan(got):
|
|
|
|
continue
|
|
|
|
if not math.isnan(expected) and not math.isnan(got):
|
2009-12-16 16:13:40 -04:00
|
|
|
if fn == 'lgamma':
|
|
|
|
# we use a weaker accuracy test for lgamma;
|
|
|
|
# lgamma only achieves an absolute error of
|
|
|
|
# a few multiples of the machine accuracy, in
|
|
|
|
# general.
|
2009-12-11 13:29:33 -04:00
|
|
|
accuracy_failure = acc_check(expected, got,
|
|
|
|
rel_err = 5e-15,
|
|
|
|
abs_err = 5e-15)
|
|
|
|
else:
|
2009-12-16 16:13:40 -04:00
|
|
|
accuracy_failure = ulps_check(expected, got, 20)
|
2009-12-11 13:29:33 -04:00
|
|
|
if accuracy_failure is None:
|
2009-09-28 15:54:55 -03:00
|
|
|
continue
|
|
|
|
|
|
|
|
if isinstance(got, str) and isinstance(expected, str):
|
|
|
|
if got == expected:
|
|
|
|
continue
|
|
|
|
|
|
|
|
fail_msg = fail_fmt.format(id, fn, arg, expected, got)
|
2009-12-11 13:29:33 -04:00
|
|
|
if accuracy_failure is not None:
|
|
|
|
fail_msg += ' ({})'.format(accuracy_failure)
|
2009-09-28 15:54:55 -03:00
|
|
|
failures.append(fail_msg)
|
|
|
|
|
|
|
|
if failures:
|
|
|
|
self.fail('Failures in test_mtestfile:\n ' +
|
|
|
|
'\n '.join(failures))
|
|
|
|
|
|
|
|
|
2006-10-28 10:51:49 -03:00
|
|
|
def test_main():
|
2008-04-18 20:13:07 -03:00
|
|
|
from doctest import DocFileSuite
|
|
|
|
suite = unittest.TestSuite()
|
|
|
|
suite.addTest(unittest.makeSuite(MathTests))
|
|
|
|
suite.addTest(DocFileSuite("ieee754.txt"))
|
|
|
|
run_unittest(suite)
|
2006-10-28 10:51:49 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|