Numerical methods
Hypotenuse (overflow-safe)
num.hypotAlso called hypot, hypotenuse, pythagoras, norm2
Length of the vector (x, y). Squaring first overflows once |x| or |y| exceeds about 1.3e154; scaling by the larger magnitude, as math.hypot and numpy.hypot do, avoids it.
Notation
first component
x ∈ ℝ
second component
y ∈ ℝ
Result: the length
scalar
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
Given
Result
≈ 1.41421356237
Given
Result
Given
Result
≈ 1.4142135623730998e+200
Common mistakes
Squares before scaling
x2 + y2 overflows to inf in float64 for magnitudes above about 1.3e154.
Fix: Scale by the larger magnitude first, or call math.hypot / numpy.hypot.
Source
Higham 2002
Higham, Nicholas J. Accuracy and Stability of Numerical Algorithms. 2nd ed. SIAM, 2002. doi:10.1137/1.9780898718027