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

Method testHypot

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

Source from the content-addressed store, hash-verified

839 self.assertEqual(gcd(MyIndexable(120), MyIndexable(84)), 12)
840
841 def testHypot(self):
842 from decimal import Decimal
843 from fractions import Fraction
844
845 hypot = math.hypot
846
847 # Test different numbers of arguments (from zero to five)
848 # against a straightforward pure python implementation
849 args = math.e, math.pi, math.sqrt(2.0), math.gamma(3.5), math.sin(2.1)
850 for i in range(len(args)+1):
851 self.assertAlmostEqual(
852 hypot(*args[:i]),
853 math.sqrt(sum(s**2 for s in args[:i]))
854 )
855
856 # Test allowable types (those with __float__)
857 self.assertEqual(hypot(12.0, 5.0), 13.0)
858 self.assertEqual(hypot(12, 5), 13)
859 self.assertEqual(hypot(0.75, -1), 1.25)
860 self.assertEqual(hypot(-1, 0.75), 1.25)
861 self.assertEqual(hypot(0.75, FloatLike(-1.)), 1.25)
862 self.assertEqual(hypot(FloatLike(-1.), 0.75), 1.25)
863 self.assertEqual(hypot(Decimal(12), Decimal(5)), 13)
864 self.assertEqual(hypot(Fraction(12, 32), Fraction(5, 32)), Fraction(13, 32))
865 self.assertEqual(hypot(True, False, True, True, True), 2.0)
866
867 # Test corner cases
868 self.assertEqual(hypot(0.0, 0.0), 0.0) # Max input is zero
869 self.assertEqual(hypot(-10.5), 10.5) # Negative input
870 self.assertEqual(hypot(), 0.0) # Negative input
871 self.assertEqual(1.0,
872 math.copysign(1.0, hypot(-0.0)) # Convert negative zero to positive zero
873 )
874 self.assertEqual( # Handling of moving max to the end
875 hypot(1.5, 1.5, 0.5),
876 hypot(1.5, 0.5, 1.5),
877 )
878
879 # Test handling of bad arguments
880 with self.assertRaises(TypeError): # Reject keyword args
881 hypot(x=1)
882 with self.assertRaises(TypeError): # Reject values without __float__
883 hypot(1.1, 'string', 2.2)
884 int_too_big_for_float = 10 ** (sys.float_info.max_10_exp + 5)
885 with self.assertRaises((ValueError, OverflowError)):
886 hypot(1, int_too_big_for_float)
887
888 # Any infinity gives positive infinity.
889 self.assertEqual(hypot(INF), INF)
890 self.assertEqual(hypot(0, INF), INF)
891 self.assertEqual(hypot(10, INF), INF)
892 self.assertEqual(hypot(-10, INF), INF)
893 self.assertEqual(hypot(NAN, INF), INF)
894 self.assertEqual(hypot(INF, NAN), INF)
895 self.assertEqual(hypot(NINF, NAN), INF)
896 self.assertEqual(hypot(NAN, NINF), INF)
897 self.assertEqual(hypot(-INF, INF), INF)
898 self.assertEqual(hypot(-INF, -INF), INF)

Callers

nothing calls this directly

Calls 8

DecimalClass · 0.90
FractionClass · 0.90
assertTrueMethod · 0.80
FloatLikeClass · 0.70
sqrtMethod · 0.45
assertAlmostEqualMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected