MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_fma_overflow

Method test_fma_overflow

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

Source from the content-addressed store, hash-verified

2837 self.assertIsPositiveZero(math.fma(x-y, -(x+y), z))
2838
2839 def test_fma_overflow(self):
2840 a = b = float.fromhex('0x1p512')
2841 c = float.fromhex('0x1p1023')
2842 # Overflow from multiplication.
2843 with self.assertRaises(OverflowError):
2844 math.fma(a, b, 0.0)
2845 self.assertEqual(math.fma(a, b/2.0, 0.0), c)
2846 # Overflow from the addition.
2847 with self.assertRaises(OverflowError):
2848 math.fma(a, b/2.0, c)
2849 # No overflow, even though a*b overflows a float.
2850 self.assertEqual(math.fma(a, b, -c), c)
2851
2852 # Extreme case: a * b is exactly at the overflow boundary, so the
2853 # tiniest offset makes a difference between overflow and a finite
2854 # result.
2855 a = float.fromhex('0x1.ffffffc000000p+511')
2856 b = float.fromhex('0x1.0000002000000p+512')
2857 c = float.fromhex('0x0.0000000000001p-1022')
2858 with self.assertRaises(OverflowError):
2859 math.fma(a, b, 0.0)
2860 with self.assertRaises(OverflowError):
2861 math.fma(a, b, c)
2862 self.assertEqual(math.fma(a, b, -c),
2863 float.fromhex('0x1.fffffffffffffp+1023'))
2864
2865 # Another extreme case: here a*b is about as large as possible subject
2866 # to math.fma(a, b, c) being finite.
2867 a = float.fromhex('0x1.ae565943785f9p+512')
2868 b = float.fromhex('0x1.3094665de9db8p+512')
2869 c = float.fromhex('0x1.fffffffffffffp+1023')
2870 self.assertEqual(math.fma(a, b, -c), c)
2871
2872 def test_fma_single_round(self):
2873 a = float.fromhex('0x1p-50')

Callers

nothing calls this directly

Calls 4

fromhexMethod · 0.45
assertRaisesMethod · 0.45
fmaMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected