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.
Code
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: TrueHow 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
GCD & LCM CalculatorFind the greatest common divisor and least common multiple of two or more integers, computed exactly with BigInt.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.SVG Path VisualizerParse SVG path d attributes (M/L/H/V/C/S/Q/T/A/Z), render a scaled wireframe preview with vertices and control handles, and compute bounding boxes.Vector Similarity CalculatorCompute Cosine Similarity, Dot Product, Euclidean L2, Manhattan L1, and Chebyshev distances between numeric vectors with dimension checks.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.