Changeset 391 for python/trunk/Lib/test/test_math.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_math.py
r2 r391 8 8 import sys 9 9 import random 10 import struct 10 11 11 12 eps = 1E-05 … … 13 14 INF = float('inf') 14 15 NINF = float('-inf') 16 17 # decorator for skipping tests on non-IEEE 754 platforms 18 requires_IEEE_754 = unittest.skipUnless( 19 float.__getformat__("double").startswith("IEEE"), 20 "test requires IEEE 754 doubles") 21 22 # detect evidence of double-rounding: fsum is not always correctly 23 # rounded on machines that suffer from double rounding. 24 x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer 25 HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4) 15 26 16 27 # locate file with test values … … 20 31 file = __file__ 21 32 test_dir = os.path.dirname(file) or os.curdir 33 math_testcases = os.path.join(test_dir, 'math_testcases.txt') 22 34 test_file = os.path.join(test_dir, 'cmath_testcases.txt') 35 36 def to_ulps(x): 37 """Convert a non-NaN float x to an integer, in such a way that 38 adjacent floats are converted to adjacent integers. Then 39 abs(ulps(x) - ulps(y)) gives the difference in ulps between two 40 floats. 41 42 The results from this function will only make sense on platforms 43 where C doubles are represented in IEEE 754 binary64 format. 44 45 """ 46 n = struct.unpack('<q', struct.pack('<d', x))[0] 47 if n < 0: 48 n = ~(n+2**63) 49 return n 50 51 def ulps_check(expected, got, ulps=20): 52 """Given non-NaN floats `expected` and `got`, 53 check that they're equal to within the given number of ulps. 54 55 Returns None on success and an error message on failure.""" 56 57 ulps_error = to_ulps(got) - to_ulps(expected) 58 if abs(ulps_error) <= ulps: 59 return None 60 return "error = {} ulps; permitted error = {} ulps".format(ulps_error, 61 ulps) 62 63 def acc_check(expected, got, rel_err=2e-15, abs_err = 5e-323): 64 """Determine whether non-NaN floats a and b are equal to within a 65 (small) rounding error. The default values for rel_err and 66 abs_err are chosen to be suitable for platforms where a float is 67 represented by an IEEE 754 double. They allow an error of between 68 9 and 19 ulps.""" 69 70 # need to special case infinities, since inf - inf gives nan 71 if math.isinf(expected) and got == expected: 72 return None 73 74 error = got - expected 75 76 permitted_error = max(abs_err, rel_err * abs(expected)) 77 if abs(error) < permitted_error: 78 return None 79 return "error = {}; permitted error = {}".format(error, 80 permitted_error) 81 82 def parse_mtestfile(fname): 83 """Parse a file with test values 84 85 -- starts a comment 86 blank lines, or lines containing only a comment, are ignored 87 other lines are expected to have the form 88 id fn arg -> expected [flag]* 89 90 """ 91 with open(fname) as fp: 92 for line in fp: 93 # strip comments, and skip blank lines 94 if '--' in line: 95 line = line[:line.index('--')] 96 if not line.strip(): 97 continue 98 99 lhs, rhs = line.split('->') 100 id, fn, arg = lhs.split() 101 rhs_pieces = rhs.split() 102 exp = rhs_pieces[0] 103 flags = rhs_pieces[1:] 104 105 yield (id, fn, float(arg), float(exp), flags) 23 106 24 107 def parse_testfile(fname): … … 68 151 self.assertRaises(ValueError, math.acos, INF) 69 152 self.assertRaises(ValueError, math.acos, NINF) 70 self.assert _(math.isnan(math.acos(NAN)))153 self.assertTrue(math.isnan(math.acos(NAN))) 71 154 72 155 def testAcosh(self): … … 76 159 self.assertRaises(ValueError, math.acosh, 0) 77 160 self.assertRaises(ValueError, math.acosh, -1) 78 self.assertEqual s(math.acosh(INF), INF)161 self.assertEqual(math.acosh(INF), INF) 79 162 self.assertRaises(ValueError, math.acosh, NINF) 80 self.assert _(math.isnan(math.acosh(NAN)))163 self.assertTrue(math.isnan(math.acosh(NAN))) 81 164 82 165 def testAsin(self): … … 87 170 self.assertRaises(ValueError, math.asin, INF) 88 171 self.assertRaises(ValueError, math.asin, NINF) 89 self.assert _(math.isnan(math.asin(NAN)))172 self.assertTrue(math.isnan(math.asin(NAN))) 90 173 91 174 def testAsinh(self): … … 94 177 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305) 95 178 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305) 96 self.assertEqual s(math.asinh(INF), INF)97 self.assertEqual s(math.asinh(NINF), NINF)98 self.assert _(math.isnan(math.asinh(NAN)))179 self.assertEqual(math.asinh(INF), INF) 180 self.assertEqual(math.asinh(NINF), NINF) 181 self.assertTrue(math.isnan(math.asinh(NAN))) 99 182 100 183 def testAtan(self): … … 105 188 self.ftest('atan(inf)', math.atan(INF), math.pi/2) 106 189 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2) 107 self.assert _(math.isnan(math.atan(NAN)))190 self.assertTrue(math.isnan(math.atan(NAN))) 108 191 109 192 def testAtanh(self): … … 116 199 self.assertRaises(ValueError, math.atanh, INF) 117 200 self.assertRaises(ValueError, math.atanh, NINF) 118 self.assert _(math.isnan(math.atanh(NAN)))201 self.assertTrue(math.isnan(math.atanh(NAN))) 119 202 120 203 def testAtan2(self): … … 133 216 self.assertEqual(math.atan2(0., 2.3), 0.) 134 217 self.assertEqual(math.atan2(0., INF), 0.) 135 self.assert _(math.isnan(math.atan2(0., NAN)))218 self.assertTrue(math.isnan(math.atan2(0., NAN))) 136 219 # math.atan2(-0, x) 137 220 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi) … … 141 224 self.assertEqual(math.atan2(-0., 2.3), -0.) 142 225 self.assertEqual(math.atan2(-0., INF), -0.) 143 self.assert _(math.isnan(math.atan2(-0., NAN)))226 self.assertTrue(math.isnan(math.atan2(-0., NAN))) 144 227 # math.atan2(INF, x) 145 228 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4) … … 149 232 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2) 150 233 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4) 151 self.assert _(math.isnan(math.atan2(INF, NAN)))234 self.assertTrue(math.isnan(math.atan2(INF, NAN))) 152 235 # math.atan2(NINF, x) 153 236 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4) … … 157 240 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2) 158 241 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4) 159 self.assert _(math.isnan(math.atan2(NINF, NAN)))242 self.assertTrue(math.isnan(math.atan2(NINF, NAN))) 160 243 # math.atan2(+finite, x) 161 244 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi) … … 163 246 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2) 164 247 self.assertEqual(math.atan2(2.3, INF), 0.) 165 self.assert _(math.isnan(math.atan2(2.3, NAN)))248 self.assertTrue(math.isnan(math.atan2(2.3, NAN))) 166 249 # math.atan2(-finite, x) 167 250 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi) … … 169 252 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2) 170 253 self.assertEqual(math.atan2(-2.3, INF), -0.) 171 self.assert _(math.isnan(math.atan2(-2.3, NAN)))254 self.assertTrue(math.isnan(math.atan2(-2.3, NAN))) 172 255 # 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)))256 self.assertTrue(math.isnan(math.atan2(NAN, NINF))) 257 self.assertTrue(math.isnan(math.atan2(NAN, -2.3))) 258 self.assertTrue(math.isnan(math.atan2(NAN, -0.))) 259 self.assertTrue(math.isnan(math.atan2(NAN, 0.))) 260 self.assertTrue(math.isnan(math.atan2(NAN, 2.3))) 261 self.assertTrue(math.isnan(math.atan2(NAN, INF))) 262 self.assertTrue(math.isnan(math.atan2(NAN, NAN))) 180 263 181 264 def testCeil(self): 182 265 self.assertRaises(TypeError, math.ceil) 183 266 # These types will be int in py3k. 184 self.assertEqual s(float, type(math.ceil(1)))185 self.assertEqual s(float, type(math.ceil(1L)))186 self.assertEqual s(float, type(math.ceil(1.0)))267 self.assertEqual(float, type(math.ceil(1))) 268 self.assertEqual(float, type(math.ceil(1L))) 269 self.assertEqual(float, type(math.ceil(1.0))) 187 270 self.ftest('ceil(0.5)', math.ceil(0.5), 1) 188 271 self.ftest('ceil(1.0)', math.ceil(1.0), 1) … … 191 274 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) 192 275 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) 193 self.assertEqual s(math.ceil(INF), INF)194 self.assertEqual s(math.ceil(NINF), NINF)195 self.assert _(math.isnan(math.ceil(NAN)))276 self.assertEqual(math.ceil(INF), INF) 277 self.assertEqual(math.ceil(NINF), NINF) 278 self.assertTrue(math.isnan(math.ceil(NAN))) 196 279 197 280 class TestCeil(object): … … 208 291 self.assertRaises(TypeError, math.ceil, t, 0) 209 292 210 if float.__getformat__("double").startswith("IEEE"):211 212 213 214 215 216 217 218 219 220 self.assertEquals(math.copysign(1., 0.), 1.)221 self.assertEquals(math.copysign(1., -0.), -1.)222 self.assertEquals(math.copysign(INF, 0.), INF)223 self.assertEquals(math.copysign(INF, -0.), NINF)224 self.assertEquals(math.copysign(NINF, 0.), INF)225 self.assertEquals(math.copysign(NINF, -0.), NINF)226 227 self.assertEquals(math.copysign(1., INF), 1.)228 self.assertEquals(math.copysign(1., NINF), -1.)229 self.assertEquals(math.copysign(INF, INF), INF)230 self.assertEquals(math.copysign(INF, NINF), NINF)231 self.assertEquals(math.copysign(NINF, INF), INF)232 self.assertEquals(math.copysign(NINF, NINF), NINF)233 234 235 236 237 238 239 240 241 242 self.assertEquals(abs(math.copysign(2., NAN)), 2.)293 @requires_IEEE_754 294 def testCopysign(self): 295 self.assertEqual(math.copysign(1, 42), 1.0) 296 self.assertEqual(math.copysign(0., 42), 0.0) 297 self.assertEqual(math.copysign(1., -42), -1.0) 298 self.assertEqual(math.copysign(3, 0.), 3.0) 299 self.assertEqual(math.copysign(4., -0.), -4.0) 300 301 self.assertRaises(TypeError, math.copysign) 302 # copysign should let us distinguish signs of zeros 303 self.assertEqual(math.copysign(1., 0.), 1.) 304 self.assertEqual(math.copysign(1., -0.), -1.) 305 self.assertEqual(math.copysign(INF, 0.), INF) 306 self.assertEqual(math.copysign(INF, -0.), NINF) 307 self.assertEqual(math.copysign(NINF, 0.), INF) 308 self.assertEqual(math.copysign(NINF, -0.), NINF) 309 # and of infinities 310 self.assertEqual(math.copysign(1., INF), 1.) 311 self.assertEqual(math.copysign(1., NINF), -1.) 312 self.assertEqual(math.copysign(INF, INF), INF) 313 self.assertEqual(math.copysign(INF, NINF), NINF) 314 self.assertEqual(math.copysign(NINF, INF), INF) 315 self.assertEqual(math.copysign(NINF, NINF), NINF) 316 self.assertTrue(math.isnan(math.copysign(NAN, 1.))) 317 self.assertTrue(math.isnan(math.copysign(NAN, INF))) 318 self.assertTrue(math.isnan(math.copysign(NAN, NINF))) 319 self.assertTrue(math.isnan(math.copysign(NAN, NAN))) 320 # copysign(INF, NAN) may be INF or it may be NINF, since 321 # we don't know whether the sign bit of NAN is set on any 322 # given platform. 323 self.assertTrue(math.isinf(math.copysign(INF, NAN))) 324 # similarly, copysign(2., NAN) could be 2. or -2. 325 self.assertEqual(abs(math.copysign(2., NAN)), 2.) 243 326 244 327 def testCos(self): … … 249 332 self.ftest('cos(pi)', math.cos(math.pi), -1) 250 333 try: 251 self.assert _(math.isnan(math.cos(INF)))252 self.assert _(math.isnan(math.cos(NINF)))334 self.assertTrue(math.isnan(math.cos(INF))) 335 self.assertTrue(math.isnan(math.cos(NINF))) 253 336 except ValueError: 254 337 self.assertRaises(ValueError, math.cos, INF) 255 338 self.assertRaises(ValueError, math.cos, NINF) 256 self.assert _(math.isnan(math.cos(NAN)))339 self.assertTrue(math.isnan(math.cos(NAN))) 257 340 258 341 def testCosh(self): … … 260 343 self.ftest('cosh(0)', math.cosh(0), 1) 261 344 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert 262 self.assertEqual s(math.cosh(INF), INF)263 self.assertEqual s(math.cosh(NINF), INF)264 self.assert _(math.isnan(math.cosh(NAN)))345 self.assertEqual(math.cosh(INF), INF) 346 self.assertEqual(math.cosh(NINF), INF) 347 self.assertTrue(math.isnan(math.cosh(NAN))) 265 348 266 349 def testDegrees(self): … … 275 358 self.ftest('exp(0)', math.exp(0), 1) 276 359 self.ftest('exp(1)', math.exp(1), math.e) 277 self.assertEqual s(math.exp(INF), INF)278 self.assertEqual s(math.exp(NINF), 0.)279 self.assert _(math.isnan(math.exp(NAN)))360 self.assertEqual(math.exp(INF), INF) 361 self.assertEqual(math.exp(NINF), 0.) 362 self.assertTrue(math.isnan(math.exp(NAN))) 280 363 281 364 def testFabs(self): … … 293 376 values = range(10) + [50, 100, 500] 294 377 random.shuffle(values) 295 for x in range(10):378 for x in values: 296 379 for cast in (int, long, float): 297 380 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x))) … … 302 385 self.assertRaises(TypeError, math.floor) 303 386 # These types will be int in py3k. 304 self.assertEqual s(float, type(math.floor(1)))305 self.assertEqual s(float, type(math.floor(1L)))306 self.assertEqual s(float, type(math.floor(1.0)))387 self.assertEqual(float, type(math.floor(1))) 388 self.assertEqual(float, type(math.floor(1L))) 389 self.assertEqual(float, type(math.floor(1.0))) 307 390 self.ftest('floor(0.5)', math.floor(0.5), 0) 308 391 self.ftest('floor(1.0)', math.floor(1.0), 1) … … 315 398 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) 316 399 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) 317 self.assertEqual s(math.ceil(INF), INF)318 self.assertEqual s(math.ceil(NINF), NINF)319 self.assert _(math.isnan(math.floor(NAN)))400 self.assertEqual(math.ceil(INF), INF) 401 self.assertEqual(math.ceil(NINF), NINF) 402 self.assertTrue(math.isnan(math.floor(NAN))) 320 403 321 404 class TestFloor(object): … … 340 423 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0) 341 424 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1) 342 self.assert _(math.isnan(math.fmod(NAN, 1.)))343 self.assert _(math.isnan(math.fmod(1., NAN)))344 self.assert _(math.isnan(math.fmod(NAN, NAN)))425 self.assertTrue(math.isnan(math.fmod(NAN, 1.))) 426 self.assertTrue(math.isnan(math.fmod(1., NAN))) 427 self.assertTrue(math.isnan(math.fmod(NAN, NAN))) 345 428 self.assertRaises(ValueError, math.fmod, 1., 0.) 346 429 self.assertRaises(ValueError, math.fmod, INF, 1.) 347 430 self.assertRaises(ValueError, math.fmod, NINF, 1.) 348 431 self.assertRaises(ValueError, math.fmod, INF, 0.) 349 self.assertEqual s(math.fmod(3.0, INF), 3.0)350 self.assertEqual s(math.fmod(-3.0, INF), -3.0)351 self.assertEqual s(math.fmod(3.0, NINF), 3.0)352 self.assertEqual s(math.fmod(-3.0, NINF), -3.0)353 self.assertEqual s(math.fmod(0.0, 3.0), 0.0)354 self.assertEqual s(math.fmod(0.0, NINF), 0.0)432 self.assertEqual(math.fmod(3.0, INF), 3.0) 433 self.assertEqual(math.fmod(-3.0, INF), -3.0) 434 self.assertEqual(math.fmod(3.0, NINF), 3.0) 435 self.assertEqual(math.fmod(-3.0, NINF), -3.0) 436 self.assertEqual(math.fmod(0.0, 3.0), 0.0) 437 self.assertEqual(math.fmod(0.0, NINF), 0.0) 355 438 356 439 def testFrexp(self): … … 368 451 testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) 369 452 370 self.assertEquals(math.frexp(INF)[0], INF) 371 self.assertEquals(math.frexp(NINF)[0], NINF) 372 self.assert_(math.isnan(math.frexp(NAN)[0])) 373 453 self.assertEqual(math.frexp(INF)[0], INF) 454 self.assertEqual(math.frexp(NINF)[0], NINF) 455 self.assertTrue(math.isnan(math.frexp(NAN)[0])) 456 457 @requires_IEEE_754 458 @unittest.skipIf(HAVE_DOUBLE_ROUNDING, 459 "fsum is not exact on machines with double rounding") 374 460 def testFsum(self): 375 461 # math.fsum relies on exact rounding for correct operation. … … 380 466 # problem described in issue #2937, we simply skip the whole 381 467 # test. 382 383 if not float.__getformat__("double").startswith("IEEE"):384 return385 386 # on IEEE 754 compliant machines, both of the expressions387 # below should round to 10000000000000002.0.388 if 1e16+2.0 != 1e16+2.9999:389 return390 468 391 469 # Python version of math.fsum, for comparison. Uses a … … 476 554 self.assertEqual(math.hypot(NAN, NINF), INF) 477 555 self.assertEqual(math.hypot(NINF, NAN), INF) 478 self.assert _(math.isnan(math.hypot(1.0, NAN)))479 self.assert _(math.isnan(math.hypot(NAN, -2.0)))556 self.assertTrue(math.isnan(math.hypot(1.0, NAN))) 557 self.assertTrue(math.isnan(math.hypot(NAN, -2.0))) 480 558 481 559 def testLdexp(self): … … 487 565 self.assertRaises(OverflowError, math.ldexp, 1., 1000000) 488 566 self.assertRaises(OverflowError, math.ldexp, -1., 1000000) 489 self.assertEqual s(math.ldexp(1., -1000000), 0.)490 self.assertEqual s(math.ldexp(-1., -1000000), -0.)491 self.assertEqual s(math.ldexp(INF, 30), INF)492 self.assertEqual s(math.ldexp(NINF, -213), NINF)493 self.assert _(math.isnan(math.ldexp(NAN, 0)))567 self.assertEqual(math.ldexp(1., -1000000), 0.) 568 self.assertEqual(math.ldexp(-1., -1000000), -0.) 569 self.assertEqual(math.ldexp(INF, 30), INF) 570 self.assertEqual(math.ldexp(NINF, -213), NINF) 571 self.assertTrue(math.isnan(math.ldexp(NAN, 0))) 494 572 495 573 # large second argument 496 574 for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]: 497 self.assertEqual s(math.ldexp(INF, -n), INF)498 self.assertEqual s(math.ldexp(NINF, -n), NINF)499 self.assertEqual s(math.ldexp(1., -n), 0.)500 self.assertEqual s(math.ldexp(-1., -n), -0.)501 self.assertEqual s(math.ldexp(0., -n), 0.)502 self.assertEqual s(math.ldexp(-0., -n), -0.)503 self.assert _(math.isnan(math.ldexp(NAN, -n)))575 self.assertEqual(math.ldexp(INF, -n), INF) 576 self.assertEqual(math.ldexp(NINF, -n), NINF) 577 self.assertEqual(math.ldexp(1., -n), 0.) 578 self.assertEqual(math.ldexp(-1., -n), -0.) 579 self.assertEqual(math.ldexp(0., -n), 0.) 580 self.assertEqual(math.ldexp(-0., -n), -0.) 581 self.assertTrue(math.isnan(math.ldexp(NAN, -n))) 504 582 505 583 self.assertRaises(OverflowError, math.ldexp, 1., n) 506 584 self.assertRaises(OverflowError, math.ldexp, -1., n) 507 self.assertEqual s(math.ldexp(0., n), 0.)508 self.assertEqual s(math.ldexp(-0., n), -0.)509 self.assertEqual s(math.ldexp(INF, n), INF)510 self.assertEqual s(math.ldexp(NINF, n), NINF)511 self.assert _(math.isnan(math.ldexp(NAN, n)))585 self.assertEqual(math.ldexp(0., n), 0.) 586 self.assertEqual(math.ldexp(-0., n), -0.) 587 self.assertEqual(math.ldexp(INF, n), INF) 588 self.assertEqual(math.ldexp(NINF, n), NINF) 589 self.assertTrue(math.isnan(math.ldexp(NAN, n))) 512 590 513 591 def testLog(self): … … 519 597 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) 520 598 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) 521 self.assertEqual s(math.log(INF), INF)599 self.assertEqual(math.log(INF), INF) 522 600 self.assertRaises(ValueError, math.log, NINF) 523 self.assert_(math.isnan(math.log(NAN))) 601 self.assertTrue(math.isnan(math.log(NAN))) 602 # Log values should match for int and long (issue #18739). 603 for n in range(1, 1000): 604 self.assertEqual(math.log(n), math.log(long(n))) 524 605 525 606 def testLog1p(self): … … 529 610 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1) 530 611 self.ftest('log1p(1)', math.log1p(1), math.log(2)) 531 self.assertEqual s(math.log1p(INF), INF)612 self.assertEqual(math.log1p(INF), INF) 532 613 self.assertRaises(ValueError, math.log1p, NINF) 533 self.assert _(math.isnan(math.log1p(NAN)))614 self.assertTrue(math.isnan(math.log1p(NAN))) 534 615 n= 2**90 535 self.assertAlmostEqual s(math.log1p(n), 62.383246250395075)536 self.assertAlmostEqual s(math.log1p(n), math.log1p(float(n)))616 self.assertAlmostEqual(math.log1p(n), 62.383246250395075) 617 self.assertAlmostEqual(math.log1p(n), math.log1p(float(n))) 537 618 538 619 def testLog10(self): … … 541 622 self.ftest('log10(1)', math.log10(1), 0) 542 623 self.ftest('log10(10)', math.log10(10), 1) 543 self.assertEqual s(math.log(INF), INF)624 self.assertEqual(math.log(INF), INF) 544 625 self.assertRaises(ValueError, math.log10, NINF) 545 self.assert_(math.isnan(math.log10(NAN))) 626 self.assertTrue(math.isnan(math.log10(NAN))) 627 # Log values should match for int and long (issue #18739). 628 for n in range(1, 1000): 629 self.assertEqual(math.log10(n), math.log10(long(n))) 546 630 547 631 def testModf(self): … … 557 641 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) 558 642 559 self.assertEqual s(math.modf(INF), (0.0, INF))560 self.assertEqual s(math.modf(NINF), (-0.0, NINF))643 self.assertEqual(math.modf(INF), (0.0, INF)) 644 self.assertEqual(math.modf(NINF), (-0.0, NINF)) 561 645 562 646 modf_nan = math.modf(NAN) 563 self.assert _(math.isnan(modf_nan[0]))564 self.assert _(math.isnan(modf_nan[1]))647 self.assertTrue(math.isnan(modf_nan[0])) 648 self.assertTrue(math.isnan(modf_nan[1])) 565 649 566 650 def testPow(self): … … 574 658 self.assertEqual((math.pow(1, INF)), 1.) 575 659 self.assertEqual((math.pow(1, NINF)), 1.) 576 self.assert _(math.isnan(math.pow(NAN, 1)))577 self.assert _(math.isnan(math.pow(2, NAN)))578 self.assert _(math.isnan(math.pow(0, NAN)))660 self.assertTrue(math.isnan(math.pow(NAN, 1))) 661 self.assertTrue(math.isnan(math.pow(2, NAN))) 662 self.assertTrue(math.isnan(math.pow(0, NAN))) 579 663 self.assertEqual(math.pow(1, NAN), 1) 580 664 … … 590 674 self.assertRaises(ValueError, math.pow, 0., -3.) 591 675 self.assertRaises(ValueError, math.pow, 0., NINF) 592 self.assert _(math.isnan(math.pow(0., NAN)))676 self.assertTrue(math.isnan(math.pow(0., NAN))) 593 677 594 678 # pow(INF, x) … … 603 687 self.assertEqual(math.pow(INF, -3.), 0.) 604 688 self.assertEqual(math.pow(INF, NINF), 0.) 605 self.assert _(math.isnan(math.pow(INF, NAN)))689 self.assertTrue(math.isnan(math.pow(INF, NAN))) 606 690 607 691 # pow(-0., x) … … 616 700 self.assertRaises(ValueError, math.pow, -0., -3.) 617 701 self.assertRaises(ValueError, math.pow, -0., NINF) 618 self.assert _(math.isnan(math.pow(-0., NAN)))702 self.assertTrue(math.isnan(math.pow(-0., NAN))) 619 703 620 704 # pow(NINF, x) … … 629 713 self.assertEqual(math.pow(NINF, -3.), -0.) 630 714 self.assertEqual(math.pow(NINF, NINF), 0.) 631 self.assert _(math.isnan(math.pow(NINF, NAN)))715 self.assertTrue(math.isnan(math.pow(NINF, NAN))) 632 716 633 717 # pow(-1, x) … … 642 726 self.assertEqual(math.pow(-1., -3.), -1.) 643 727 self.assertEqual(math.pow(-1., NINF), 1.) 644 self.assert _(math.isnan(math.pow(-1., NAN)))728 self.assertTrue(math.isnan(math.pow(-1., NAN))) 645 729 646 730 # pow(1, x) … … 703 787 # the following tests have been commented out since they don't 704 788 # really belong here: the implementation of ** for floats is 705 # independent of the implement ion of math.pow789 # independent of the implementation of math.pow 706 790 #self.assertEqual(1**NAN, 1) 707 791 #self.assertEqual(1**INF, 1) … … 725 809 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) 726 810 try: 727 self.assert _(math.isnan(math.sin(INF)))728 self.assert _(math.isnan(math.sin(NINF)))811 self.assertTrue(math.isnan(math.sin(INF))) 812 self.assertTrue(math.isnan(math.sin(NINF))) 729 813 except ValueError: 730 814 self.assertRaises(ValueError, math.sin, INF) 731 815 self.assertRaises(ValueError, math.sin, NINF) 732 self.assert _(math.isnan(math.sin(NAN)))816 self.assertTrue(math.isnan(math.sin(NAN))) 733 817 734 818 def testSinh(self): … … 737 821 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) 738 822 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) 739 self.assertEqual s(math.sinh(INF), INF)740 self.assertEqual s(math.sinh(NINF), NINF)741 self.assert _(math.isnan(math.sinh(NAN)))823 self.assertEqual(math.sinh(INF), INF) 824 self.assertEqual(math.sinh(NINF), NINF) 825 self.assertTrue(math.isnan(math.sinh(NAN))) 742 826 743 827 def testSqrt(self): … … 746 830 self.ftest('sqrt(1)', math.sqrt(1), 1) 747 831 self.ftest('sqrt(4)', math.sqrt(4), 2) 748 self.assertEqual s(math.sqrt(INF), INF)832 self.assertEqual(math.sqrt(INF), INF) 749 833 self.assertRaises(ValueError, math.sqrt, NINF) 750 self.assert _(math.isnan(math.sqrt(NAN)))834 self.assertTrue(math.isnan(math.sqrt(NAN))) 751 835 752 836 def testTan(self): … … 756 840 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) 757 841 try: 758 self.assert _(math.isnan(math.tan(INF)))759 self.assert _(math.isnan(math.tan(NINF)))842 self.assertTrue(math.isnan(math.tan(INF))) 843 self.assertTrue(math.isnan(math.tan(NINF))) 760 844 except: 761 845 self.assertRaises(ValueError, math.tan, INF) 762 846 self.assertRaises(ValueError, math.tan, NINF) 763 self.assert _(math.isnan(math.tan(NAN)))847 self.assertTrue(math.isnan(math.tan(NAN))) 764 848 765 849 def testTanh(self): … … 769 853 self.ftest('tanh(inf)', math.tanh(INF), 1) 770 854 self.ftest('tanh(-inf)', math.tanh(NINF), -1) 771 self.assert _(math.isnan(math.tanh(NAN)))855 self.assertTrue(math.isnan(math.tanh(NAN))) 772 856 # check that tanh(-0.) == -0. on IEEE 754 systems 773 857 if float.__getformat__("double").startswith("IEEE"): … … 799 883 self.assertRaises(TypeError, math.trunc) 800 884 self.assertRaises(TypeError, math.trunc, 1, 2) 801 # XXX: This is not ideal, but see the comment in math_trunc(). 802 self.assertRaises(AttributeError, math.trunc, TestNoTrunc()) 803 804 t = TestNoTrunc() 805 t.__trunc__ = lambda *args: args 806 self.assertEquals((), math.trunc(t)) 807 self.assertRaises(TypeError, math.trunc, t, 0) 885 self.assertRaises((AttributeError, TypeError), math.trunc, 886 TestNoTrunc()) 808 887 809 888 def testIsnan(self): 810 self.assert _(math.isnan(float("nan")))811 self.assert _(math.isnan(float("inf")* 0.))812 self. failIf(math.isnan(float("inf")))813 self. failIf(math.isnan(0.))814 self. failIf(math.isnan(1.))889 self.assertTrue(math.isnan(float("nan"))) 890 self.assertTrue(math.isnan(float("inf")* 0.)) 891 self.assertFalse(math.isnan(float("inf"))) 892 self.assertFalse(math.isnan(0.)) 893 self.assertFalse(math.isnan(1.)) 815 894 816 895 def testIsinf(self): 817 self.assert _(math.isinf(float("inf")))818 self.assert _(math.isinf(float("-inf")))819 self.assert _(math.isinf(1E400))820 self.assert _(math.isinf(-1E400))821 self. failIf(math.isinf(float("nan")))822 self. failIf(math.isinf(0.))823 self. failIf(math.isinf(1.))896 self.assertTrue(math.isinf(float("inf"))) 897 self.assertTrue(math.isinf(float("-inf"))) 898 self.assertTrue(math.isinf(1E400)) 899 self.assertTrue(math.isinf(-1E400)) 900 self.assertFalse(math.isinf(float("nan"))) 901 self.assertFalse(math.isinf(0.)) 902 self.assertFalse(math.isinf(1.)) 824 903 825 904 # RED_FLAG 16-Oct-2000 Tim … … 861 940 self.fail("sqrt(-1) didn't raise ValueError") 862 941 942 @requires_IEEE_754 863 943 def test_testfile(self): 864 if not float.__getformat__("double").startswith("IEEE"):865 return866 944 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): 867 945 # Skip if either the input or result is complex, or if … … 885 963 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er) 886 964 965 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), 966 "test requires IEEE 754 doubles") 967 def test_mtestfile(self): 968 ALLOWED_ERROR = 20 # permitted error, in ulps 969 fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}" 970 971 failures = [] 972 for id, fn, arg, expected, flags in parse_mtestfile(math_testcases): 973 func = getattr(math, fn) 974 975 if 'invalid' in flags or 'divide-by-zero' in flags: 976 expected = 'ValueError' 977 elif 'overflow' in flags: 978 expected = 'OverflowError' 979 980 try: 981 got = func(arg) 982 except ValueError: 983 got = 'ValueError' 984 except OverflowError: 985 got = 'OverflowError' 986 987 accuracy_failure = None 988 if isinstance(got, float) and isinstance(expected, float): 989 if math.isnan(expected) and math.isnan(got): 990 continue 991 if not math.isnan(expected) and not math.isnan(got): 992 if fn == 'lgamma': 993 # we use a weaker accuracy test for lgamma; 994 # lgamma only achieves an absolute error of 995 # a few multiples of the machine accuracy, in 996 # general. 997 accuracy_failure = acc_check(expected, got, 998 rel_err = 5e-15, 999 abs_err = 5e-15) 1000 elif fn == 'erfc': 1001 # erfc has less-than-ideal accuracy for large 1002 # arguments (x ~ 25 or so), mainly due to the 1003 # error involved in computing exp(-x*x). 1004 # 1005 # XXX Would be better to weaken this test only 1006 # for large x, instead of for all x. 1007 accuracy_failure = ulps_check(expected, got, 2000) 1008 1009 else: 1010 accuracy_failure = ulps_check(expected, got, 20) 1011 if accuracy_failure is None: 1012 continue 1013 1014 if isinstance(got, str) and isinstance(expected, str): 1015 if got == expected: 1016 continue 1017 1018 fail_msg = fail_fmt.format(id, fn, arg, expected, got) 1019 if accuracy_failure is not None: 1020 fail_msg += ' ({})'.format(accuracy_failure) 1021 failures.append(fail_msg) 1022 1023 if failures: 1024 self.fail('Failures in test_mtestfile:\n ' + 1025 '\n '.join(failures)) 1026 1027 887 1028 def test_main(): 888 1029 from doctest import DocFileSuite
Note:
See TracChangeset
for help on using the changeset viewer.