hypot And dist Measure Straight Distances

math.hypot computes Euclidean length from components and math.dist measures the gap between two points, both in n dimensions.

PythonData Integrity

Code

pythonsnippet
16 lines
import math

# hypot computes sqrt(x*x + y*y + ...) without overflow for big components
print("hypot 2d:", math.hypot(3, 4))
print("hypot 3d:", math.hypot(1, 2, 2))

# dist measures the straight-line distance between two points
p1 = (2, 3)
p2 = (5, 7)
print("dist 2d:", math.dist(p1, p2))

# dist also accepts higher-dimensional points of equal length
print("dist 3d:", math.dist((0, 0, 0), (1, 2, 2)))

# dist(a, b) equals hypot of the component differences
print("match:", math.dist(p1, p2) == math.hypot(5 - 2, 7 - 3))

Output

Captured from a sandboxed run of this snippet in the build pipeline.

hypot 2d: 5.0
hypot 3d: 3.0
dist 2d: 5.0
dist 3d: 3.0
match: True

How it works

The file’s state is a handful of top-level bindings: p1, p2.

Caveats

  • Self-contained — imports go no further than what the runtime provides.

Applying it

The file’s output announces each step as it happens: hypot 2d:, hypot 3d:, dist 2d:.

Interactive Tools for This Topic

FAQ

Are there third-party dependencies?
No. The dependency list is empty; the code stays inside the standard library. Its state lives in module-level bindings: `p1`, `p2`.