symbian-qemu-0.9.1-12/python-2.6.1/Lib/test/test_math.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 # Python test set -- math module
       
     2 # XXXX Should not do tests around zero only
       
     3 
       
     4 from test.test_support import run_unittest, verbose
       
     5 import unittest
       
     6 import math
       
     7 import os
       
     8 import sys
       
     9 import random
       
    10 
       
    11 eps = 1E-05
       
    12 NAN = float('nan')
       
    13 INF = float('inf')
       
    14 NINF = float('-inf')
       
    15 
       
    16 # locate file with test values
       
    17 if __name__ == '__main__':
       
    18     file = sys.argv[0]
       
    19 else:
       
    20     file = __file__
       
    21 test_dir = os.path.dirname(file) or os.curdir
       
    22 test_file = os.path.join(test_dir, 'cmath_testcases.txt')
       
    23 
       
    24 def parse_testfile(fname):
       
    25     """Parse a file with test values
       
    26 
       
    27     Empty lines or lines starting with -- are ignored
       
    28     yields id, fn, arg_real, arg_imag, exp_real, exp_imag
       
    29     """
       
    30     with open(fname) as fp:
       
    31         for line in fp:
       
    32             # skip comment lines and blank lines
       
    33             if line.startswith('--') or not line.strip():
       
    34                 continue
       
    35 
       
    36             lhs, rhs = line.split('->')
       
    37             id, fn, arg_real, arg_imag = lhs.split()
       
    38             rhs_pieces = rhs.split()
       
    39             exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
       
    40             flags = rhs_pieces[2:]
       
    41 
       
    42             yield (id, fn,
       
    43                    float(arg_real), float(arg_imag),
       
    44                    float(exp_real), float(exp_imag),
       
    45                    flags
       
    46                   )
       
    47 
       
    48 class MathTests(unittest.TestCase):
       
    49 
       
    50     def ftest(self, name, value, expected):
       
    51         if abs(value-expected) > eps:
       
    52             # Use %r instead of %f so the error message
       
    53             # displays full precision. Otherwise discrepancies
       
    54             # in the last few bits will lead to very confusing
       
    55             # error messages
       
    56             self.fail('%s returned %r, expected %r' %
       
    57                       (name, value, expected))
       
    58 
       
    59     def testConstants(self):
       
    60         self.ftest('pi', math.pi, 3.1415926)
       
    61         self.ftest('e', math.e, 2.7182818)
       
    62 
       
    63     def testAcos(self):
       
    64         self.assertRaises(TypeError, math.acos)
       
    65         self.ftest('acos(-1)', math.acos(-1), math.pi)
       
    66         self.ftest('acos(0)', math.acos(0), math.pi/2)
       
    67         self.ftest('acos(1)', math.acos(1), 0)
       
    68         self.assertRaises(ValueError, math.acos, INF)
       
    69         self.assertRaises(ValueError, math.acos, NINF)
       
    70         self.assert_(math.isnan(math.acos(NAN)))
       
    71 
       
    72     def testAcosh(self):
       
    73         self.assertRaises(TypeError, math.acosh)
       
    74         self.ftest('acosh(1)', math.acosh(1), 0)
       
    75         self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
       
    76         self.assertRaises(ValueError, math.acosh, 0)
       
    77         self.assertRaises(ValueError, math.acosh, -1)
       
    78         self.assertEquals(math.acosh(INF), INF)
       
    79         self.assertRaises(ValueError, math.acosh, NINF)
       
    80         self.assert_(math.isnan(math.acosh(NAN)))
       
    81 
       
    82     def testAsin(self):
       
    83         self.assertRaises(TypeError, math.asin)
       
    84         self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
       
    85         self.ftest('asin(0)', math.asin(0), 0)
       
    86         self.ftest('asin(1)', math.asin(1), math.pi/2)
       
    87         self.assertRaises(ValueError, math.asin, INF)
       
    88         self.assertRaises(ValueError, math.asin, NINF)
       
    89         self.assert_(math.isnan(math.asin(NAN)))
       
    90 
       
    91     def testAsinh(self):
       
    92         self.assertRaises(TypeError, math.asinh)
       
    93         self.ftest('asinh(0)', math.asinh(0), 0)
       
    94         self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
       
    95         self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
       
    96         self.assertEquals(math.asinh(INF), INF)
       
    97         self.assertEquals(math.asinh(NINF), NINF)
       
    98         self.assert_(math.isnan(math.asinh(NAN)))
       
    99 
       
   100     def testAtan(self):
       
   101         self.assertRaises(TypeError, math.atan)
       
   102         self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
       
   103         self.ftest('atan(0)', math.atan(0), 0)
       
   104         self.ftest('atan(1)', math.atan(1), math.pi/4)
       
   105         self.ftest('atan(inf)', math.atan(INF), math.pi/2)
       
   106         self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
       
   107         self.assert_(math.isnan(math.atan(NAN)))
       
   108 
       
   109     def testAtanh(self):
       
   110         self.assertRaises(TypeError, math.atan)
       
   111         self.ftest('atanh(0)', math.atanh(0), 0)
       
   112         self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
       
   113         self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
       
   114         self.assertRaises(ValueError, math.atanh, 1)
       
   115         self.assertRaises(ValueError, math.atanh, -1)
       
   116         self.assertRaises(ValueError, math.atanh, INF)
       
   117         self.assertRaises(ValueError, math.atanh, NINF)
       
   118         self.assert_(math.isnan(math.atanh(NAN)))
       
   119 
       
   120     def testAtan2(self):
       
   121         self.assertRaises(TypeError, math.atan2)
       
   122         self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
       
   123         self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
       
   124         self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
       
   125         self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
       
   126         self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
       
   127 
       
   128         # math.atan2(0, x)
       
   129         self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
       
   130         self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
       
   131         self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
       
   132         self.assertEqual(math.atan2(0., 0.), 0.)
       
   133         self.assertEqual(math.atan2(0., 2.3), 0.)
       
   134         self.assertEqual(math.atan2(0., INF), 0.)
       
   135         self.assert_(math.isnan(math.atan2(0., NAN)))
       
   136         # math.atan2(-0, x)
       
   137         self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
       
   138         self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
       
   139         self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
       
   140         self.assertEqual(math.atan2(-0., 0.), -0.)
       
   141         self.assertEqual(math.atan2(-0., 2.3), -0.)
       
   142         self.assertEqual(math.atan2(-0., INF), -0.)
       
   143         self.assert_(math.isnan(math.atan2(-0., NAN)))
       
   144         # math.atan2(INF, x)
       
   145         self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
       
   146         self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
       
   147         self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
       
   148         self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
       
   149         self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
       
   150         self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
       
   151         self.assert_(math.isnan(math.atan2(INF, NAN)))
       
   152         # math.atan2(NINF, x)
       
   153         self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
       
   154         self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
       
   155         self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
       
   156         self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
       
   157         self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
       
   158         self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
       
   159         self.assert_(math.isnan(math.atan2(NINF, NAN)))
       
   160         # math.atan2(+finite, x)
       
   161         self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
       
   162         self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
       
   163         self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
       
   164         self.assertEqual(math.atan2(2.3, INF), 0.)
       
   165         self.assert_(math.isnan(math.atan2(2.3, NAN)))
       
   166         # math.atan2(-finite, x)
       
   167         self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
       
   168         self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
       
   169         self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
       
   170         self.assertEqual(math.atan2(-2.3, INF), -0.)
       
   171         self.assert_(math.isnan(math.atan2(-2.3, NAN)))
       
   172         # math.atan2(NAN, x)
       
   173         self.assert_(math.isnan(math.atan2(NAN, NINF)))
       
   174         self.assert_(math.isnan(math.atan2(NAN, -2.3)))
       
   175         self.assert_(math.isnan(math.atan2(NAN, -0.)))
       
   176         self.assert_(math.isnan(math.atan2(NAN, 0.)))
       
   177         self.assert_(math.isnan(math.atan2(NAN, 2.3)))
       
   178         self.assert_(math.isnan(math.atan2(NAN, INF)))
       
   179         self.assert_(math.isnan(math.atan2(NAN, NAN)))
       
   180 
       
   181     def testCeil(self):
       
   182         self.assertRaises(TypeError, math.ceil)
       
   183         # These types will be int in py3k.
       
   184         self.assertEquals(float, type(math.ceil(1)))
       
   185         self.assertEquals(float, type(math.ceil(1L)))
       
   186         self.assertEquals(float, type(math.ceil(1.0)))
       
   187         self.ftest('ceil(0.5)', math.ceil(0.5), 1)
       
   188         self.ftest('ceil(1.0)', math.ceil(1.0), 1)
       
   189         self.ftest('ceil(1.5)', math.ceil(1.5), 2)
       
   190         self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
       
   191         self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
       
   192         self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
       
   193         self.assertEquals(math.ceil(INF), INF)
       
   194         self.assertEquals(math.ceil(NINF), NINF)
       
   195         self.assert_(math.isnan(math.ceil(NAN)))
       
   196 
       
   197         class TestCeil(object):
       
   198             def __float__(self):
       
   199                 return 41.3
       
   200         class TestNoCeil(object):
       
   201             pass
       
   202         self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
       
   203         self.assertRaises(TypeError, math.ceil, TestNoCeil())
       
   204 
       
   205         t = TestNoCeil()
       
   206         t.__ceil__ = lambda *args: args
       
   207         self.assertRaises(TypeError, math.ceil, t)
       
   208         self.assertRaises(TypeError, math.ceil, t, 0)
       
   209 
       
   210     if float.__getformat__("double").startswith("IEEE"):
       
   211         def testCopysign(self):
       
   212             self.assertRaises(TypeError, math.copysign)
       
   213             # copysign should let us distinguish signs of zeros
       
   214             self.assertEquals(copysign(1., 0.), 1.)
       
   215             self.assertEquals(copysign(1., -0.), -1.)
       
   216             self.assertEquals(copysign(INF, 0.), INF)
       
   217             self.assertEquals(copysign(INF, -0.), NINF)
       
   218             self.assertEquals(copysign(NINF, 0.), INF)
       
   219             self.assertEquals(copysign(NINF, -0.), NINF)
       
   220             # and of infinities
       
   221             self.assertEquals(copysign(1., INF), 1.)
       
   222             self.assertEquals(copysign(1., NINF), -1.)
       
   223             self.assertEquals(copysign(INF, INF), INF)
       
   224             self.assertEquals(copysign(INF, NINF), NINF)
       
   225             self.assertEquals(copysign(NINF, INF), INF)
       
   226             self.assertEquals(copysign(NINF, NINF), NINF)
       
   227             self.assert_(math.isnan(copysign(NAN, 1.)))
       
   228             self.assert_(math.isnan(copysign(NAN, INF)))
       
   229             self.assert_(math.isnan(copysign(NAN, NINF)))
       
   230             self.assert_(math.isnan(copysign(NAN, NAN)))
       
   231             # copysign(INF, NAN) may be INF or it may be NINF, since
       
   232             # we don't know whether the sign bit of NAN is set on any
       
   233             # given platform.
       
   234             self.assert_(math.isinf(copysign(INF, NAN)))
       
   235             # similarly, copysign(2., NAN) could be 2. or -2.
       
   236             self.assertEquals(abs(copysign(2., NAN)), 2.)
       
   237 
       
   238     def testCos(self):
       
   239         self.assertRaises(TypeError, math.cos)
       
   240         self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
       
   241         self.ftest('cos(0)', math.cos(0), 1)
       
   242         self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
       
   243         self.ftest('cos(pi)', math.cos(math.pi), -1)
       
   244         try:
       
   245             self.assert_(math.isnan(math.cos(INF)))
       
   246             self.assert_(math.isnan(math.cos(NINF)))
       
   247         except ValueError:
       
   248             self.assertRaises(ValueError, math.cos, INF)
       
   249             self.assertRaises(ValueError, math.cos, NINF)
       
   250         self.assert_(math.isnan(math.cos(NAN)))
       
   251 
       
   252     def testCosh(self):
       
   253         self.assertRaises(TypeError, math.cosh)
       
   254         self.ftest('cosh(0)', math.cosh(0), 1)
       
   255         self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
       
   256         self.assertEquals(math.cosh(INF), INF)
       
   257         self.assertEquals(math.cosh(NINF), INF)
       
   258         self.assert_(math.isnan(math.cosh(NAN)))
       
   259 
       
   260     def testDegrees(self):
       
   261         self.assertRaises(TypeError, math.degrees)
       
   262         self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
       
   263         self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
       
   264         self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
       
   265 
       
   266     def testExp(self):
       
   267         self.assertRaises(TypeError, math.exp)
       
   268         self.ftest('exp(-1)', math.exp(-1), 1/math.e)
       
   269         self.ftest('exp(0)', math.exp(0), 1)
       
   270         self.ftest('exp(1)', math.exp(1), math.e)
       
   271         self.assertEquals(math.exp(INF), INF)
       
   272         self.assertEquals(math.exp(NINF), 0.)
       
   273         self.assert_(math.isnan(math.exp(NAN)))
       
   274 
       
   275     def testFabs(self):
       
   276         self.assertRaises(TypeError, math.fabs)
       
   277         self.ftest('fabs(-1)', math.fabs(-1), 1)
       
   278         self.ftest('fabs(0)', math.fabs(0), 0)
       
   279         self.ftest('fabs(1)', math.fabs(1), 1)
       
   280 
       
   281     def testFactorial(self):
       
   282         def fact(n):
       
   283             result = 1
       
   284             for i in range(1, int(n)+1):
       
   285                 result *= i
       
   286             return result
       
   287         values = range(10) + [50, 100, 500]
       
   288         random.shuffle(values)
       
   289         for x in range(10):
       
   290             for cast in (int, long, float):
       
   291                 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
       
   292         self.assertRaises(ValueError, math.factorial, -1)
       
   293         self.assertRaises(ValueError, math.factorial, math.pi)
       
   294 
       
   295     def testFloor(self):
       
   296         self.assertRaises(TypeError, math.floor)
       
   297         # These types will be int in py3k.
       
   298         self.assertEquals(float, type(math.floor(1)))
       
   299         self.assertEquals(float, type(math.floor(1L)))
       
   300         self.assertEquals(float, type(math.floor(1.0)))
       
   301         self.ftest('floor(0.5)', math.floor(0.5), 0)
       
   302         self.ftest('floor(1.0)', math.floor(1.0), 1)
       
   303         self.ftest('floor(1.5)', math.floor(1.5), 1)
       
   304         self.ftest('floor(-0.5)', math.floor(-0.5), -1)
       
   305         self.ftest('floor(-1.0)', math.floor(-1.0), -1)
       
   306         self.ftest('floor(-1.5)', math.floor(-1.5), -2)
       
   307         # pow() relies on floor() to check for integers
       
   308         # This fails on some platforms - so check it here
       
   309         self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
       
   310         self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
       
   311         self.assertEquals(math.ceil(INF), INF)
       
   312         self.assertEquals(math.ceil(NINF), NINF)
       
   313         self.assert_(math.isnan(math.floor(NAN)))
       
   314 
       
   315         class TestFloor(object):
       
   316             def __float__(self):
       
   317                 return 42.3
       
   318         class TestNoFloor(object):
       
   319             pass
       
   320         self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
       
   321         self.assertRaises(TypeError, math.floor, TestNoFloor())
       
   322 
       
   323         t = TestNoFloor()
       
   324         t.__floor__ = lambda *args: args
       
   325         self.assertRaises(TypeError, math.floor, t)
       
   326         self.assertRaises(TypeError, math.floor, t, 0)
       
   327 
       
   328     def testFmod(self):
       
   329         self.assertRaises(TypeError, math.fmod)
       
   330         self.ftest('fmod(10,1)', math.fmod(10,1), 0)
       
   331         self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
       
   332         self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
       
   333         self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
       
   334         self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
       
   335         self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
       
   336         self.assert_(math.isnan(math.fmod(NAN, 1.)))
       
   337         self.assert_(math.isnan(math.fmod(1., NAN)))
       
   338         self.assert_(math.isnan(math.fmod(NAN, NAN)))
       
   339         self.assertRaises(ValueError, math.fmod, 1., 0.)
       
   340         self.assertRaises(ValueError, math.fmod, INF, 1.)
       
   341         self.assertRaises(ValueError, math.fmod, NINF, 1.)
       
   342         self.assertRaises(ValueError, math.fmod, INF, 0.)
       
   343         self.assertEquals(math.fmod(3.0, INF), 3.0)
       
   344         self.assertEquals(math.fmod(-3.0, INF), -3.0)
       
   345         self.assertEquals(math.fmod(3.0, NINF), 3.0)
       
   346         self.assertEquals(math.fmod(-3.0, NINF), -3.0)
       
   347         self.assertEquals(math.fmod(0.0, 3.0), 0.0)
       
   348         self.assertEquals(math.fmod(0.0, NINF), 0.0)
       
   349 
       
   350     def testFrexp(self):
       
   351         self.assertRaises(TypeError, math.frexp)
       
   352 
       
   353         def testfrexp(name, (mant, exp), (emant, eexp)):
       
   354             if abs(mant-emant) > eps or exp != eexp:
       
   355                 self.fail('%s returned %r, expected %r'%\
       
   356                           (name, (mant, exp), (emant,eexp)))
       
   357 
       
   358         testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
       
   359         testfrexp('frexp(0)', math.frexp(0), (0, 0))
       
   360         testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
       
   361         testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
       
   362 
       
   363         self.assertEquals(math.frexp(INF)[0], INF)
       
   364         self.assertEquals(math.frexp(NINF)[0], NINF)
       
   365         self.assert_(math.isnan(math.frexp(NAN)[0]))
       
   366 
       
   367     def testFsum(self):
       
   368         # math.fsum relies on exact rounding for correct operation.
       
   369         # There's a known problem with IA32 floating-point that causes
       
   370         # inexact rounding in some situations, and will cause the
       
   371         # math.fsum tests below to fail; see issue #2937.  On non IEEE
       
   372         # 754 platforms, and on IEEE 754 platforms that exhibit the
       
   373         # problem described in issue #2937, we simply skip the whole
       
   374         # test.
       
   375 
       
   376         if not float.__getformat__("double").startswith("IEEE"):
       
   377             return
       
   378 
       
   379         # on IEEE 754 compliant machines, both of the expressions
       
   380         # below should round to 10000000000000002.0.
       
   381         if 1e16+2.0 != 1e16+2.9999:
       
   382             return
       
   383 
       
   384         # Python version of math.fsum, for comparison.  Uses a
       
   385         # different algorithm based on frexp, ldexp and integer
       
   386         # arithmetic.
       
   387         from sys import float_info
       
   388         mant_dig = float_info.mant_dig
       
   389         etiny = float_info.min_exp - mant_dig
       
   390 
       
   391         def msum(iterable):
       
   392             """Full precision summation.  Compute sum(iterable) without any
       
   393             intermediate accumulation of error.  Based on the 'lsum' function
       
   394             at http://code.activestate.com/recipes/393090/
       
   395 
       
   396             """
       
   397             tmant, texp = 0, 0
       
   398             for x in iterable:
       
   399                 mant, exp = math.frexp(x)
       
   400                 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
       
   401                 if texp > exp:
       
   402                     tmant <<= texp-exp
       
   403                     texp = exp
       
   404                 else:
       
   405                     mant <<= exp-texp
       
   406                 tmant += mant
       
   407             # Round tmant * 2**texp to a float.  The original recipe
       
   408             # used float(str(tmant)) * 2.0**texp for this, but that's
       
   409             # a little unsafe because str -> float conversion can't be
       
   410             # relied upon to do correct rounding on all platforms.
       
   411             tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
       
   412             if tail > 0:
       
   413                 h = 1 << (tail-1)
       
   414                 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
       
   415                 texp += tail
       
   416             return math.ldexp(tmant, texp)
       
   417 
       
   418         test_values = [
       
   419             ([], 0.0),
       
   420             ([0.0], 0.0),
       
   421             ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
       
   422             ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
       
   423             ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
       
   424             ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
       
   425             ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
       
   426             ([1./n for n in range(1, 1001)],
       
   427              float.fromhex('0x1.df11f45f4e61ap+2')),
       
   428             ([(-1.)**n/n for n in range(1, 1001)],
       
   429              float.fromhex('-0x1.62a2af1bd3624p-1')),
       
   430             ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
       
   431             ([1e16, 1., 1e-16], 10000000000000002.0),
       
   432             ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
       
   433             # exercise code for resizing partials array
       
   434             ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
       
   435              [-2.**1022],
       
   436              float.fromhex('0x1.5555555555555p+970')),
       
   437             ]
       
   438 
       
   439         for i, (vals, expected) in enumerate(test_values):
       
   440             try:
       
   441                 actual = math.fsum(vals)
       
   442             except OverflowError:
       
   443                 self.fail("test %d failed: got OverflowError, expected %r "
       
   444                           "for math.fsum(%.100r)" % (i, expected, vals))
       
   445             except ValueError:
       
   446                 self.fail("test %d failed: got ValueError, expected %r "
       
   447                           "for math.fsum(%.100r)" % (i, expected, vals))
       
   448             self.assertEqual(actual, expected)
       
   449 
       
   450         from random import random, gauss, shuffle
       
   451         for j in xrange(1000):
       
   452             vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
       
   453             s = 0
       
   454             for i in xrange(200):
       
   455                 v = gauss(0, random()) ** 7 - s
       
   456                 s += v
       
   457                 vals.append(v)
       
   458             shuffle(vals)
       
   459 
       
   460             s = msum(vals)
       
   461             self.assertEqual(msum(vals), math.fsum(vals))
       
   462 
       
   463     def testHypot(self):
       
   464         self.assertRaises(TypeError, math.hypot)
       
   465         self.ftest('hypot(0,0)', math.hypot(0,0), 0)
       
   466         self.ftest('hypot(3,4)', math.hypot(3,4), 5)
       
   467         self.assertEqual(math.hypot(NAN, INF), INF)
       
   468         self.assertEqual(math.hypot(INF, NAN), INF)
       
   469         self.assertEqual(math.hypot(NAN, NINF), INF)
       
   470         self.assertEqual(math.hypot(NINF, NAN), INF)
       
   471         self.assert_(math.isnan(math.hypot(1.0, NAN)))
       
   472         self.assert_(math.isnan(math.hypot(NAN, -2.0)))
       
   473 
       
   474     def testLdexp(self):
       
   475         self.assertRaises(TypeError, math.ldexp)
       
   476         self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
       
   477         self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
       
   478         self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
       
   479         self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
       
   480         self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
       
   481         self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
       
   482         self.assertEquals(math.ldexp(1., -1000000), 0.)
       
   483         self.assertEquals(math.ldexp(-1., -1000000), -0.)
       
   484         self.assertEquals(math.ldexp(INF, 30), INF)
       
   485         self.assertEquals(math.ldexp(NINF, -213), NINF)
       
   486         self.assert_(math.isnan(math.ldexp(NAN, 0)))
       
   487 
       
   488         # large second argument
       
   489         for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
       
   490             self.assertEquals(math.ldexp(INF, -n), INF)
       
   491             self.assertEquals(math.ldexp(NINF, -n), NINF)
       
   492             self.assertEquals(math.ldexp(1., -n), 0.)
       
   493             self.assertEquals(math.ldexp(-1., -n), -0.)
       
   494             self.assertEquals(math.ldexp(0., -n), 0.)
       
   495             self.assertEquals(math.ldexp(-0., -n), -0.)
       
   496             self.assert_(math.isnan(math.ldexp(NAN, -n)))
       
   497 
       
   498             self.assertRaises(OverflowError, math.ldexp, 1., n)
       
   499             self.assertRaises(OverflowError, math.ldexp, -1., n)
       
   500             self.assertEquals(math.ldexp(0., n), 0.)
       
   501             self.assertEquals(math.ldexp(-0., n), -0.)
       
   502             self.assertEquals(math.ldexp(INF, n), INF)
       
   503             self.assertEquals(math.ldexp(NINF, n), NINF)
       
   504             self.assert_(math.isnan(math.ldexp(NAN, n)))
       
   505 
       
   506     def testLog(self):
       
   507         self.assertRaises(TypeError, math.log)
       
   508         self.ftest('log(1/e)', math.log(1/math.e), -1)
       
   509         self.ftest('log(1)', math.log(1), 0)
       
   510         self.ftest('log(e)', math.log(math.e), 1)
       
   511         self.ftest('log(32,2)', math.log(32,2), 5)
       
   512         self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
       
   513         self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
       
   514         self.assertEquals(math.log(INF), INF)
       
   515         self.assertRaises(ValueError, math.log, NINF)
       
   516         self.assert_(math.isnan(math.log(NAN)))
       
   517 
       
   518     def testLog1p(self):
       
   519         self.assertRaises(TypeError, math.log1p)
       
   520         self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
       
   521         self.ftest('log1p(0)', math.log1p(0), 0)
       
   522         self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
       
   523         self.ftest('log1p(1)', math.log1p(1), math.log(2))
       
   524         self.assertEquals(math.log1p(INF), INF)
       
   525         self.assertRaises(ValueError, math.log1p, NINF)
       
   526         self.assert_(math.isnan(math.log1p(NAN)))
       
   527         n= 2**90
       
   528         self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
       
   529         self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
       
   530 
       
   531     def testLog10(self):
       
   532         self.assertRaises(TypeError, math.log10)
       
   533         self.ftest('log10(0.1)', math.log10(0.1), -1)
       
   534         self.ftest('log10(1)', math.log10(1), 0)
       
   535         self.ftest('log10(10)', math.log10(10), 1)
       
   536         self.assertEquals(math.log(INF), INF)
       
   537         self.assertRaises(ValueError, math.log10, NINF)
       
   538         self.assert_(math.isnan(math.log10(NAN)))
       
   539 
       
   540     def testModf(self):
       
   541         self.assertRaises(TypeError, math.modf)
       
   542 
       
   543         def testmodf(name, (v1, v2), (e1, e2)):
       
   544             if abs(v1-e1) > eps or abs(v2-e2):
       
   545                 self.fail('%s returned %r, expected %r'%\
       
   546                           (name, (v1,v2), (e1,e2)))
       
   547 
       
   548         testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
       
   549         testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
       
   550 
       
   551         self.assertEquals(math.modf(INF), (0.0, INF))
       
   552         self.assertEquals(math.modf(NINF), (-0.0, NINF))
       
   553 
       
   554         modf_nan = math.modf(NAN)
       
   555         self.assert_(math.isnan(modf_nan[0]))
       
   556         self.assert_(math.isnan(modf_nan[1]))
       
   557 
       
   558     def testPow(self):
       
   559         self.assertRaises(TypeError, math.pow)
       
   560         self.ftest('pow(0,1)', math.pow(0,1), 0)
       
   561         self.ftest('pow(1,0)', math.pow(1,0), 1)
       
   562         self.ftest('pow(2,1)', math.pow(2,1), 2)
       
   563         self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
       
   564         self.assertEqual(math.pow(INF, 1), INF)
       
   565         self.assertEqual(math.pow(NINF, 1), NINF)
       
   566         self.assertEqual((math.pow(1, INF)), 1.)
       
   567         self.assertEqual((math.pow(1, NINF)), 1.)
       
   568         self.assert_(math.isnan(math.pow(NAN, 1)))
       
   569         self.assert_(math.isnan(math.pow(2, NAN)))
       
   570         self.assert_(math.isnan(math.pow(0, NAN)))
       
   571         self.assertEqual(math.pow(1, NAN), 1)
       
   572 
       
   573         # pow(0., x)
       
   574         self.assertEqual(math.pow(0., INF), 0.)
       
   575         self.assertEqual(math.pow(0., 3.), 0.)
       
   576         self.assertEqual(math.pow(0., 2.3), 0.)
       
   577         self.assertEqual(math.pow(0., 2.), 0.)
       
   578         self.assertEqual(math.pow(0., 0.), 1.)
       
   579         self.assertEqual(math.pow(0., -0.), 1.)
       
   580         self.assertRaises(ValueError, math.pow, 0., -2.)
       
   581         self.assertRaises(ValueError, math.pow, 0., -2.3)
       
   582         self.assertRaises(ValueError, math.pow, 0., -3.)
       
   583         self.assertRaises(ValueError, math.pow, 0., NINF)
       
   584         self.assert_(math.isnan(math.pow(0., NAN)))
       
   585 
       
   586         # pow(INF, x)
       
   587         self.assertEqual(math.pow(INF, INF), INF)
       
   588         self.assertEqual(math.pow(INF, 3.), INF)
       
   589         self.assertEqual(math.pow(INF, 2.3), INF)
       
   590         self.assertEqual(math.pow(INF, 2.), INF)
       
   591         self.assertEqual(math.pow(INF, 0.), 1.)
       
   592         self.assertEqual(math.pow(INF, -0.), 1.)
       
   593         self.assertEqual(math.pow(INF, -2.), 0.)
       
   594         self.assertEqual(math.pow(INF, -2.3), 0.)
       
   595         self.assertEqual(math.pow(INF, -3.), 0.)
       
   596         self.assertEqual(math.pow(INF, NINF), 0.)
       
   597         self.assert_(math.isnan(math.pow(INF, NAN)))
       
   598 
       
   599         # pow(-0., x)
       
   600         self.assertEqual(math.pow(-0., INF), 0.)
       
   601         self.assertEqual(math.pow(-0., 3.), -0.)
       
   602         self.assertEqual(math.pow(-0., 2.3), 0.)
       
   603         self.assertEqual(math.pow(-0., 2.), 0.)
       
   604         self.assertEqual(math.pow(-0., 0.), 1.)
       
   605         self.assertEqual(math.pow(-0., -0.), 1.)
       
   606         self.assertRaises(ValueError, math.pow, -0., -2.)
       
   607         self.assertRaises(ValueError, math.pow, -0., -2.3)
       
   608         self.assertRaises(ValueError, math.pow, -0., -3.)
       
   609         self.assertRaises(ValueError, math.pow, -0., NINF)
       
   610         self.assert_(math.isnan(math.pow(-0., NAN)))
       
   611 
       
   612         # pow(NINF, x)
       
   613         self.assertEqual(math.pow(NINF, INF), INF)
       
   614         self.assertEqual(math.pow(NINF, 3.), NINF)
       
   615         self.assertEqual(math.pow(NINF, 2.3), INF)
       
   616         self.assertEqual(math.pow(NINF, 2.), INF)
       
   617         self.assertEqual(math.pow(NINF, 0.), 1.)
       
   618         self.assertEqual(math.pow(NINF, -0.), 1.)
       
   619         self.assertEqual(math.pow(NINF, -2.), 0.)
       
   620         self.assertEqual(math.pow(NINF, -2.3), 0.)
       
   621         self.assertEqual(math.pow(NINF, -3.), -0.)
       
   622         self.assertEqual(math.pow(NINF, NINF), 0.)
       
   623         self.assert_(math.isnan(math.pow(NINF, NAN)))
       
   624 
       
   625         # pow(-1, x)
       
   626         self.assertEqual(math.pow(-1., INF), 1.)
       
   627         self.assertEqual(math.pow(-1., 3.), -1.)
       
   628         self.assertRaises(ValueError, math.pow, -1., 2.3)
       
   629         self.assertEqual(math.pow(-1., 2.), 1.)
       
   630         self.assertEqual(math.pow(-1., 0.), 1.)
       
   631         self.assertEqual(math.pow(-1., -0.), 1.)
       
   632         self.assertEqual(math.pow(-1., -2.), 1.)
       
   633         self.assertRaises(ValueError, math.pow, -1., -2.3)
       
   634         self.assertEqual(math.pow(-1., -3.), -1.)
       
   635         self.assertEqual(math.pow(-1., NINF), 1.)
       
   636         self.assert_(math.isnan(math.pow(-1., NAN)))
       
   637 
       
   638         # pow(1, x)
       
   639         self.assertEqual(math.pow(1., INF), 1.)
       
   640         self.assertEqual(math.pow(1., 3.), 1.)
       
   641         self.assertEqual(math.pow(1., 2.3), 1.)
       
   642         self.assertEqual(math.pow(1., 2.), 1.)
       
   643         self.assertEqual(math.pow(1., 0.), 1.)
       
   644         self.assertEqual(math.pow(1., -0.), 1.)
       
   645         self.assertEqual(math.pow(1., -2.), 1.)
       
   646         self.assertEqual(math.pow(1., -2.3), 1.)
       
   647         self.assertEqual(math.pow(1., -3.), 1.)
       
   648         self.assertEqual(math.pow(1., NINF), 1.)
       
   649         self.assertEqual(math.pow(1., NAN), 1.)
       
   650 
       
   651         # pow(x, 0) should be 1 for any x
       
   652         self.assertEqual(math.pow(2.3, 0.), 1.)
       
   653         self.assertEqual(math.pow(-2.3, 0.), 1.)
       
   654         self.assertEqual(math.pow(NAN, 0.), 1.)
       
   655         self.assertEqual(math.pow(2.3, -0.), 1.)
       
   656         self.assertEqual(math.pow(-2.3, -0.), 1.)
       
   657         self.assertEqual(math.pow(NAN, -0.), 1.)
       
   658 
       
   659         # pow(x, y) is invalid if x is negative and y is not integral
       
   660         self.assertRaises(ValueError, math.pow, -1., 2.3)
       
   661         self.assertRaises(ValueError, math.pow, -15., -3.1)
       
   662 
       
   663         # pow(x, NINF)
       
   664         self.assertEqual(math.pow(1.9, NINF), 0.)
       
   665         self.assertEqual(math.pow(1.1, NINF), 0.)
       
   666         self.assertEqual(math.pow(0.9, NINF), INF)
       
   667         self.assertEqual(math.pow(0.1, NINF), INF)
       
   668         self.assertEqual(math.pow(-0.1, NINF), INF)
       
   669         self.assertEqual(math.pow(-0.9, NINF), INF)
       
   670         self.assertEqual(math.pow(-1.1, NINF), 0.)
       
   671         self.assertEqual(math.pow(-1.9, NINF), 0.)
       
   672 
       
   673         # pow(x, INF)
       
   674         self.assertEqual(math.pow(1.9, INF), INF)
       
   675         self.assertEqual(math.pow(1.1, INF), INF)
       
   676         self.assertEqual(math.pow(0.9, INF), 0.)
       
   677         self.assertEqual(math.pow(0.1, INF), 0.)
       
   678         self.assertEqual(math.pow(-0.1, INF), 0.)
       
   679         self.assertEqual(math.pow(-0.9, INF), 0.)
       
   680         self.assertEqual(math.pow(-1.1, INF), INF)
       
   681         self.assertEqual(math.pow(-1.9, INF), INF)
       
   682 
       
   683         # pow(x, y) should work for x negative, y an integer
       
   684         self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
       
   685         self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
       
   686         self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
       
   687         self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
       
   688         self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
       
   689         self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
       
   690         self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
       
   691         self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
       
   692         self.assertRaises(ValueError, math.pow, -2.0, -0.5)
       
   693         self.assertRaises(ValueError, math.pow, -2.0, 0.5)
       
   694 
       
   695         # the following tests have been commented out since they don't
       
   696         # really belong here:  the implementation of ** for floats is
       
   697         # independent of the implemention of math.pow
       
   698         #self.assertEqual(1**NAN, 1)
       
   699         #self.assertEqual(1**INF, 1)
       
   700         #self.assertEqual(1**NINF, 1)
       
   701         #self.assertEqual(1**0, 1)
       
   702         #self.assertEqual(1.**NAN, 1)
       
   703         #self.assertEqual(1.**INF, 1)
       
   704         #self.assertEqual(1.**NINF, 1)
       
   705         #self.assertEqual(1.**0, 1)
       
   706 
       
   707     def testRadians(self):
       
   708         self.assertRaises(TypeError, math.radians)
       
   709         self.ftest('radians(180)', math.radians(180), math.pi)
       
   710         self.ftest('radians(90)', math.radians(90), math.pi/2)
       
   711         self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
       
   712 
       
   713     def testSin(self):
       
   714         self.assertRaises(TypeError, math.sin)
       
   715         self.ftest('sin(0)', math.sin(0), 0)
       
   716         self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
       
   717         self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
       
   718         try:
       
   719             self.assert_(math.isnan(math.sin(INF)))
       
   720             self.assert_(math.isnan(math.sin(NINF)))
       
   721         except ValueError:
       
   722             self.assertRaises(ValueError, math.sin, INF)
       
   723             self.assertRaises(ValueError, math.sin, NINF)
       
   724         self.assert_(math.isnan(math.sin(NAN)))
       
   725 
       
   726     def testSinh(self):
       
   727         self.assertRaises(TypeError, math.sinh)
       
   728         self.ftest('sinh(0)', math.sinh(0), 0)
       
   729         self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
       
   730         self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
       
   731         self.assertEquals(math.sinh(INF), INF)
       
   732         self.assertEquals(math.sinh(NINF), NINF)
       
   733         self.assert_(math.isnan(math.sinh(NAN)))
       
   734 
       
   735     def testSqrt(self):
       
   736         self.assertRaises(TypeError, math.sqrt)
       
   737         self.ftest('sqrt(0)', math.sqrt(0), 0)
       
   738         self.ftest('sqrt(1)', math.sqrt(1), 1)
       
   739         self.ftest('sqrt(4)', math.sqrt(4), 2)
       
   740         self.assertEquals(math.sqrt(INF), INF)
       
   741         self.assertRaises(ValueError, math.sqrt, NINF)
       
   742         self.assert_(math.isnan(math.sqrt(NAN)))
       
   743 
       
   744     def testTan(self):
       
   745         self.assertRaises(TypeError, math.tan)
       
   746         self.ftest('tan(0)', math.tan(0), 0)
       
   747         self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
       
   748         self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
       
   749         try:
       
   750             self.assert_(math.isnan(math.tan(INF)))
       
   751             self.assert_(math.isnan(math.tan(NINF)))
       
   752         except:
       
   753             self.assertRaises(ValueError, math.tan, INF)
       
   754             self.assertRaises(ValueError, math.tan, NINF)
       
   755         self.assert_(math.isnan(math.tan(NAN)))
       
   756 
       
   757     def testTanh(self):
       
   758         self.assertRaises(TypeError, math.tanh)
       
   759         self.ftest('tanh(0)', math.tanh(0), 0)
       
   760         self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
       
   761         self.ftest('tanh(inf)', math.tanh(INF), 1)
       
   762         self.ftest('tanh(-inf)', math.tanh(NINF), -1)
       
   763         self.assert_(math.isnan(math.tanh(NAN)))
       
   764         # check that tanh(-0.) == -0. on IEEE 754 systems
       
   765         if float.__getformat__("double").startswith("IEEE"):
       
   766             self.assertEqual(math.tanh(-0.), -0.)
       
   767             self.assertEqual(math.copysign(1., math.tanh(-0.)),
       
   768                              math.copysign(1., -0.))
       
   769 
       
   770     def test_trunc(self):
       
   771         self.assertEqual(math.trunc(1), 1)
       
   772         self.assertEqual(math.trunc(-1), -1)
       
   773         self.assertEqual(type(math.trunc(1)), int)
       
   774         self.assertEqual(type(math.trunc(1.5)), int)
       
   775         self.assertEqual(math.trunc(1.5), 1)
       
   776         self.assertEqual(math.trunc(-1.5), -1)
       
   777         self.assertEqual(math.trunc(1.999999), 1)
       
   778         self.assertEqual(math.trunc(-1.999999), -1)
       
   779         self.assertEqual(math.trunc(-0.999999), -0)
       
   780         self.assertEqual(math.trunc(-100.999), -100)
       
   781 
       
   782         class TestTrunc(object):
       
   783             def __trunc__(self):
       
   784                 return 23
       
   785 
       
   786         class TestNoTrunc(object):
       
   787             pass
       
   788 
       
   789         self.assertEqual(math.trunc(TestTrunc()), 23)
       
   790 
       
   791         self.assertRaises(TypeError, math.trunc)
       
   792         self.assertRaises(TypeError, math.trunc, 1, 2)
       
   793         # XXX: This is not ideal, but see the comment in math_trunc().
       
   794         self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
       
   795 
       
   796         t = TestNoTrunc()
       
   797         t.__trunc__ = lambda *args: args
       
   798         self.assertEquals((), math.trunc(t))
       
   799         self.assertRaises(TypeError, math.trunc, t, 0)
       
   800 
       
   801     def testCopysign(self):
       
   802         self.assertEqual(math.copysign(1, 42), 1.0)
       
   803         self.assertEqual(math.copysign(0., 42), 0.0)
       
   804         self.assertEqual(math.copysign(1., -42), -1.0)
       
   805         self.assertEqual(math.copysign(3, 0.), 3.0)
       
   806         self.assertEqual(math.copysign(4., -0.), -4.0)
       
   807 
       
   808     def testIsnan(self):
       
   809         self.assert_(math.isnan(float("nan")))
       
   810         self.assert_(math.isnan(float("inf")* 0.))
       
   811         self.failIf(math.isnan(float("inf")))
       
   812         self.failIf(math.isnan(0.))
       
   813         self.failIf(math.isnan(1.))
       
   814 
       
   815     def testIsinf(self):
       
   816         self.assert_(math.isinf(float("inf")))
       
   817         self.assert_(math.isinf(float("-inf")))
       
   818         self.assert_(math.isinf(1E400))
       
   819         self.assert_(math.isinf(-1E400))
       
   820         self.failIf(math.isinf(float("nan")))
       
   821         self.failIf(math.isinf(0.))
       
   822         self.failIf(math.isinf(1.))
       
   823 
       
   824     # RED_FLAG 16-Oct-2000 Tim
       
   825     # While 2.0 is more consistent about exceptions than previous releases, it
       
   826     # still fails this part of the test on some platforms.  For now, we only
       
   827     # *run* test_exceptions() in verbose mode, so that this isn't normally
       
   828     # tested.
       
   829 
       
   830     if verbose:
       
   831         def test_exceptions(self):
       
   832             try:
       
   833                 x = math.exp(-1000000000)
       
   834             except:
       
   835                 # mathmodule.c is failing to weed out underflows from libm, or
       
   836                 # we've got an fp format with huge dynamic range
       
   837                 self.fail("underflowing exp() should not have raised "
       
   838                           "an exception")
       
   839             if x != 0:
       
   840                 self.fail("underflowing exp() should have returned 0")
       
   841 
       
   842             # If this fails, probably using a strict IEEE-754 conforming libm, and x
       
   843             # is +Inf afterwards.  But Python wants overflows detected by default.
       
   844             try:
       
   845                 x = math.exp(1000000000)
       
   846             except OverflowError:
       
   847                 pass
       
   848             else:
       
   849                 self.fail("overflowing exp() didn't trigger OverflowError")
       
   850 
       
   851             # If this fails, it could be a puzzle.  One odd possibility is that
       
   852             # mathmodule.c's macros are getting confused while comparing
       
   853             # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
       
   854             # as a result (and so raising OverflowError instead).
       
   855             try:
       
   856                 x = math.sqrt(-1.0)
       
   857             except ValueError:
       
   858                 pass
       
   859             else:
       
   860                 self.fail("sqrt(-1) didn't raise ValueError")
       
   861 
       
   862     def test_testfile(self):
       
   863         if not float.__getformat__("double").startswith("IEEE"):
       
   864             return
       
   865         for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
       
   866             # Skip if either the input or result is complex, or if
       
   867             # flags is nonempty
       
   868             if ai != 0. or ei != 0. or flags:
       
   869                 continue
       
   870             if fn in ['rect', 'polar']:
       
   871                 # no real versions of rect, polar
       
   872                 continue
       
   873             func = getattr(math, fn)
       
   874             try:
       
   875                 result = func(ar)
       
   876             except ValueError:
       
   877                 message = ("Unexpected ValueError in " +
       
   878                            "test %s:%s(%r)\n" % (id, fn, ar))
       
   879                 self.fail(message)
       
   880             except OverflowError:
       
   881                 message = ("Unexpected OverflowError in " +
       
   882                            "test %s:%s(%r)\n" % (id, fn, ar))
       
   883                 self.fail(message)
       
   884             self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
       
   885 
       
   886 def test_main():
       
   887     from doctest import DocFileSuite
       
   888     suite = unittest.TestSuite()
       
   889     suite.addTest(unittest.makeSuite(MathTests))
       
   890     suite.addTest(DocFileSuite("ieee754.txt"))
       
   891     run_unittest(suite)
       
   892 
       
   893 if __name__ == '__main__':
       
   894     test_main()