All formulas

Numerical methods

Hypotenuse (overflow-safe)

num.hypotAlso called hypot, hypotenuse, pythagoras, norm2

hypot⁡(x,y)=x2+y2\operatorname{hypot}(x, y) = \sqrt{x^2 + y^2}

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

xx

first component

x ∈ ℝ

yy

second component

y ∈ ℝ

hypot⁡(x,y)\operatorname{hypot}(x, y)

Result: the length

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    x=3,y=4x = 3, y = 4

    Result

    hypot⁡(x,y)=5\displaystyle \operatorname{hypot}(x, y) = 5
  2. Given

    x=1,y=1x = 1, y = 1

    Result

    hypot⁡(x,y)=2\displaystyle \operatorname{hypot}(x, y) = \sqrt{2}

    ≈ 1.41421356237

  3. Given

    x=−5,y=12x = -5, y = 12

    Result

    hypot⁡(x,y)=13\displaystyle \operatorname{hypot}(x, y) = 13
  4. Given

    x=10200,y=10200x = 10^{200}, y = 10^{200}

    Result

    hypot⁡(x,y)=102002\displaystyle \operatorname{hypot}(x, y) = 10^{200} \sqrt{2}

    ≈ 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

    doi.org