MCPcopy Index your code
hub / github.com/python/cpython / test_fma_overflow

Method test_fma_overflow

Lib/test/test_math.py:2746–2777  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2744 self.assertIsPositiveZero(math.fma(x-y, -(x+y), z))
2745
2746 def test_fma_overflow(self):
2747 a = b = float.fromhex('0x1p512')
2748 c = float.fromhex('0x1p1023')
2749 # Overflow from multiplication.
2750 with self.assertRaises(OverflowError):
2751 math.fma(a, b, 0.0)
2752 self.assertEqual(math.fma(a, b/2.0, 0.0), c)
2753 # Overflow from the addition.
2754 with self.assertRaises(OverflowError):
2755 math.fma(a, b/2.0, c)
2756 # No overflow, even though a*b overflows a float.
2757 self.assertEqual(math.fma(a, b, -c), c)
2758
2759 # Extreme case: a * b is exactly at the overflow boundary, so the
2760 # tiniest offset makes a difference between overflow and a finite
2761 # result.
2762 a = float.fromhex('0x1.ffffffc000000p+511')
2763 b = float.fromhex('0x1.0000002000000p+512')
2764 c = float.fromhex('0x0.0000000000001p-1022')
2765 with self.assertRaises(OverflowError):
2766 math.fma(a, b, 0.0)
2767 with self.assertRaises(OverflowError):
2768 math.fma(a, b, c)
2769 self.assertEqual(math.fma(a, b, -c),
2770 float.fromhex('0x1.fffffffffffffp+1023'))
2771
2772 # Another extreme case: here a*b is about as large as possible subject
2773 # to math.fma(a, b, c) being finite.
2774 a = float.fromhex('0x1.ae565943785f9p+512')
2775 b = float.fromhex('0x1.3094665de9db8p+512')
2776 c = float.fromhex('0x1.fffffffffffffp+1023')
2777 self.assertEqual(math.fma(a, b, -c), c)
2778
2779 def test_fma_single_round(self):
2780 a = float.fromhex('0x1p-50')

Callers

nothing calls this directly

Calls 3

assertRaisesMethod · 0.45
fmaMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected