summaryrefslogtreecommitdiff
path: root/Lib/test/test_math.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_math.py')
-rw-r--r--Lib/test/test_math.py153
1 files changed, 142 insertions, 11 deletions
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index b8c23db638..401fc1efc8 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -102,7 +102,7 @@ class MathTests(unittest.TestCase):
self.ftest('atan(0)', math.atan(0), 0)
self.ftest('atan(1)', math.atan(1), math.pi/4)
self.ftest('atan(inf)', math.atan(INF), math.pi/2)
- self.ftest('atan(-inf)', math.atan(-INF), -math.pi/2)
+ self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
self.assert_(math.isnan(math.atan(NAN)))
def testAtanh(self):
@@ -383,14 +383,140 @@ class MathTests(unittest.TestCase):
self.assert_(math.isnan(math.pow(2, NAN)))
self.assert_(math.isnan(math.pow(0, NAN)))
self.assertEqual(math.pow(1, NAN), 1)
- 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)
+
+ # 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)
+ self.assert_(math.isnan(math.pow(0., NAN)))
+
+ # 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.)
+ self.assert_(math.isnan(math.pow(INF, NAN)))
+
+ # 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)
+ self.assert_(math.isnan(math.pow(-0., NAN)))
+
+ # 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.)
+ self.assert_(math.isnan(math.pow(NINF, NAN)))
+
+ # 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.)
+ self.assert_(math.isnan(math.pow(-1., NAN)))
+
+ # 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)
+
+ # 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)
+
+ # 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)
def testRadians(self):
self.assertRaises(TypeError, math.radians)
@@ -417,7 +543,7 @@ class MathTests(unittest.TestCase):
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)
self.assertEquals(math.sinh(INF), INF)
- self.assertEquals(math.sinh(-INF), -INF)
+ self.assertEquals(math.sinh(NINF), NINF)
self.assert_(math.isnan(math.sinh(NAN)))
def testSqrt(self):
@@ -555,7 +681,12 @@ class MathTests(unittest.TestCase):
# no real versions of rect, polar
continue
func = getattr(math, fn)
- result = func(ar)
+ try:
+ result = func(ar)
+ except ValueError:
+ message = ("Unexpected ValueError in " +
+ "test %s:%s(%r)\n" % (id, fn, ar))
+ self.fail(message)
self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
def test_main():