Lines Matching refs:self

220     def __init__(self, value):
221 self.value = value
223 def __float__(self):
224 return self.value
231 def __init__(self, value):
232 self.value = value
234 def __index__(self):
235 return self.value
239 def ftest(self, name, got, expected, ulp_tol=5, abs_tol=0.0):
250 self.fail("{}: {}".format(name, failure))
252 def testConstants(self):
254 self.ftest('pi', math.pi, 3.141592653589793238462643)
255 self.ftest('e', math.e, 2.718281828459045235360287)
256 self.assertEqual(math.tau, 2*math.pi)
258 def testAcos(self):
259 self.assertRaises(TypeError, math.acos)
260 self.ftest('acos(-1)', math.acos(-1), math.pi)
261 self.ftest('acos(0)', math.acos(0), math.pi/2)
262 self.ftest('acos(1)', math.acos(1), 0)
263 self.assertRaises(ValueError, math.acos, INF)
264 self.assertRaises(ValueError, math.acos, NINF)
265 self.assertRaises(ValueError, math.acos, 1 + eps)
266 self.assertRaises(ValueError, math.acos, -1 - eps)
267 self.assertTrue(math.isnan(math.acos(NAN)))
269 def testAcosh(self):
270 self.assertRaises(TypeError, math.acosh)
271 self.ftest('acosh(1)', math.acosh(1), 0)
272 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
273 self.assertRaises(ValueError, math.acosh, 0)
274 self.assertRaises(ValueError, math.acosh, -1)
275 self.assertEqual(math.acosh(INF), INF)
276 self.assertRaises(ValueError, math.acosh, NINF)
277 self.assertTrue(math.isnan(math.acosh(NAN)))
279 def testAsin(self):
280 self.assertRaises(TypeError, math.asin)
281 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
282 self.ftest('asin(0)', math.asin(0), 0)
283 self.ftest('asin(1)', math.asin(1), math.pi/2)
284 self.assertRaises(ValueError, math.asin, INF)
285 self.assertRaises(ValueError, math.asin, NINF)
286 self.assertRaises(ValueError, math.asin, 1 + eps)
287 self.assertRaises(ValueError, math.asin, -1 - eps)
288 self.assertTrue(math.isnan(math.asin(NAN)))
290 def testAsinh(self):
291 self.assertRaises(TypeError, math.asinh)
292 self.ftest('asinh(0)', math.asinh(0), 0)
293 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
294 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
295 self.assertEqual(math.asinh(INF), INF)
296 self.assertEqual(math.asinh(NINF), NINF)
297 self.assertTrue(math.isnan(math.asinh(NAN)))
299 def testAtan(self):
300 self.assertRaises(TypeError, math.atan)
301 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
302 self.ftest('atan(0)', math.atan(0), 0)
303 self.ftest('atan(1)', math.atan(1), math.pi/4)
304 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
305 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
306 self.assertTrue(math.isnan(math.atan(NAN)))
308 def testAtanh(self):
309 self.assertRaises(TypeError, math.atan)
310 self.ftest('atanh(0)', math.atanh(0), 0)
311 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
312 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
313 self.assertRaises(ValueError, math.atanh, 1)
314 self.assertRaises(ValueError, math.atanh, -1)
315 self.assertRaises(ValueError, math.atanh, INF)
316 self.assertRaises(ValueError, math.atanh, NINF)
317 self.assertTrue(math.isnan(math.atanh(NAN)))
319 def testAtan2(self):
320 self.assertRaises(TypeError, math.atan2)
321 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
322 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
323 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
324 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
325 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
328 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
329 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
330 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
331 self.assertEqual(math.atan2(0., 0.), 0.)
332 self.assertEqual(math.atan2(0., 2.3), 0.)
333 self.assertEqual(math.atan2(0., INF), 0.)
334 self.assertTrue(math.isnan(math.atan2(0., NAN)))
336 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
337 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
338 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
339 self.assertEqual(math.atan2(-0., 0.), -0.)
340 self.assertEqual(math.atan2(-0., 2.3), -0.)
341 self.assertEqual(math.atan2(-0., INF), -0.)
342 self.assertTrue(math.isnan(math.atan2(-0., NAN)))
344 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
345 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
346 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
347 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
348 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
349 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
350 self.assertTrue(math.isnan(math.atan2(INF, NAN)))
352 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
353 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
354 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
355 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
356 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
357 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
358 self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
360 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
361 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
362 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
363 self.assertEqual(math.atan2(2.3, INF), 0.)
364 self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
366 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
367 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
368 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
369 self.assertEqual(math.atan2(-2.3, INF), -0.)
370 self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
372 self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
373 self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
374 self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
375 self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
376 self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
377 self.assertTrue(math.isnan(math.atan2(NAN, INF)))
378 self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
380 def testCbrt(self):
381 self.assertRaises(TypeError, math.cbrt)
382 self.ftest('cbrt(0)', math.cbrt(0), 0)
383 self.ftest('cbrt(1)', math.cbrt(1), 1)
384 self.ftest('cbrt(8)', math.cbrt(8), 2)
385 self.ftest('cbrt(0.0)', math.cbrt(0.0), 0.0)
386 self.ftest('cbrt(-0.0)', math.cbrt(-0.0), -0.0)
387 self.ftest('cbrt(1.2)', math.cbrt(1.2), 1.062658569182611)
388 self.ftest('cbrt(-2.6)', math.cbrt(-2.6), -1.375068867074141)
389 self.ftest('cbrt(27)', math.cbrt(27), 3)
390 self.ftest('cbrt(-1)', math.cbrt(-1), -1)
391 self.ftest('cbrt(-27)', math.cbrt(-27), -3)
392 self.assertEqual(math.cbrt(INF), INF)
393 self.assertEqual(math.cbrt(NINF), NINF)
394 self.assertTrue(math.isnan(math.cbrt(NAN)))
396 def testCeil(self):
397 self.assertRaises(TypeError, math.ceil)
398 self.assertEqual(int, type(math.ceil(0.5)))
399 self.assertEqual(math.ceil(0.5), 1)
400 self.assertEqual(math.ceil(1.0), 1)
401 self.assertEqual(math.ceil(1.5), 2)
402 self.assertEqual(math.ceil(-0.5), 0)
403 self.assertEqual(math.ceil(-1.0), -1)
404 self.assertEqual(math.ceil(-1.5), -1)
405 self.assertEqual(math.ceil(0.0), 0)
406 self.assertEqual(math.ceil(-0.0), 0)
407 #self.assertEqual(math.ceil(INF), INF)
408 #self.assertEqual(math.ceil(NINF), NINF)
409 #self.assertTrue(math.isnan(math.ceil(NAN)))
412 def __ceil__(self):
415 def __ceil__(self):
419 self.assertEqual(math.ceil(TestCeil()), 42)
420 self.assertEqual(math.ceil(FloatCeil()), 42)
421 self.assertEqual(math.ceil(FloatLike(42.5)), 43)
422 self.assertRaises(TypeError, math.ceil, TestNoCeil())
426 self.assertRaises(TypeError, math.ceil, t)
427 self.assertRaises(TypeError, math.ceil, t, 0)
430 def testCopysign(self):
431 self.assertEqual(math.copysign(1, 42), 1.0)
432 self.assertEqual(math.copysign(0., 42), 0.0)
433 self.assertEqual(math.copysign(1., -42), -1.0)
434 self.assertEqual(math.copysign(3, 0.), 3.0)
435 self.assertEqual(math.copysign(4., -0.), -4.0)
437 self.assertRaises(TypeError, math.copysign)
439 self.assertEqual(math.copysign(1., 0.), 1.)
440 self.assertEqual(math.copysign(1., -0.), -1.)
441 self.assertEqual(math.copysign(INF, 0.), INF)
442 self.assertEqual(math.copysign(INF, -0.), NINF)
443 self.assertEqual(math.copysign(NINF, 0.), INF)
444 self.assertEqual(math.copysign(NINF, -0.), NINF)
446 self.assertEqual(math.copysign(1., INF), 1.)
447 self.assertEqual(math.copysign(1., NINF), -1.)
448 self.assertEqual(math.copysign(INF, INF), INF)
449 self.assertEqual(math.copysign(INF, NINF), NINF)
450 self.assertEqual(math.copysign(NINF, INF), INF)
451 self.assertEqual(math.copysign(NINF, NINF), NINF)
452 self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
453 self.assertTrue(math.isnan(math.copysign(NAN, INF)))
454 self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
455 self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
459 self.assertTrue(math.isinf(math.copysign(INF, NAN)))
461 self.assertEqual(abs(math.copysign(2., NAN)), 2.)
463 def testCos(self):
464 self.assertRaises(TypeError, math.cos)
465 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0, abs_tol=math.ulp(1))
466 self.ftest('cos(0)', math.cos(0), 1)
467 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0, abs_tol=math.ulp(1))
468 self.ftest('cos(pi)', math.cos(math.pi), -1)
470 self.assertTrue(math.isnan(math.cos(INF)))
471 self.assertTrue(math.isnan(math.cos(NINF)))
473 self.assertRaises(ValueError, math.cos, INF)
474 self.assertRaises(ValueError, math.cos, NINF)
475 self.assertTrue(math.isnan(math.cos(NAN)))
479 def testCosh(self):
480 self.assertRaises(TypeError, math.cosh)
481 self.ftest('cosh(0)', math.cosh(0), 1)
482 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
483 self.assertEqual(math.cosh(INF), INF)
484 self.assertEqual(math.cosh(NINF), INF)
485 self.assertTrue(math.isnan(math.cosh(NAN)))
487 def testDegrees(self):
488 self.assertRaises(TypeError, math.degrees)
489 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
490 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
491 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
492 self.ftest('degrees(0)', math.degrees(0), 0)
494 def testExp(self):
495 self.assertRaises(TypeError, math.exp)
496 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
497 self.ftest('exp(0)', math.exp(0), 1)
498 self.ftest('exp(1)', math.exp(1), math.e)
499 self.assertEqual(math.exp(INF), INF)
500 self.assertEqual(math.exp(NINF), 0.)
501 self.assertTrue(math.isnan(math.exp(NAN)))
502 self.assertRaises(OverflowError, math.exp, 1000000)
504 def testExp2(self):
505 self.assertRaises(TypeError, math.exp2)
506 self.ftest('exp2(-1)', math.exp2(-1), 0.5)
507 self.ftest('exp2(0)', math.exp2(0), 1)
508 self.ftest('exp2(1)', math.exp2(1), 2)
509 self.ftest('exp2(2.3)', math.exp2(2.3), 4.924577653379665)
510 self.assertEqual(math.exp2(INF), INF)
511 self.assertEqual(math.exp2(NINF), 0.)
512 self.assertTrue(math.isnan(math.exp2(NAN)))
513 self.assertRaises(OverflowError, math.exp2, 1000000)
515 def testFabs(self):
516 self.assertRaises(TypeError, math.fabs)
517 self.ftest('fabs(-1)', math.fabs(-1), 1)
518 self.ftest('fabs(0)', math.fabs(0), 0)
519 self.ftest('fabs(1)', math.fabs(1), 1)
521 def testFactorial(self):
522 self.assertEqual(math.factorial(0), 1)
526 self.assertEqual(math.factorial(i), total)
527 self.assertEqual(math.factorial(i), py_factorial(i))
528 self.assertRaises(ValueError, math.factorial, -1)
529 self.assertRaises(ValueError, math.factorial, -10**100)
531 def testFactorialNonIntegers(self):
532 self.assertRaises(TypeError, math.factorial, 5.0)
533 self.assertRaises(TypeError, math.factorial, 5.2)
534 self.assertRaises(TypeError, math.factorial, -1.0)
535 self.assertRaises(TypeError, math.factorial, -1e100)
536 self.assertRaises(TypeError, math.factorial, decimal.Decimal('5'))
537 self.assertRaises(TypeError, math.factorial, decimal.Decimal('5.2'))
538 self.assertRaises(TypeError, math.factorial, "5")
542 def testFactorialHugeInputs(self):
545 self.assertRaises(OverflowError, math.factorial, 10**100)
546 self.assertRaises(TypeError, math.factorial, 1e100)
548 def testFloor(self):
549 self.assertRaises(TypeError, math.floor)
550 self.assertEqual(int, type(math.floor(0.5)))
551 self.assertEqual(math.floor(0.5), 0)
552 self.assertEqual(math.floor(1.0), 1)
553 self.assertEqual(math.floor(1.5), 1)
554 self.assertEqual(math.floor(-0.5), -1)
555 self.assertEqual(math.floor(-1.0), -1)
556 self.assertEqual(math.floor(-1.5), -2)
557 #self.assertEqual(math.ceil(INF), INF)
558 #self.assertEqual(math.ceil(NINF), NINF)
559 #self.assertTrue(math.isnan(math.floor(NAN)))
562 def __floor__(self):
565 def __floor__(self):
569 self.assertEqual(math.floor(TestFloor()), 42)
570 self.assertEqual(math.floor(FloatFloor()), 42)
571 self.assertEqual(math.floor(FloatLike(41.9)), 41)
572 self.assertRaises(TypeError, math.floor, TestNoFloor())
576 self.assertRaises(TypeError, math.floor, t)
577 self.assertRaises(TypeError, math.floor, t, 0)
579 def testFmod(self):
580 self.assertRaises(TypeError, math.fmod)
581 self.ftest('fmod(10, 1)', math.fmod(10, 1), 0.0)
582 self.ftest('fmod(10, 0.5)', math.fmod(10, 0.5), 0.0)
583 self.ftest('fmod(10, 1.5)', math.fmod(10, 1.5), 1.0)
584 self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0)
585 self.ftest('fmod(-10, 0.5)', math.fmod(-10, 0.5), -0.0)
586 self.ftest('fmod(-10, 1.5)', math.fmod(-10, 1.5), -1.0)
587 self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
588 self.assertTrue(math.isnan(math.fmod(1., NAN)))
589 self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
590 self.assertRaises(ValueError, math.fmod, 1., 0.)
591 self.assertRaises(ValueError, math.fmod, INF, 1.)
592 self.assertRaises(ValueError, math.fmod, NINF, 1.)
593 self.assertRaises(ValueError, math.fmod, INF, 0.)
594 self.assertEqual(math.fmod(3.0, INF), 3.0)
595 self.assertEqual(math.fmod(-3.0, INF), -3.0)
596 self.assertEqual(math.fmod(3.0, NINF), 3.0)
597 self.assertEqual(math.fmod(-3.0, NINF), -3.0)
598 self.assertEqual(math.fmod(0.0, 3.0), 0.0)
599 self.assertEqual(math.fmod(0.0, NINF), 0.0)
601 def testFrexp(self):
602 self.assertRaises(TypeError, math.frexp)
607 self.fail('%s returned %r, expected %r'%\
615 self.assertEqual(math.frexp(INF)[0], INF)
616 self.assertEqual(math.frexp(NINF)[0], NINF)
617 self.assertTrue(math.isnan(math.frexp(NAN)[0]))
622 def testFsum(self):
696 self.fail("test %d failed: got OverflowError, expected %r "
699 self.fail("test %d failed: got ValueError, expected %r "
701 self.assertEqual(actual, expected)
714 self.assertEqual(msum(vals), math.fsum(vals))
716 def testGcd(self):
718 self.assertEqual(gcd(0, 0), 0)
719 self.assertEqual(gcd(1, 0), 1)
720 self.assertEqual(gcd(-1, 0), 1)
721 self.assertEqual(gcd(0, 1), 1)
722 self.assertEqual(gcd(0, -1), 1)
723 self.assertEqual(gcd(7, 1), 1)
724 self.assertEqual(gcd(7, -1), 1)
725 self.assertEqual(gcd(-23, 15), 1)
726 self.assertEqual(gcd(120, 84), 12)
727 self.assertEqual(gcd(84, -120), 12)
728 self.assertEqual(gcd(1216342683557601535506311712,
737 self.assertEqual(gcd(a, b), c)
738 self.assertEqual(gcd(b, a), c)
739 self.assertEqual(gcd(-a, b), c)
740 self.assertEqual(gcd(b, -a), c)
741 self.assertEqual(gcd(a, -b), c)
742 self.assertEqual(gcd(-b, a), c)
743 self.assertEqual(gcd(-a, -b), c)
744 self.assertEqual(gcd(-b, -a), c)
746 self.assertEqual(gcd(), 0)
747 self.assertEqual(gcd(120), 120)
748 self.assertEqual(gcd(-120), 120)
749 self.assertEqual(gcd(120, 84, 102), 6)
750 self.assertEqual(gcd(120, 1, 84), 1)
752 self.assertRaises(TypeError, gcd, 120.0)
753 self.assertRaises(TypeError, gcd, 120.0, 84)
754 self.assertRaises(TypeError, gcd, 120, 84.0)
755 self.assertRaises(TypeError, gcd, 120, 1, 84.0)
756 self.assertEqual(gcd(MyIndexable(120), MyIndexable(84)), 12)
758 def testHypot(self):
768 self.assertAlmostEqual(
774 self.assertEqual(hypot(12.0, 5.0), 13.0)
775 self.assertEqual(hypot(12, 5), 13)
776 self.assertEqual(hypot(Decimal(12), Decimal(5)), 13)
777 self.assertEqual(hypot(Fraction(12, 32), Fraction(5, 32)), Fraction(13, 32))
778 self.assertEqual(hypot(bool(1), bool(0), bool(1), bool(1)), math.sqrt(3))
781 self.assertEqual(hypot(0.0, 0.0), 0.0) # Max input is zero
782 self.assertEqual(hypot(-10.5), 10.5) # Negative input
783 self.assertEqual(hypot(), 0.0) # Negative input
784 self.assertEqual(1.0,
787 self.assertEqual( # Handling of moving max to the end
793 with self.assertRaises(TypeError): # Reject keyword args
795 with self.assertRaises(TypeError): # Reject values without __float__
798 with self.assertRaises((ValueError, OverflowError)):
802 self.assertEqual(hypot(INF), INF)
803 self.assertEqual(hypot(0, INF), INF)
804 self.assertEqual(hypot(10, INF), INF)
805 self.assertEqual(hypot(-10, INF), INF)
806 self.assertEqual(hypot(NAN, INF), INF)
807 self.assertEqual(hypot(INF, NAN), INF)
808 self.assertEqual(hypot(NINF, NAN), INF)
809 self.assertEqual(hypot(NAN, NINF), INF)
810 self.assertEqual(hypot(-INF, INF), INF)
811 self.assertEqual(hypot(-INF, -INF), INF)
812 self.assertEqual(hypot(10, -INF), INF)
815 self.assertTrue(math.isnan(hypot(NAN)))
816 self.assertTrue(math.isnan(hypot(0, NAN)))
817 self.assertTrue(math.isnan(hypot(NAN, 10)))
818 self.assertTrue(math.isnan(hypot(10, NAN)))
819 self.assertTrue(math.isnan(hypot(NAN, NAN)))
820 self.assertTrue(math.isnan(hypot(NAN)))
825 self.assertTrue(math.isclose(hypot(*([fourthmax]*n)),
831 self.assertEqual(math.hypot(4*scale, 3*scale), 5*scale)
836 def testHypotAccuracy(self):
891 with self.subTest(hx=hx, hy=hy, x=x, y=y):
894 self.assertEqual(hypot(x, y), z)
896 def testDist(self):
904 self.assertEqual(dist((1.0, 2.0, 3.0), (4.0, 2.0, -1.0)), 5.0)
905 self.assertEqual(dist((1, 2, 3), (4, 2, -1)), 5.0)
913 self.assertAlmostEqual(
919 self.assertEqual(dist([1.0, 2.0, 3.0], [4.0, 2.0, -1.0]), 5.0)
920 self.assertEqual(dist(iter([1.0, 2.0, 3.0]), iter([4.0, 2.0, -1.0])), 5.0)
923 self.assertEqual(dist((14.0, 1.0), (2.0, -4.0)), 13.0)
924 self.assertEqual(dist((14, 1), (2, -4)), 13)
925 self.assertEqual(dist((D(14), D(1)), (D(2), D(-4))), D(13))
926 self.assertEqual(dist((F(14, 32), F(1, 32)), (F(2, 32), F(-4, 32))),
928 self.assertEqual(dist((True, True, False, True, False),
933 self.assertEqual(dist((13.25, 12.5, -3.25),
935 0.0) # Distance with self is zero
936 self.assertEqual(dist((), ()), 0.0) # Zero-dimensional case
937 self.assertEqual(1.0, # Convert negative zero to positive zero
940 self.assertEqual(1.0, # Convert negative zero to positive zero
943 self.assertEqual( # Handling of moving max to the end
951 self.assertEqual(dist(T((1, 2, 3)), ((4, 2, -1))), 5.0)
954 with self.assertRaises(TypeError): # Reject keyword args
956 with self.assertRaises(TypeError): # Too few args
958 with self.assertRaises(TypeError): # Too many args
960 with self.assertRaises(TypeError): # Scalars not allowed
962 with self.assertRaises(TypeError): # Reject values without __float__
964 with self.assertRaises(ValueError): # Check dimension agree
966 with self.assertRaises(ValueError): # Check dimension agree
968 with self.assertRaises(TypeError): # Rejects invalid types
971 with self.assertRaises((ValueError, OverflowError)):
973 with self.assertRaises((ValueError, OverflowError)):
979 self.assertEqual(dist((p,), (q,)), abs(p - q))
988 self.assertEqual(dist(p, q), INF)
991 self.assertTrue(math.isnan(dist(p, q)))
998 self.assertTrue(math.isclose(dist(p, q), fourthmax * math.sqrt(n)))
999 self.assertTrue(math.isclose(dist(q, p), fourthmax * math.sqrt(n)))
1006 self.assertEqual(math.dist(p, q), 5*scale)
1007 self.assertEqual(math.dist(q, p), 5*scale)
1009 def test_math_dist_leak(self):
1011 with self.assertRaises(ValueError):
1014 def testIsqrt(self):
1024 with self.subTest(value=value):
1026 self.assertIs(type(s), int)
1027 self.assertLessEqual(s*s, value)
1028 self.assertLess(value, (s+1)*(s+1))
1031 with self.assertRaises(ValueError):
1036 self.assertIs(type(s), int)
1037 self.assertEqual(s, 1)
1040 self.assertIs(type(s), int)
1041 self.assertEqual(s, 0)
1044 def __init__(self, value):
1045 self.value = value
1047 def __index__(self):
1048 return self.value
1051 self.assertIs(type(s), int)
1052 self.assertEqual(s, 41)
1054 with self.assertRaises(ValueError):
1063 with self.subTest(value=value):
1064 with self.assertRaises(TypeError):
1067 def test_lcm(self):
1069 self.assertEqual(lcm(0, 0), 0)
1070 self.assertEqual(lcm(1, 0), 0)
1071 self.assertEqual(lcm(-1, 0), 0)
1072 self.assertEqual(lcm(0, 1), 0)
1073 self.assertEqual(lcm(0, -1), 0)
1074 self.assertEqual(lcm(7, 1), 7)
1075 self.assertEqual(lcm(7, -1), 7)
1076 self.assertEqual(lcm(-23, 15), 345)
1077 self.assertEqual(lcm(120, 84), 840)
1078 self.assertEqual(lcm(84, -120), 840)
1079 self.assertEqual(lcm(1216342683557601535506311712,
1090 self.assertEqual(lcm(a, b), d)
1091 self.assertEqual(lcm(b, a), d)
1092 self.assertEqual(lcm(-a, b), d)
1093 self.assertEqual(lcm(b, -a), d)
1094 self.assertEqual(lcm(a, -b), d)
1095 self.assertEqual(lcm(-b, a), d)
1096 self.assertEqual(lcm(-a, -b), d)
1097 self.assertEqual(lcm(-b, -a), d)
1099 self.assertEqual(lcm(), 1)
1100 self.assertEqual(lcm(120), 120)
1101 self.assertEqual(lcm(-120), 120)
1102 self.assertEqual(lcm(120, 84, 102), 14280)
1103 self.assertEqual(lcm(120, 0, 84), 0)
1105 self.assertRaises(TypeError, lcm, 120.0)
1106 self.assertRaises(TypeError, lcm, 120.0, 84)
1107 self.assertRaises(TypeError, lcm, 120, 84.0)
1108 self.assertRaises(TypeError, lcm, 120, 0, 84.0)
1109 self.assertEqual(lcm(MyIndexable(120), MyIndexable(84)), 840)
1111 def testLdexp(self):
1112 self.assertRaises(TypeError, math.ldexp)
1113 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
1114 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
1115 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
1116 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
1117 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
1118 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
1119 self.assertEqual(math.ldexp(1., -1000000), 0.)
1120 self.assertEqual(math.ldexp(-1., -1000000), -0.)
1121 self.assertEqual(math.ldexp(INF, 30), INF)
1122 self.assertEqual(math.ldexp(NINF, -213), NINF)
1123 self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
1127 self.assertEqual(math.ldexp(INF, -n), INF)
1128 self.assertEqual(math.ldexp(NINF, -n), NINF)
1129 self.assertEqual(math.ldexp(1., -n), 0.)
1130 self.assertEqual(math.ldexp(-1., -n), -0.)
1131 self.assertEqual(math.ldexp(0., -n), 0.)
1132 self.assertEqual(math.ldexp(-0., -n), -0.)
1133 self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
1135 self.assertRaises(OverflowError, math.ldexp, 1., n)
1136 self.assertRaises(OverflowError, math.ldexp, -1., n)
1137 self.assertEqual(math.ldexp(0., n), 0.)
1138 self.assertEqual(math.ldexp(-0., n), -0.)
1139 self.assertEqual(math.ldexp(INF, n), INF)
1140 self.assertEqual(math.ldexp(NINF, n), NINF)
1141 self.assertTrue(math.isnan(math.ldexp(NAN, n)))
1143 def testLog(self):
1144 self.assertRaises(TypeError, math.log)
1145 self.ftest('log(1/e)', math.log(1/math.e), -1)
1146 self.ftest('log(1)', math.log(1), 0)
1147 self.ftest('log(e)', math.log(math.e), 1)
1148 self.ftest('log(32,2)', math.log(32,2), 5)
1149 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
1150 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
1151 self.ftest('log(10**1000)', math.log(10**1000),
1153 self.assertRaises(ValueError, math.log, -1.5)
1154 self.assertRaises(ValueError, math.log, -10**1000)
1155 self.assertRaises(ValueError, math.log, NINF)
1156 self.assertEqual(math.log(INF), INF)
1157 self.assertTrue(math.isnan(math.log(NAN)))
1159 def testLog1p(self):
1160 self.assertRaises(TypeError, math.log1p)
1162 self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
1163 self.assertRaises(ValueError, math.log1p, -1)
1164 self.assertEqual(math.log1p(INF), INF)
1167 def testLog2(self):
1168 self.assertRaises(TypeError, math.log2)
1171 self.assertEqual(math.log2(1), 0.0)
1172 self.assertEqual(math.log2(2), 1.0)
1173 self.assertEqual(math.log2(4), 2.0)
1176 self.assertEqual(math.log2(2**1023), 1023.0)
1177 self.assertEqual(math.log2(2**1024), 1024.0)
1178 self.assertEqual(math.log2(2**2000), 2000.0)
1180 self.assertRaises(ValueError, math.log2, -1.5)
1181 self.assertRaises(ValueError, math.log2, NINF)
1182 self.assertTrue(math.isnan(math.log2(NAN)))
1187 def testLog2Exact(self):
1191 self.assertEqual(actual, expected)
1193 def testLog10(self):
1194 self.assertRaises(TypeError, math.log10)
1195 self.ftest('log10(0.1)', math.log10(0.1), -1)
1196 self.ftest('log10(1)', math.log10(1), 0)
1197 self.ftest('log10(10)', math.log10(10), 1)
1198 self.ftest('log10(10**1000)', math.log10(10**1000), 1000.0)
1199 self.assertRaises(ValueError, math.log10, -1.5)
1200 self.assertRaises(ValueError, math.log10, -10**1000)
1201 self.assertRaises(ValueError, math.log10, NINF)
1202 self.assertEqual(math.log(INF), INF)
1203 self.assertTrue(math.isnan(math.log10(NAN)))
1205 def testModf(self):
1206 self.assertRaises(TypeError, math.modf)
1211 self.fail('%s returned %r, expected %r'%\
1217 self.assertEqual(math.modf(INF), (0.0, INF))
1218 self.assertEqual(math.modf(NINF), (-0.0, NINF))
1221 self.assertTrue(math.isnan(modf_nan[0]))
1222 self.assertTrue(math.isnan(modf_nan[1]))
1224 def testPow(self):
1225 self.assertRaises(TypeError, math.pow)
1226 self.ftest('pow(0,1)', math.pow(0,1), 0)
1227 self.ftest('pow(1,0)', math.pow(1,0), 1)
1228 self.ftest('pow(2,1)', math.pow(2,1), 2)
1229 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
1230 self.assertEqual(math.pow(INF, 1), INF)
1231 self.assertEqual(math.pow(NINF, 1), NINF)
1232 self.assertEqual((math.pow(1, INF)), 1.)
1233 self.assertEqual((math.pow(1, NINF)), 1.)
1234 self.assertTrue(math.isnan(math.pow(NAN, 1)))
1235 self.assertTrue(math.isnan(math.pow(2, NAN)))
1236 self.assertTrue(math.isnan(math.pow(0, NAN)))
1237 self.assertEqual(math.pow(1, NAN), 1)
1240 self.assertEqual(math.pow(0., INF), 0.)
1241 self.assertEqual(math.pow(0., 3.), 0.)
1242 self.assertEqual(math.pow(0., 2.3), 0.)
1243 self.assertEqual(math.pow(0., 2.), 0.)
1244 self.assertEqual(math.pow(0., 0.), 1.)
1245 self.assertEqual(math.pow(0., -0.), 1.)
1246 self.assertRaises(ValueError, math.pow, 0., -2.)
1247 self.assertRaises(ValueError, math.pow, 0., -2.3)
1248 self.assertRaises(ValueError, math.pow, 0., -3.)
1249 self.assertEqual(math.pow(0., NINF), INF)
1250 self.assertTrue(math.isnan(math.pow(0., NAN)))
1253 self.assertEqual(math.pow(INF, INF), INF)
1254 self.assertEqual(math.pow(INF, 3.), INF)
1255 self.assertEqual(math.pow(INF, 2.3), INF)
1256 self.assertEqual(math.pow(INF, 2.), INF)
1257 self.assertEqual(math.pow(INF, 0.), 1.)
1258 self.assertEqual(math.pow(INF, -0.), 1.)
1259 self.assertEqual(math.pow(INF, -2.), 0.)
1260 self.assertEqual(math.pow(INF, -2.3), 0.)
1261 self.assertEqual(math.pow(INF, -3.), 0.)
1262 self.assertEqual(math.pow(INF, NINF), 0.)
1263 self.assertTrue(math.isnan(math.pow(INF, NAN)))
1266 self.assertEqual(math.pow(-0., INF), 0.)
1267 self.assertEqual(math.pow(-0., 3.), -0.)
1268 self.assertEqual(math.pow(-0., 2.3), 0.)
1269 self.assertEqual(math.pow(-0., 2.), 0.)
1270 self.assertEqual(math.pow(-0., 0.), 1.)
1271 self.assertEqual(math.pow(-0., -0.), 1.)
1272 self.assertRaises(ValueError, math.pow, -0., -2.)
1273 self.assertRaises(ValueError, math.pow, -0., -2.3)
1274 self.assertRaises(ValueError, math.pow, -0., -3.)
1275 self.assertEqual(math.pow(-0., NINF), INF)
1276 self.assertTrue(math.isnan(math.pow(-0., NAN)))
1279 self.assertEqual(math.pow(NINF, INF), INF)
1280 self.assertEqual(math.pow(NINF, 3.), NINF)
1281 self.assertEqual(math.pow(NINF, 2.3), INF)
1282 self.assertEqual(math.pow(NINF, 2.), INF)
1283 self.assertEqual(math.pow(NINF, 0.), 1.)
1284 self.assertEqual(math.pow(NINF, -0.), 1.)
1285 self.assertEqual(math.pow(NINF, -2.), 0.)
1286 self.assertEqual(math.pow(NINF, -2.3), 0.)
1287 self.assertEqual(math.pow(NINF, -3.), -0.)
1288 self.assertEqual(math.pow(NINF, NINF), 0.)
1289 self.assertTrue(math.isnan(math.pow(NINF, NAN)))
1292 self.assertEqual(math.pow(-1., INF), 1.)
1293 self.assertEqual(math.pow(-1., 3.), -1.)
1294 self.assertRaises(ValueError, math.pow, -1., 2.3)
1295 self.assertEqual(math.pow(-1., 2.), 1.)
1296 self.assertEqual(math.pow(-1., 0.), 1.)
1297 self.assertEqual(math.pow(-1., -0.), 1.)
1298 self.assertEqual(math.pow(-1., -2.), 1.)
1299 self.assertRaises(ValueError, math.pow, -1., -2.3)
1300 self.assertEqual(math.pow(-1., -3.), -1.)
1301 self.assertEqual(math.pow(-1., NINF), 1.)
1302 self.assertTrue(math.isnan(math.pow(-1., NAN)))
1305 self.assertEqual(math.pow(1., INF), 1.)
1306 self.assertEqual(math.pow(1., 3.), 1.)
1307 self.assertEqual(math.pow(1., 2.3), 1.)
1308 self.assertEqual(math.pow(1., 2.), 1.)
1309 self.assertEqual(math.pow(1., 0.), 1.)
1310 self.assertEqual(math.pow(1., -0.), 1.)
1311 self.assertEqual(math.pow(1., -2.), 1.)
1312 self.assertEqual(math.pow(1., -2.3), 1.)
1313 self.assertEqual(math.pow(1., -3.), 1.)
1314 self.assertEqual(math.pow(1., NINF), 1.)
1315 self.assertEqual(math.pow(1., NAN), 1.)
1318 self.assertEqual(math.pow(2.3, 0.), 1.)
1319 self.assertEqual(math.pow(-2.3, 0.), 1.)
1320 self.assertEqual(math.pow(NAN, 0.), 1.)
1321 self.assertEqual(math.pow(2.3, -0.), 1.)
1322 self.assertEqual(math.pow(-2.3, -0.), 1.)
1323 self.assertEqual(math.pow(NAN, -0.), 1.)
1326 self.assertRaises(ValueError, math.pow, -1., 2.3)
1327 self.assertRaises(ValueError, math.pow, -15., -3.1)
1330 self.assertEqual(math.pow(1.9, NINF), 0.)
1331 self.assertEqual(math.pow(1.1, NINF), 0.)
1332 self.assertEqual(math.pow(0.9, NINF), INF)
1333 self.assertEqual(math.pow(0.1, NINF), INF)
1334 self.assertEqual(math.pow(-0.1, NINF), INF)
1335 self.assertEqual(math.pow(-0.9, NINF), INF)
1336 self.assertEqual(math.pow(-1.1, NINF), 0.)
1337 self.assertEqual(math.pow(-1.9, NINF), 0.)
1340 self.assertEqual(math.pow(1.9, INF), INF)
1341 self.assertEqual(math.pow(1.1, INF), INF)
1342 self.assertEqual(math.pow(0.9, INF), 0.)
1343 self.assertEqual(math.pow(0.1, INF), 0.)
1344 self.assertEqual(math.pow(-0.1, INF), 0.)
1345 self.assertEqual(math.pow(-0.9, INF), 0.)
1346 self.assertEqual(math.pow(-1.1, INF), INF)
1347 self.assertEqual(math.pow(-1.9, INF), INF)
1350 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
1351 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
1352 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
1353 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
1354 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
1355 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
1356 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
1357 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
1358 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
1359 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
1364 #self.assertEqual(1**NAN, 1)
1365 #self.assertEqual(1**INF, 1)
1366 #self.assertEqual(1**NINF, 1)
1367 #self.assertEqual(1**0, 1)
1368 #self.assertEqual(1.**NAN, 1)
1369 #self.assertEqual(1.**INF, 1)
1370 #self.assertEqual(1.**NINF, 1)
1371 #self.assertEqual(1.**0, 1)
1373 def testRadians(self):
1374 self.assertRaises(TypeError, math.radians)
1375 self.ftest('radians(180)', math.radians(180), math.pi)
1376 self.ftest('radians(90)', math.radians(90), math.pi/2)
1377 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
1378 self.ftest('radians(0)', math.radians(0), 0)
1381 def testRemainder(self):
1391 self.assertLessEqual(abs(fr), abs(fy/2))
1394 self.assertEqual(n, int(n))
1397 self.assertEqual(n/2, int(n/2))
1460 with self.subTest(case=case):
1469 self.assertEqual(actual.hex(), expected.hex())
1489 self.assertIsNaN(math.remainder(NAN, value))
1490 self.assertIsNaN(math.remainder(value, NAN))
1494 self.assertEqual(math.remainder(value, INF), value)
1495 self.assertEqual(math.remainder(value, NINF), value)
1500 with self.assertRaises(ValueError):
1502 with self.assertRaises(ValueError):
1504 with self.assertRaises(ValueError):
1506 with self.assertRaises(ValueError):
1509 def testSin(self):
1510 self.assertRaises(TypeError, math.sin)
1511 self.ftest('sin(0)', math.sin(0), 0)
1512 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
1513 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
1515 self.assertTrue(math.isnan(math.sin(INF)))
1516 self.assertTrue(math.isnan(math.sin(NINF)))
1518 self.assertRaises(ValueError, math.sin, INF)
1519 self.assertRaises(ValueError, math.sin, NINF)
1520 self.assertTrue(math.isnan(math.sin(NAN)))
1522 def testSinh(self):
1523 self.assertRaises(TypeError, math.sinh)
1524 self.ftest('sinh(0)', math.sinh(0), 0)
1525 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
1526 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
1527 self.assertEqual(math.sinh(INF), INF)
1528 self.assertEqual(math.sinh(NINF), NINF)
1529 self.assertTrue(math.isnan(math.sinh(NAN)))
1531 def testSqrt(self):
1532 self.assertRaises(TypeError, math.sqrt)
1533 self.ftest('sqrt(0)', math.sqrt(0), 0)
1534 self.ftest('sqrt(0)', math.sqrt(0.0), 0.0)
1535 self.ftest('sqrt(2.5)', math.sqrt(2.5), 1.5811388300841898)
1536 self.ftest('sqrt(0.25)', math.sqrt(0.25), 0.5)
1537 self.ftest('sqrt(25.25)', math.sqrt(25.25), 5.024937810560445)
1538 self.ftest('sqrt(1)', math.sqrt(1), 1)
1539 self.ftest('sqrt(4)', math.sqrt(4), 2)
1540 self.assertEqual(math.sqrt(INF), INF)
1541 self.assertRaises(ValueError, math.sqrt, -1)
1542 self.assertRaises(ValueError, math.sqrt, NINF)
1543 self.assertTrue(math.isnan(math.sqrt(NAN)))
1545 def testTan(self):
1546 self.assertRaises(TypeError, math.tan)
1547 self.ftest('tan(0)', math.tan(0), 0)
1548 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
1549 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
1551 self.assertTrue(math.isnan(math.tan(INF)))
1552 self.assertTrue(math.isnan(math.tan(NINF)))
1554 self.assertRaises(ValueError, math.tan, INF)
1555 self.assertRaises(ValueError, math.tan, NINF)
1556 self.assertTrue(math.isnan(math.tan(NAN)))
1558 def testTanh(self):
1559 self.assertRaises(TypeError, math.tanh)
1560 self.ftest('tanh(0)', math.tanh(0), 0)
1561 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0,
1563 self.ftest('tanh(inf)', math.tanh(INF), 1)
1564 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
1565 self.assertTrue(math.isnan(math.tanh(NAN)))
1568 def testTanhSign(self):
1570 self.assertEqual(math.tanh(-0.), -0.)
1571 self.assertEqual(math.copysign(1., math.tanh(-0.)),
1574 def test_trunc(self):
1575 self.assertEqual(math.trunc(1), 1)
1576 self.assertEqual(math.trunc(-1), -1)
1577 self.assertEqual(type(math.trunc(1)), int)
1578 self.assertEqual(type(math.trunc(1.5)), int)
1579 self.assertEqual(math.trunc(1.5), 1)
1580 self.assertEqual(math.trunc(-1.5), -1)
1581 self.assertEqual(math.trunc(1.999999), 1)
1582 self.assertEqual(math.trunc(-1.999999), -1)
1583 self.assertEqual(math.trunc(-0.999999), -0)
1584 self.assertEqual(math.trunc(-100.999), -100)
1587 def __trunc__(self):
1590 def __trunc__(self):
1595 self.assertEqual(math.trunc(TestTrunc()), 23)
1596 self.assertEqual(math.trunc(FloatTrunc()), 23)
1598 self.assertRaises(TypeError, math.trunc)
1599 self.assertRaises(TypeError, math.trunc, 1, 2)
1600 self.assertRaises(TypeError, math.trunc, FloatLike(23.5))
1601 self.assertRaises(TypeError, math.trunc, TestNoTrunc())
1603 def testIsfinite(self):
1604 self.assertTrue(math.isfinite(0.0))
1605 self.assertTrue(math.isfinite(-0.0))
1606 self.assertTrue(math.isfinite(1.0))
1607 self.assertTrue(math.isfinite(-1.0))
1608 self.assertFalse(math.isfinite(float("nan")))
1609 self.assertFalse(math.isfinite(float("inf")))
1610 self.assertFalse(math.isfinite(float("-inf")))
1612 def testIsnan(self):
1613 self.assertTrue(math.isnan(float("nan")))
1614 self.assertTrue(math.isnan(float("-nan")))
1615 self.assertTrue(math.isnan(float("inf") * 0.))
1616 self.assertFalse(math.isnan(float("inf")))
1617 self.assertFalse(math.isnan(0.))
1618 self.assertFalse(math.isnan(1.))
1620 def testIsinf(self):
1621 self.assertTrue(math.isinf(float("inf")))
1622 self.assertTrue(math.isinf(float("-inf")))
1623 self.assertTrue(math.isinf(1E400))
1624 self.assertTrue(math.isinf(-1E400))
1625 self.assertFalse(math.isinf(float("nan")))
1626 self.assertFalse(math.isinf(0.))
1627 self.assertFalse(math.isinf(1.))
1630 def test_nan_constant(self):
1631 self.assertTrue(math.isnan(math.nan))
1634 def test_inf_constant(self):
1635 self.assertTrue(math.isinf(math.inf))
1636 self.assertGreater(math.inf, 0.0)
1637 self.assertEqual(math.inf, float("inf"))
1638 self.assertEqual(-math.inf, float("-inf"))
1646 def test_exceptions(self):
1652 self.fail("underflowing exp() should not have raised "
1655 self.fail("underflowing exp() should have returned 0")
1664 self.fail("overflowing exp() didn't trigger OverflowError")
1675 self.fail("sqrt(-1) didn't raise ValueError")
1678 def test_testfile(self):
1731 self.fail('Failures in test_testfile:\n ' +
1735 def test_mtestfile(self):
1798 self.fail('Failures in test_mtestfile:\n ' +
1801 def test_prod(self):
1803 self.assertEqual(prod([]), 1)
1804 self.assertEqual(prod([], start=5), 5)
1805 self.assertEqual(prod(list(range(2,8))), 5040)
1806 self.assertEqual(prod(iter(list(range(2,8)))), 5040)
1807 self.assertEqual(prod(range(1, 10), start=10), 3628800)
1809 self.assertEqual(prod([1, 2, 3, 4, 5]), 120)
1810 self.assertEqual(prod([1.0, 2.0, 3.0, 4.0, 5.0]), 120.0)
1811 self.assertEqual(prod([1, 2, 3, 4.0, 5.0]), 120.0)
1812 self.assertEqual(prod([1.0, 2.0, 3.0, 4, 5]), 120.0)
1815 self.assertEqual(prod([1, 1, 2**32, 1, 1]), 2**32)
1817 self.assertEqual(prod([1.0, 1.0, 2**32, 1, 1]), float(2**32))
1819 self.assertRaises(TypeError, prod)
1820 self.assertRaises(TypeError, prod, 42)
1821 self.assertRaises(TypeError, prod, ['a', 'b', 'c'])
1822 self.assertRaises(TypeError, prod, ['a', 'b', 'c'], start='')
1823 self.assertRaises(TypeError, prod, [b'a', b'c'], start=b'')
1825 self.assertRaises(TypeError, prod, values, start=bytearray(b''))
1826 self.assertRaises(TypeError, prod, [[1], [2], [3]])
1827 self.assertRaises(TypeError, prod, [{2:3}])
1828 self.assertRaises(TypeError, prod, [{2:3}]*2, start={2:3})
1829 self.assertRaises(TypeError, prod, [[1], [2], [3]], start=[])
1832 self.assertEqual(prod([2, 3], start='ab'), 'abababababab')
1833 self.assertEqual(prod([2, 3], start=[1, 2]), [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2])
1834 self.assertEqual(prod([], start={2: 3}), {2:3})
1836 with self.assertRaises(TypeError):
1839 self.assertEqual(prod([0, 1, 2, 3]), 0)
1840 self.assertEqual(prod([1, 0, 2, 3]), 0)
1841 self.assertEqual(prod([1, 2, 3, 0]), 0)
1851 self.assertEqual(prod(iterable), _naive_prod(iterable))
1853 self.assertEqual(prod(iterable), _naive_prod(iterable))
1855 self.assertEqual(prod(iterable), 0)
1860 self.assertEqual(prod(iterable), _naive_prod(iterable))
1862 self.assertEqual(prod(iterable), _naive_prod(iterable))
1864 self.assertIsNaN(prod(iterable))
1868 self.assertIsNaN(prod([1, 2, 3, float("nan"), 2, 3]))
1869 self.assertIsNaN(prod([1, 0, float("nan"), 2, 3]))
1870 self.assertIsNaN(prod([1, float("nan"), 0, 3]))
1871 self.assertIsNaN(prod([1, float("inf"), float("nan"),3]))
1872 self.assertIsNaN(prod([1, float("-inf"), float("nan"),3]))
1873 self.assertIsNaN(prod([1, float("nan"), float("inf"),3]))
1874 self.assertIsNaN(prod([1, float("nan"), float("-inf"),3]))
1876 self.assertEqual(prod([1, 2, 3, float('inf'),-3,4]), float('-inf'))
1877 self.assertEqual(prod([1, 2, 3, float('-inf'),-3,4]), float('inf'))
1879 self.assertIsNaN(prod([1,2,0,float('inf'), -3, 4]))
1880 self.assertIsNaN(prod([1,2,0,float('-inf'), -3, 4]))
1881 self.assertIsNaN(prod([1, 2, 3, float('inf'), -3, 0, 3]))
1882 self.assertIsNaN(prod([1, 2, 3, float('-inf'), -3, 0, 2]))
1886 self.assertEqual(type(prod([1, 2, 3, 4, 5, 6])), int)
1887 self.assertEqual(type(prod([1, 2.0, 3, 4, 5, 6])), float)
1888 self.assertEqual(type(prod(range(1, 10000))), int)
1889 self.assertEqual(type(prod(range(1, 10000), start=1.0)), float)
1890 self.assertEqual(type(prod([1, decimal.Decimal(2.0), 3, 4, 5, 6])),
1893 def testPerm(self):
1899 self.assertEqual(perm(n, k),
1905 self.assertEqual(perm(n, k), perm(n - 1, k - 1) * k + perm(n - 1, k))
1909 self.assertEqual(perm(n, 0), 1)
1910 self.assertEqual(perm(n, 1), n)
1911 self.assertEqual(perm(n, n), factorial(n))
1915 self.assertEqual(perm(n), factorial(n))
1916 self.assertEqual(perm(n, None), factorial(n))
1920 self.assertRaises(TypeError, perm, 10, 1.0)
1921 self.assertRaises(TypeError, perm, 10, decimal.Decimal(1.0))
1922 self.assertRaises(TypeError, perm, 10, "1")
1923 self.assertRaises(TypeError, perm, 10.0, 1)
1924 self.assertRaises(TypeError, perm, decimal.Decimal(10.0), 1)
1925 self.assertRaises(TypeError, perm, "10", 1)
1927 self.assertRaises(TypeError, perm)
1928 self.assertRaises(TypeError, perm, 10, 1, 3)
1929 self.assertRaises(TypeError, perm)
1932 self.assertRaises(ValueError, perm, -1, 1)
1933 self.assertRaises(ValueError, perm, -2**1000, 1)
1934 self.assertRaises(ValueError, perm, 1, -1)
1935 self.assertRaises(ValueError, perm, 1, -2**1000)
1938 self.assertEqual(perm(1, 2), 0)
1939 self.assertEqual(perm(1, 2**1000), 0)
1942 self.assertEqual(perm(n, 0), 1)
1943 self.assertEqual(perm(n, 1), n)
1944 self.assertEqual(perm(n, 2), n * (n-1))
1946 self.assertRaises(OverflowError, perm, n, n)
1949 self.assertEqual(perm(n, k), 1)
1950 self.assertIs(type(perm(n, k)), int)
1951 self.assertEqual(perm(IntSubclass(5), IntSubclass(2)), 20)
1952 self.assertEqual(perm(MyIndexable(5), MyIndexable(2)), 20)
1954 self.assertIs(type(perm(IntSubclass(5), IntSubclass(k))), int)
1955 self.assertIs(type(perm(MyIndexable(5), MyIndexable(k))), int)
1957 def testComb(self):
1963 self.assertEqual(comb(n, k), factorial(n)
1969 self.assertEqual(comb(n, k), comb(n - 1, k - 1) + comb(n - 1, k))
1973 self.assertEqual(comb(n, 0), 1)
1974 self.assertEqual(comb(n, n), 1)
1977 self.assertEqual(comb(n, 1), n)
1978 self.assertEqual(comb(n, n - 1), n)
1983 self.assertEqual(comb(n, k), comb(n, n - k))
1987 self.assertRaises(TypeError, comb, 10, 1.0)
1988 self.assertRaises(TypeError, comb, 10, decimal.Decimal(1.0))
1989 self.assertRaises(TypeError, comb, 10, "1")
1990 self.assertRaises(TypeError, comb, 10.0, 1)
1991 self.assertRaises(TypeError, comb, decimal.Decimal(10.0), 1)
1992 self.assertRaises(TypeError, comb, "10", 1)
1994 self.assertRaises(TypeError, comb, 10)
1995 self.assertRaises(TypeError, comb, 10, 1, 3)
1996 self.assertRaises(TypeError, comb)
1999 self.assertRaises(ValueError, comb, -1, 1)
2000 self.assertRaises(ValueError, comb, -2**1000, 1)
2001 self.assertRaises(ValueError, comb, 1, -1)
2002 self.assertRaises(ValueError, comb, 1, -2**1000)
2005 self.assertEqual(comb(1, 2), 0)
2006 self.assertEqual(comb(1, 2**1000), 0)
2009 self.assertEqual(comb(n, 0), 1)
2010 self.assertEqual(comb(n, 1), n)
2011 self.assertEqual(comb(n, 2), n * (n-1) // 2)
2012 self.assertEqual(comb(n, n), 1)
2013 self.assertEqual(comb(n, n-1), n)
2014 self.assertEqual(comb(n, n-2), n * (n-1) // 2)
2016 self.assertRaises(OverflowError, comb, n, n//2)
2019 self.assertEqual(comb(n, k), 1)
2020 self.assertIs(type(comb(n, k)), int)
2021 self.assertEqual(comb(IntSubclass(5), IntSubclass(2)), 10)
2022 self.assertEqual(comb(MyIndexable(5), MyIndexable(2)), 10)
2024 self.assertIs(type(comb(IntSubclass(5), IntSubclass(k))), int)
2025 self.assertIs(type(comb(MyIndexable(5), MyIndexable(k))), int)
2028 def test_nextafter(self):
2030 self.assertEqual(math.nextafter(4503599627370496.0, -INF),
2032 self.assertEqual(math.nextafter(4503599627370496.0, INF),
2034 self.assertEqual(math.nextafter(9223372036854775808.0, 0.0),
2036 self.assertEqual(math.nextafter(-9223372036854775808.0, 0.0),
2040 self.assertEqual(math.nextafter(1.0, -INF),
2042 self.assertEqual(math.nextafter(1.0, INF),
2046 self.assertEqual(math.nextafter(2.0, 2.0), 2.0)
2047 self.assertEqualSign(math.nextafter(-0.0, +0.0), +0.0)
2048 self.assertEqualSign(math.nextafter(+0.0, -0.0), -0.0)
2052 self.assertEqual(math.nextafter(+0.0, INF), smallest_subnormal)
2053 self.assertEqual(math.nextafter(-0.0, INF), smallest_subnormal)
2054 self.assertEqual(math.nextafter(+0.0, -INF), -smallest_subnormal)
2055 self.assertEqual(math.nextafter(-0.0, -INF), -smallest_subnormal)
2056 self.assertEqualSign(math.nextafter(smallest_subnormal, +0.0), +0.0)
2057 self.assertEqualSign(math.nextafter(-smallest_subnormal, +0.0), -0.0)
2058 self.assertEqualSign(math.nextafter(smallest_subnormal, -0.0), +0.0)
2059 self.assertEqualSign(math.nextafter(-smallest_subnormal, -0.0), -0.0)
2063 self.assertEqual(math.nextafter(INF, 0.0), largest_normal)
2064 self.assertEqual(math.nextafter(-INF, 0.0), -largest_normal)
2065 self.assertEqual(math.nextafter(largest_normal, INF), INF)
2066 self.assertEqual(math.nextafter(-largest_normal, -INF), -INF)
2069 self.assertIsNaN(math.nextafter(NAN, 1.0))
2070 self.assertIsNaN(math.nextafter(1.0, NAN))
2071 self.assertIsNaN(math.nextafter(NAN, NAN))
2074 def test_ulp(self):
2075 self.assertEqual(math.ulp(1.0), sys.float_info.epsilon)
2077 self.assertEqual(math.ulp(2 ** 52), 1.0)
2078 self.assertEqual(math.ulp(2 ** 53), 2.0)
2079 self.assertEqual(math.ulp(2 ** 64), 4096.0)
2082 self.assertEqual(math.ulp(0.0),
2084 self.assertEqual(math.ulp(FLOAT_MAX),
2088 self.assertEqual(math.ulp(INF), INF)
2089 self.assertIsNaN(math.ulp(math.nan))
2093 with self.subTest(x=x):
2094 self.assertEqual(math.ulp(-x), math.ulp(x))
2096 def test_issue39871(self):
2100 def __float__(self):
2101 self.converted = True
2105 with self.assertRaises(TypeError):
2110 self.assertFalse(getattr(y, "converted", False))
2114 def assertIsNaN(self, value):
2116 self.fail("Expected a NaN, got {!r}.".format(value))
2118 def assertEqualSign(self, x, y):
2123 self.assertEqual(x, y)
2124 self.assertEqual(math.copysign(1.0, x), math.copysign(1.0, y))
2130 def assertIsClose(self, a, b, *args, **kwargs):
2131 self.assertTrue(self.isclose(a, b, *args, **kwargs),
2134 def assertIsNotClose(self, a, b, *args, **kwargs):
2135 self.assertFalse(self.isclose(a, b, *args, **kwargs),
2138 def assertAllClose(self, examples, *args, **kwargs):
2140 self.assertIsClose(a, b, *args, **kwargs)
2142 def assertAllNotClose(self, examples, *args, **kwargs):
2144 self.assertIsNotClose(a, b, *args, **kwargs)
2146 def test_negative_tolerances(self):
2148 with self.assertRaises(ValueError):
2149 self.assertIsClose(1, 1, rel_tol=-1e-100)
2150 with self.assertRaises(ValueError):
2151 self.assertIsClose(1, 1, rel_tol=1e-100, abs_tol=-1e10)
2153 def test_identical(self):
2161 self.assertAllClose(identical_examples, rel_tol=0.0, abs_tol=0.0)
2163 def test_eight_decimal_places(self):
2168 self.assertAllClose(eight_decimal_places_examples, rel_tol=1e-8)
2169 self.assertAllNotClose(eight_decimal_places_examples, rel_tol=1e-9)
2171 def test_near_zero(self):
2177 self.assertAllNotClose(near_zero_examples, rel_tol=0.9)
2179 self.assertAllClose(near_zero_examples, abs_tol=1e-8)
2181 def test_identical_infinite(self):
2183 self.assertIsClose(INF, INF)
2184 self.assertIsClose(INF, INF, abs_tol=0.0)
2185 self.assertIsClose(NINF, NINF)
2186 self.assertIsClose(NINF, NINF, abs_tol=0.0)
2188 def test_inf_ninf_nan(self):
2201 self.assertAllNotClose(not_close_examples, abs_tol=0.999999999999999)
2203 def test_zero_tolerance(self):
2208 self.assertAllClose(zero_tolerance_close_examples, rel_tol=0.0)
2213 self.assertAllNotClose(zero_tolerance_not_close_examples, rel_tol=0.0)
2215 def test_asymmetry(self):
2217 self.assertAllClose([(9, 10), (10, 9)], rel_tol=0.1)
2219 def test_integers(self):
2224 self.assertAllClose(integer_examples, rel_tol=1e-8)
2225 self.assertAllNotClose(integer_examples, rel_tol=1e-9)
2227 def test_decimals(self):
2235 self.assertAllClose(decimal_examples, rel_tol=1e-8)
2236 self.assertAllNotClose(decimal_examples, rel_tol=1e-9)
2238 def test_fractions(self):
2246 self.assertAllClose(fraction_examples, rel_tol=1e-8)
2247 self.assertAllNotClose(fraction_examples, rel_tol=1e-9)