Python math.hypot() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.hypot(*coordinates) returns the Euclidean norm of its arguments: sqrt(x1*x1 + x2*x2 + ...), which is the hypotenuse of a right triangle in 2D.

Unlike writing math.sqrt(x*x + y*y) by hand, math.hypot() scales intermediate values so huge inputs do not cause spurious OverflowError. Accepting zero, one, or three or more coordinates was added in Python 3.8.

math.hypot() is a function in Python's standard math module: import it with import math and call math.hypot(...), or import the name directly with from math import hypot.

Syntax

Import math and pass two coordinates (or any count in Python 3.8+).

python
import math

length = math.hypot(x, y)
norm3d = math.hypot(x, y, z)
CallWhat it does
math.hypot(3, 4)Returns 5.0.
math.hypot(2, 3, 6)Returns 7.0 (Python 3.8+ 3D norm).
math.hypot()Returns 0.0 (Python 3.8+).
math.hypot(float("inf"), float("nan"))Returns inf.

To find the distance between two points p and q instead of from the origin, use math.dist() (Python 3.8+).

Function signatures
python
math.hypot(*coordinates)
# returns: float sqrt(sum(x*x for x in coordinates))
# 2 args in all versions; 0, 1, or 3+ args since Python 3.8

Parameters

ParameterDescription
*coordinatesZero or more real numbers (int or float). Prior to Python 3.8, exactly two arguments were required.

Return Value

Return ValueDescription
floatEuclidean length. Returns inf if any argument is inf or -inf (even if another is nan).

Python Examples

Example 1 — Measure 2D Right Triangles and 3D Vector Lengths

Find exact integer-triple lengths in 2D and 3D using math.hypot() (3+ args need Python 3.8+).

python
import math

print("3-4-5:", math.hypot(3, 4))
print("5-12-13:", math.hypot(5, 12))
print("3D (2,3,6):", math.hypot(2, 3, 6))
print("empty:", math.hypot())

How It Works

  1. Classic right triangles. Passing legs (3, 4) and (5, 12) produces exact float hypotenuses 5.0 and 13.0.
  2. 3D norm in 3.8+. Supplying three coordinates computes sqrt(4 + 9 + 36) = 7.0 directly.
  3. Zero arguments. With no coordinates, math.hypot() returns 0.0.

Example 2 — Avoid Intermediate Overflow on Huge Coordinates

See math.sqrt(x*x + y*y) fail at 1e200 while math.hypot(x, y) succeeds.

python
import math

x, y = 1e200, 1e200
try:
    print(math.sqrt(x * x + y * y))
except OverflowError:
    print("squaring 1e200 directly overflows")

print(f"hypot: {math.hypot(x, y):.6e}")

How It Works

  1. Direct squaring overflows. Multiplying 1e200 * 1e200 exceeds the float maximum (~1.8e308) and produces infinity or overflow.
  2. Safe internal scaling. math.hypot() divides out the largest magnitude before squaring, then scales back at the end.
  3. Clean scientific output. Formatting with :.6e displays sqrt(2) * 1e200 accurately.

Example 3 — Normalize a Game Velocity Vector and Test Inf + NaN

Divide velocity components by math.hypot() to get unit direction, and check IEEE special rules.

python
import math

vx, vy = 6.0, 8.0
speed = math.hypot(vx, vy)
print(speed, vx / speed, vy / speed)
print(math.hypot(float("inf"), float("nan")))

How It Works

  1. Vector speed. A 6-8-10 velocity triangle has speed 10.0.
  2. Unit direction vector. Dividing both components by speed normalizes the vector to (0.6, 0.8).
  3. Inf beats NaN. By IEEE 754 definition, if any coordinate is infinite, math.hypot() returns inf even alongside nan.

Frequently Asked Questions

Since Python 3.8 it accepts zero, one, two, or any number of arguments. On Python 3.7 and older it took only two arguments.
math.hypot() rescales inputs internally so very large or tiny numbers do not overflow to inf or underflow to zero during squaring.
Use math.dist((x1, y1), (x2, y2)) in Python 3.8+, or math.hypot(x2 - x1, y2 - y1).
Yes, unpack it with a star: math.hypot(*coords).
The Euclidean distance with an infinite component is always positive infinity under IEEE 754 rules, regardless of any NaN components.

Next: math.isclose()

Return True if a and b are close within tolerances (Python 3.5+).

isclose() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful