Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 1 | # Python test set -- math module |
| 2 | # XXXX Should not do tests around zero only |
| 3 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4 | from test.test_support import run_unittest, verbose |
| 5 | import unittest |
| 6 | import math |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 7 | import os |
| 8 | import sys |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 9 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 10 | eps = 1E-05 |
| 11 | NAN = float('nan') |
| 12 | INF = float('inf') |
| 13 | NINF = float('-inf') |
| 14 | |
| 15 | # locate file with test values |
| 16 | if __name__ == '__main__': |
| 17 | file = sys.argv[0] |
| 18 | else: |
| 19 | file = __file__ |
| 20 | test_dir = os.path.dirname(file) or os.curdir |
| 21 | test_file = os.path.join(test_dir, 'cmath_testcases.txt') |
| 22 | |
| 23 | def parse_testfile(fname): |
| 24 | """Parse a file with test values |
| 25 | |
| 26 | Empty lines or lines starting with -- are ignored |
| 27 | yields id, fn, arg_real, arg_imag, exp_real, exp_imag |
| 28 | """ |
| 29 | with open(fname) as fp: |
| 30 | for line in fp: |
| 31 | # skip comment lines and blank lines |
| 32 | if line.startswith('--') or not line.strip(): |
| 33 | continue |
| 34 | |
| 35 | lhs, rhs = line.split('->') |
| 36 | id, fn, arg_real, arg_imag = lhs.split() |
| 37 | rhs_pieces = rhs.split() |
| 38 | exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1] |
| 39 | flags = rhs_pieces[2:] |
| 40 | |
| 41 | yield (id, fn, |
| 42 | float(arg_real), float(arg_imag), |
| 43 | float(exp_real), float(exp_imag), |
| 44 | flags |
| 45 | ) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 46 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 47 | class MathTests(unittest.TestCase): |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 48 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 49 | def ftest(self, name, value, expected): |
| 50 | if abs(value-expected) > eps: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 51 | # Use %r instead of %f so the error message |
| 52 | # displays full precision. Otherwise discrepancies |
| 53 | # in the last few bits will lead to very confusing |
| 54 | # error messages |
| 55 | self.fail('%s returned %r, expected %r' % |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | (name, value, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 58 | def testConstants(self): |
| 59 | self.ftest('pi', math.pi, 3.1415926) |
| 60 | self.ftest('e', math.e, 2.7182818) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 62 | def testAcos(self): |
| 63 | self.assertRaises(TypeError, math.acos) |
| 64 | self.ftest('acos(-1)', math.acos(-1), math.pi) |
| 65 | self.ftest('acos(0)', math.acos(0), math.pi/2) |
| 66 | self.ftest('acos(1)', math.acos(1), 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 67 | self.assertRaises(ValueError, math.acos, INF) |
| 68 | self.assertRaises(ValueError, math.acos, NINF) |
| 69 | self.assert_(math.isnan(math.acos(NAN))) |
| 70 | |
| 71 | def testAcosh(self): |
| 72 | self.assertRaises(TypeError, math.acosh) |
| 73 | self.ftest('acosh(1)', math.acosh(1), 0) |
| 74 | self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168) |
| 75 | self.assertRaises(ValueError, math.acosh, 0) |
| 76 | self.assertRaises(ValueError, math.acosh, -1) |
| 77 | self.assertEquals(math.acosh(INF), INF) |
| 78 | self.assertRaises(ValueError, math.acosh, NINF) |
| 79 | self.assert_(math.isnan(math.acosh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 80 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 81 | def testAsin(self): |
| 82 | self.assertRaises(TypeError, math.asin) |
| 83 | self.ftest('asin(-1)', math.asin(-1), -math.pi/2) |
| 84 | self.ftest('asin(0)', math.asin(0), 0) |
| 85 | self.ftest('asin(1)', math.asin(1), math.pi/2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 86 | self.assertRaises(ValueError, math.asin, INF) |
| 87 | self.assertRaises(ValueError, math.asin, NINF) |
| 88 | self.assert_(math.isnan(math.asin(NAN))) |
| 89 | |
| 90 | def testAsinh(self): |
| 91 | self.assertRaises(TypeError, math.asinh) |
| 92 | self.ftest('asinh(0)', math.asinh(0), 0) |
| 93 | self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305) |
| 94 | self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305) |
| 95 | self.assertEquals(math.asinh(INF), INF) |
| 96 | self.assertEquals(math.asinh(NINF), NINF) |
| 97 | self.assert_(math.isnan(math.asinh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 98 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 99 | def testAtan(self): |
| 100 | self.assertRaises(TypeError, math.atan) |
| 101 | self.ftest('atan(-1)', math.atan(-1), -math.pi/4) |
| 102 | self.ftest('atan(0)', math.atan(0), 0) |
| 103 | self.ftest('atan(1)', math.atan(1), math.pi/4) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 104 | self.ftest('atan(inf)', math.atan(INF), math.pi/2) |
| 105 | self.ftest('atan(-inf)', math.atan(-INF), -math.pi/2) |
| 106 | self.assert_(math.isnan(math.atan(NAN))) |
| 107 | |
| 108 | def testAtanh(self): |
| 109 | self.assertRaises(TypeError, math.atan) |
| 110 | self.ftest('atanh(0)', math.atanh(0), 0) |
| 111 | self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489) |
| 112 | self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489) |
| 113 | self.assertRaises(ValueError, math.atanh, 1) |
| 114 | self.assertRaises(ValueError, math.atanh, -1) |
| 115 | self.assertRaises(ValueError, math.atanh, INF) |
| 116 | self.assertRaises(ValueError, math.atanh, NINF) |
| 117 | self.assert_(math.isnan(math.atanh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 118 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | def testAtan2(self): |
| 120 | self.assertRaises(TypeError, math.atan2) |
| 121 | self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) |
| 122 | self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) |
| 123 | self.ftest('atan2(0, 1)', math.atan2(0, 1), 0) |
| 124 | self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4) |
| 125 | self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 126 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 127 | def testCeil(self): |
| 128 | self.assertRaises(TypeError, math.ceil) |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 129 | self.assertEquals(int, type(math.ceil(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | self.ftest('ceil(0.5)', math.ceil(0.5), 1) |
| 131 | self.ftest('ceil(1.0)', math.ceil(1.0), 1) |
| 132 | self.ftest('ceil(1.5)', math.ceil(1.5), 2) |
| 133 | self.ftest('ceil(-0.5)', math.ceil(-0.5), 0) |
| 134 | self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) |
| 135 | self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 136 | #self.assertEquals(math.ceil(INF), INF) |
| 137 | #self.assertEquals(math.ceil(NINF), NINF) |
| 138 | #self.assert_(math.isnan(math.ceil(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 140 | class TestCeil: |
| 141 | def __ceil__(self): |
| 142 | return 42 |
| 143 | class TestNoCeil: |
| 144 | pass |
| 145 | self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) |
| 146 | self.assertRaises(TypeError, math.ceil, TestNoCeil()) |
| 147 | |
| 148 | t = TestNoCeil() |
| 149 | t.__ceil__ = lambda *args: args |
| 150 | self.assertRaises(TypeError, math.ceil, t) |
| 151 | self.assertRaises(TypeError, math.ceil, t, 0) |
| 152 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 153 | if float.__getformat__("double").startswith("IEEE"): |
| 154 | def testCopysign(self): |
| 155 | self.assertRaises(TypeError, math.copysign) |
| 156 | # copysign should let us distinguish signs of zeros |
| 157 | self.assertEquals(copysign(1., 0.), 1.) |
| 158 | self.assertEquals(copysign(1., -0.), -1.) |
| 159 | self.assertEquals(copysign(INF, 0.), INF) |
| 160 | self.assertEquals(copysign(INF, -0.), NINF) |
| 161 | self.assertEquals(copysign(NINF, 0.), INF) |
| 162 | self.assertEquals(copysign(NINF, -0.), NINF) |
| 163 | # and of infinities |
| 164 | self.assertEquals(copysign(1., INF), 1.) |
| 165 | self.assertEquals(copysign(1., NINF), -1.) |
| 166 | self.assertEquals(copysign(INF, INF), INF) |
| 167 | self.assertEquals(copysign(INF, NINF), NINF) |
| 168 | self.assertEquals(copysign(NINF, INF), INF) |
| 169 | self.assertEquals(copysign(NINF, NINF), NINF) |
| 170 | self.assert_(math.isnan(copysign(NAN, 1.))) |
| 171 | self.assert_(math.isnan(copysign(NAN, INF))) |
| 172 | self.assert_(math.isnan(copysign(NAN, NINF))) |
| 173 | self.assert_(math.isnan(copysign(NAN, NAN))) |
| 174 | # copysign(INF, NAN) may be INF or it may be NINF, since |
| 175 | # we don't know whether the sign bit of NAN is set on any |
| 176 | # given platform. |
| 177 | self.assert_(math.isinf(copysign(INF, NAN))) |
| 178 | # similarly, copysign(2., NAN) could be 2. or -2. |
| 179 | self.assertEquals(abs(copysign(2., NAN)), 2.) |
| 180 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 181 | def testCos(self): |
| 182 | self.assertRaises(TypeError, math.cos) |
| 183 | self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0) |
| 184 | self.ftest('cos(0)', math.cos(0), 1) |
| 185 | self.ftest('cos(pi/2)', math.cos(math.pi/2), 0) |
| 186 | self.ftest('cos(pi)', math.cos(math.pi), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 187 | try: |
| 188 | self.assert_(math.isnan(math.cos(INF))) |
| 189 | self.assert_(math.isnan(math.cos(NINF))) |
| 190 | except ValueError: |
| 191 | self.assertRaises(ValueError, math.cos, INF) |
| 192 | self.assertRaises(ValueError, math.cos, NINF) |
| 193 | self.assert_(math.isnan(math.cos(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 194 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 195 | def testCosh(self): |
| 196 | self.assertRaises(TypeError, math.cosh) |
| 197 | self.ftest('cosh(0)', math.cosh(0), 1) |
| 198 | self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 199 | self.assertEquals(math.cosh(INF), INF) |
| 200 | self.assertEquals(math.cosh(NINF), INF) |
| 201 | self.assert_(math.isnan(math.cosh(NAN))) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 202 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 203 | def testDegrees(self): |
| 204 | self.assertRaises(TypeError, math.degrees) |
| 205 | self.ftest('degrees(pi)', math.degrees(math.pi), 180.0) |
| 206 | self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
| 207 | self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 208 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 209 | def testExp(self): |
| 210 | self.assertRaises(TypeError, math.exp) |
| 211 | self.ftest('exp(-1)', math.exp(-1), 1/math.e) |
| 212 | self.ftest('exp(0)', math.exp(0), 1) |
| 213 | self.ftest('exp(1)', math.exp(1), math.e) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 214 | self.assertEquals(math.exp(INF), INF) |
| 215 | self.assertEquals(math.exp(NINF), 0.) |
| 216 | self.assert_(math.isnan(math.exp(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 217 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 218 | def testFabs(self): |
| 219 | self.assertRaises(TypeError, math.fabs) |
| 220 | self.ftest('fabs(-1)', math.fabs(-1), 1) |
| 221 | self.ftest('fabs(0)', math.fabs(0), 0) |
| 222 | self.ftest('fabs(1)', math.fabs(1), 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 223 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 224 | def testFloor(self): |
| 225 | self.assertRaises(TypeError, math.floor) |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 226 | self.assertEquals(int, type(math.floor(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 227 | self.ftest('floor(0.5)', math.floor(0.5), 0) |
| 228 | self.ftest('floor(1.0)', math.floor(1.0), 1) |
| 229 | self.ftest('floor(1.5)', math.floor(1.5), 1) |
| 230 | self.ftest('floor(-0.5)', math.floor(-0.5), -1) |
| 231 | self.ftest('floor(-1.0)', math.floor(-1.0), -1) |
| 232 | self.ftest('floor(-1.5)', math.floor(-1.5), -2) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 233 | # pow() relies on floor() to check for integers |
| 234 | # This fails on some platforms - so check it here |
| 235 | self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) |
| 236 | self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 237 | #self.assertEquals(math.ceil(INF), INF) |
| 238 | #self.assertEquals(math.ceil(NINF), NINF) |
| 239 | #self.assert_(math.isnan(math.floor(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 241 | class TestFloor: |
| 242 | def __floor__(self): |
| 243 | return 42 |
| 244 | class TestNoFloor: |
| 245 | pass |
| 246 | self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42) |
| 247 | self.assertRaises(TypeError, math.floor, TestNoFloor()) |
| 248 | |
| 249 | t = TestNoFloor() |
| 250 | t.__floor__ = lambda *args: args |
| 251 | self.assertRaises(TypeError, math.floor, t) |
| 252 | self.assertRaises(TypeError, math.floor, t, 0) |
| 253 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 254 | def testFmod(self): |
| 255 | self.assertRaises(TypeError, math.fmod) |
| 256 | self.ftest('fmod(10,1)', math.fmod(10,1), 0) |
| 257 | self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0) |
| 258 | self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1) |
| 259 | self.ftest('fmod(-10,1)', math.fmod(-10,1), 0) |
| 260 | self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0) |
| 261 | self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 262 | self.assert_(math.isnan(math.fmod(NAN, 1.))) |
| 263 | self.assert_(math.isnan(math.fmod(1., NAN))) |
| 264 | self.assert_(math.isnan(math.fmod(NAN, NAN))) |
| 265 | self.assertRaises(ValueError, math.fmod, 1., 0.) |
| 266 | self.assertRaises(ValueError, math.fmod, INF, 1.) |
| 267 | self.assertRaises(ValueError, math.fmod, NINF, 1.) |
| 268 | self.assertRaises(ValueError, math.fmod, INF, 0.) |
| 269 | self.assertEquals(math.fmod(3.0, INF), 3.0) |
| 270 | self.assertEquals(math.fmod(-3.0, INF), -3.0) |
| 271 | self.assertEquals(math.fmod(3.0, NINF), 3.0) |
| 272 | self.assertEquals(math.fmod(-3.0, NINF), -3.0) |
| 273 | self.assertEquals(math.fmod(0.0, 3.0), 0.0) |
| 274 | self.assertEquals(math.fmod(0.0, NINF), 0.0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 275 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 276 | def testFrexp(self): |
| 277 | self.assertRaises(TypeError, math.frexp) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 279 | def testfrexp(name, result, expected): |
| 280 | (mant, exp), (emant, eexp) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 281 | if abs(mant-emant) > eps or exp != eexp: |
| 282 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 283 | (name, result, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 284 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 285 | testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) |
| 286 | testfrexp('frexp(0)', math.frexp(0), (0, 0)) |
| 287 | testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) |
| 288 | testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 289 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 290 | self.assertEquals(math.frexp(INF)[0], INF) |
| 291 | self.assertEquals(math.frexp(NINF)[0], NINF) |
| 292 | self.assert_(math.isnan(math.frexp(NAN)[0])) |
| 293 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 294 | def testHypot(self): |
| 295 | self.assertRaises(TypeError, math.hypot) |
| 296 | self.ftest('hypot(0,0)', math.hypot(0,0), 0) |
| 297 | self.ftest('hypot(3,4)', math.hypot(3,4), 5) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 298 | self.assertEqual(math.hypot(NAN, INF), INF) |
| 299 | self.assertEqual(math.hypot(INF, NAN), INF) |
| 300 | self.assertEqual(math.hypot(NAN, NINF), INF) |
| 301 | self.assertEqual(math.hypot(NINF, NAN), INF) |
| 302 | self.assert_(math.isnan(math.hypot(1.0, NAN))) |
| 303 | self.assert_(math.isnan(math.hypot(NAN, -2.0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 304 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 305 | def testLdexp(self): |
| 306 | self.assertRaises(TypeError, math.ldexp) |
| 307 | self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) |
| 308 | self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) |
| 309 | self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) |
| 310 | self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 311 | self.assertRaises(OverflowError, math.ldexp, 1., 1000000) |
| 312 | self.assertRaises(OverflowError, math.ldexp, -1., 1000000) |
| 313 | self.assertEquals(math.ldexp(1., -1000000), 0.) |
| 314 | self.assertEquals(math.ldexp(-1., -1000000), -0.) |
| 315 | self.assertEquals(math.ldexp(INF, 30), INF) |
| 316 | self.assertEquals(math.ldexp(NINF, -213), NINF) |
| 317 | self.assert_(math.isnan(math.ldexp(NAN, 0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 318 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 319 | def testLog(self): |
| 320 | self.assertRaises(TypeError, math.log) |
| 321 | self.ftest('log(1/e)', math.log(1/math.e), -1) |
| 322 | self.ftest('log(1)', math.log(1), 0) |
| 323 | self.ftest('log(e)', math.log(math.e), 1) |
| 324 | self.ftest('log(32,2)', math.log(32,2), 5) |
| 325 | self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) |
| 326 | self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 327 | self.assertEquals(math.log(INF), INF) |
| 328 | self.assertRaises(ValueError, math.log, NINF) |
| 329 | self.assert_(math.isnan(math.log(NAN))) |
| 330 | |
| 331 | def testLog1p(self): |
| 332 | self.assertRaises(TypeError, math.log1p) |
| 333 | self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1) |
| 334 | self.ftest('log1p(0)', math.log1p(0), 0) |
| 335 | self.ftest('log1p(e-1)', math.log1p(math.e-1), 1) |
| 336 | self.ftest('log1p(1)', math.log1p(1), math.log(2)) |
| 337 | self.assertEquals(math.log1p(INF), INF) |
| 338 | self.assertRaises(ValueError, math.log1p, NINF) |
| 339 | self.assert_(math.isnan(math.log1p(NAN))) |
| 340 | n= 2**90 |
| 341 | self.assertAlmostEquals(math.log1p(n), 62.383246250395075) |
| 342 | self.assertAlmostEquals(math.log1p(n), math.log1p(float(n))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 343 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 344 | def testLog10(self): |
| 345 | self.assertRaises(TypeError, math.log10) |
| 346 | self.ftest('log10(0.1)', math.log10(0.1), -1) |
| 347 | self.ftest('log10(1)', math.log10(1), 0) |
| 348 | self.ftest('log10(10)', math.log10(10), 1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 349 | self.assertEquals(math.log(INF), INF) |
| 350 | self.assertRaises(ValueError, math.log10, NINF) |
| 351 | self.assert_(math.isnan(math.log10(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 352 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 353 | def testModf(self): |
| 354 | self.assertRaises(TypeError, math.modf) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 356 | def testmodf(name, result, expected): |
| 357 | (v1, v2), (e1, e2) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 358 | if abs(v1-e1) > eps or abs(v2-e2): |
| 359 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 360 | (name, result, expected)) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 361 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 362 | testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) |
| 363 | testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 364 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 365 | self.assertEquals(math.modf(INF), (0.0, INF)) |
| 366 | self.assertEquals(math.modf(NINF), (-0.0, NINF)) |
| 367 | |
| 368 | modf_nan = math.modf(NAN) |
| 369 | self.assert_(math.isnan(modf_nan[0])) |
| 370 | self.assert_(math.isnan(modf_nan[1])) |
| 371 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 372 | def testPow(self): |
| 373 | self.assertRaises(TypeError, math.pow) |
| 374 | self.ftest('pow(0,1)', math.pow(0,1), 0) |
| 375 | self.ftest('pow(1,0)', math.pow(1,0), 1) |
| 376 | self.ftest('pow(2,1)', math.pow(2,1), 2) |
| 377 | self.ftest('pow(2,-1)', math.pow(2,-1), 0.5) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 378 | self.assertEqual(math.pow(INF, 1), INF) |
| 379 | self.assertEqual(math.pow(NINF, 1), NINF) |
| 380 | self.assertEqual((math.pow(1, INF)), 1.) |
| 381 | self.assertEqual((math.pow(1, NINF)), 1.) |
| 382 | self.assert_(math.isnan(math.pow(NAN, 1))) |
| 383 | self.assert_(math.isnan(math.pow(2, NAN))) |
| 384 | self.assert_(math.isnan(math.pow(0, NAN))) |
| 385 | self.assertEqual(math.pow(1, NAN), 1) |
| 386 | self.assertEqual(1**NAN, 1) |
| 387 | self.assertEqual(1**INF, 1) |
| 388 | self.assertEqual(1**NINF, 1) |
| 389 | self.assertEqual(1**0, 1) |
| 390 | self.assertEqual(1.**NAN, 1) |
| 391 | self.assertEqual(1.**INF, 1) |
| 392 | self.assertEqual(1.**NINF, 1) |
| 393 | self.assertEqual(1.**0, 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 394 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 395 | def testRadians(self): |
| 396 | self.assertRaises(TypeError, math.radians) |
| 397 | self.ftest('radians(180)', math.radians(180), math.pi) |
| 398 | self.ftest('radians(90)', math.radians(90), math.pi/2) |
| 399 | self.ftest('radians(-45)', math.radians(-45), -math.pi/4) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 400 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 401 | def testSin(self): |
| 402 | self.assertRaises(TypeError, math.sin) |
| 403 | self.ftest('sin(0)', math.sin(0), 0) |
| 404 | self.ftest('sin(pi/2)', math.sin(math.pi/2), 1) |
| 405 | self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 406 | try: |
| 407 | self.assert_(math.isnan(math.sin(INF))) |
| 408 | self.assert_(math.isnan(math.sin(NINF))) |
| 409 | except ValueError: |
| 410 | self.assertRaises(ValueError, math.sin, INF) |
| 411 | self.assertRaises(ValueError, math.sin, NINF) |
| 412 | self.assert_(math.isnan(math.sin(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 413 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 414 | def testSinh(self): |
| 415 | self.assertRaises(TypeError, math.sinh) |
| 416 | self.ftest('sinh(0)', math.sinh(0), 0) |
| 417 | self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) |
| 418 | self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 419 | self.assertEquals(math.sinh(INF), INF) |
| 420 | self.assertEquals(math.sinh(-INF), -INF) |
| 421 | self.assert_(math.isnan(math.sinh(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 422 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 423 | def testSqrt(self): |
| 424 | self.assertRaises(TypeError, math.sqrt) |
| 425 | self.ftest('sqrt(0)', math.sqrt(0), 0) |
| 426 | self.ftest('sqrt(1)', math.sqrt(1), 1) |
| 427 | self.ftest('sqrt(4)', math.sqrt(4), 2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 428 | self.assertEquals(math.sqrt(INF), INF) |
| 429 | self.assertRaises(ValueError, math.sqrt, NINF) |
| 430 | self.assert_(math.isnan(math.sqrt(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 431 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 432 | def testTan(self): |
| 433 | self.assertRaises(TypeError, math.tan) |
| 434 | self.ftest('tan(0)', math.tan(0), 0) |
| 435 | self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) |
| 436 | self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 437 | try: |
| 438 | self.assert_(math.isnan(math.tan(INF))) |
| 439 | self.assert_(math.isnan(math.tan(NINF))) |
| 440 | except: |
| 441 | self.assertRaises(ValueError, math.tan, INF) |
| 442 | self.assertRaises(ValueError, math.tan, NINF) |
| 443 | self.assert_(math.isnan(math.tan(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 444 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 445 | def testTanh(self): |
| 446 | self.assertRaises(TypeError, math.tanh) |
| 447 | self.ftest('tanh(0)', math.tanh(0), 0) |
| 448 | self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 449 | self.ftest('tanh(inf)', math.tanh(INF), 1) |
| 450 | self.ftest('tanh(-inf)', math.tanh(NINF), -1) |
| 451 | self.assert_(math.isnan(math.tanh(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 452 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 453 | def test_trunc(self): |
| 454 | self.assertEqual(math.trunc(1), 1) |
| 455 | self.assertEqual(math.trunc(-1), -1) |
| 456 | self.assertEqual(type(math.trunc(1)), int) |
| 457 | self.assertEqual(type(math.trunc(1.5)), int) |
| 458 | self.assertEqual(math.trunc(1.5), 1) |
| 459 | self.assertEqual(math.trunc(-1.5), -1) |
| 460 | self.assertEqual(math.trunc(1.999999), 1) |
| 461 | self.assertEqual(math.trunc(-1.999999), -1) |
| 462 | self.assertEqual(math.trunc(-0.999999), -0) |
| 463 | self.assertEqual(math.trunc(-100.999), -100) |
| 464 | |
| 465 | class TestTrunc(object): |
| 466 | def __trunc__(self): |
| 467 | return 23 |
| 468 | |
| 469 | class TestNoTrunc(object): |
| 470 | pass |
| 471 | |
| 472 | self.assertEqual(math.trunc(TestTrunc()), 23) |
| 473 | |
| 474 | self.assertRaises(TypeError, math.trunc) |
| 475 | self.assertRaises(TypeError, math.trunc, 1, 2) |
| 476 | self.assertRaises(TypeError, math.trunc, TestNoTrunc()) |
| 477 | |
| 478 | # XXX Doesn't work because the method is looked up on |
| 479 | # the type only. |
| 480 | #t = TestNoTrunc() |
| 481 | #t.__trunc__ = lambda *args: args |
| 482 | #self.assertEquals((), math.trunc(t)) |
| 483 | #self.assertRaises(TypeError, math.trunc, t, 0) |
| 484 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 485 | def testCopysign(self): |
| 486 | self.assertEqual(math.copysign(1, 42), 1.0) |
| 487 | self.assertEqual(math.copysign(0., 42), 0.0) |
| 488 | self.assertEqual(math.copysign(1., -42), -1.0) |
| 489 | self.assertEqual(math.copysign(3, 0.), 3.0) |
| 490 | self.assertEqual(math.copysign(4., -0.), -4.0) |
| 491 | |
| 492 | def testIsnan(self): |
| 493 | self.assert_(math.isnan(float("nan"))) |
| 494 | self.assert_(math.isnan(float("inf")* 0.)) |
| 495 | self.failIf(math.isnan(float("inf"))) |
| 496 | self.failIf(math.isnan(0.)) |
| 497 | self.failIf(math.isnan(1.)) |
| 498 | |
| 499 | def testIsinf(self): |
| 500 | self.assert_(math.isinf(float("inf"))) |
| 501 | self.assert_(math.isinf(float("-inf"))) |
| 502 | self.assert_(math.isinf(1E400)) |
| 503 | self.assert_(math.isinf(-1E400)) |
| 504 | self.failIf(math.isinf(float("nan"))) |
| 505 | self.failIf(math.isinf(0.)) |
| 506 | self.failIf(math.isinf(1.)) |
| 507 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 508 | # RED_FLAG 16-Oct-2000 Tim |
| 509 | # While 2.0 is more consistent about exceptions than previous releases, it |
| 510 | # still fails this part of the test on some platforms. For now, we only |
| 511 | # *run* test_exceptions() in verbose mode, so that this isn't normally |
| 512 | # tested. |
Tim Peters | 98c8184 | 2000-10-16 17:35:13 +0000 | [diff] [blame] | 513 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 514 | if verbose: |
| 515 | def test_exceptions(self): |
| 516 | try: |
| 517 | x = math.exp(-1000000000) |
| 518 | except: |
| 519 | # mathmodule.c is failing to weed out underflows from libm, or |
| 520 | # we've got an fp format with huge dynamic range |
| 521 | self.fail("underflowing exp() should not have raised " |
| 522 | "an exception") |
| 523 | if x != 0: |
| 524 | self.fail("underflowing exp() should have returned 0") |
| 525 | |
| 526 | # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 527 | # is +Inf afterwards. But Python wants overflows detected by default. |
| 528 | try: |
| 529 | x = math.exp(1000000000) |
| 530 | except OverflowError: |
| 531 | pass |
| 532 | else: |
| 533 | self.fail("overflowing exp() didn't trigger OverflowError") |
| 534 | |
| 535 | # If this fails, it could be a puzzle. One odd possibility is that |
| 536 | # mathmodule.c's macros are getting confused while comparing |
| 537 | # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 538 | # as a result (and so raising OverflowError instead). |
| 539 | try: |
| 540 | x = math.sqrt(-1.0) |
| 541 | except ValueError: |
| 542 | pass |
| 543 | else: |
| 544 | self.fail("sqrt(-1) didn't raise ValueError") |
| 545 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 546 | def test_testfile(self): |
| 547 | if not float.__getformat__("double").startswith("IEEE"): |
| 548 | return |
| 549 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
| 550 | # Skip if either the input or result is complex, or if |
| 551 | # flags is nonempty |
| 552 | if ai != 0. or ei != 0. or flags: |
| 553 | continue |
| 554 | if fn in ['rect', 'polar']: |
| 555 | # no real versions of rect, polar |
| 556 | continue |
| 557 | func = getattr(math, fn) |
| 558 | result = func(ar) |
| 559 | self.ftest("%s:%s(%r)" % (id, fn, ar), result, er) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 560 | |
| 561 | def test_main(): |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame^] | 562 | from doctest import DocFileSuite |
| 563 | suite = unittest.TestSuite() |
| 564 | suite.addTest(unittest.makeSuite(MathTests)) |
| 565 | suite.addTest(DocFileSuite("ieee754.txt")) |
| 566 | run_unittest(suite) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 567 | |
| 568 | if __name__ == '__main__': |
| 569 | test_main() |