Reference · Math

math.hypot

signature
math.hypot(num...) -> float

What it does

The Euclidean length of a vector given as separate numbers: the square root of the sum of their squares, as a float, as Python's math.hypot. With two numbers it is the hypotenuse of a right triangle; with more, the length in that many dimensions. The squares are never formed outright, so very large and very small numbers neither overflow nor underflow.

Examples

print(math.hypot(3, 4), math.hypot(1, 1))print(math.hypot(1, 2, 2), math.hypot(3, 4, 12))
output
5.0 1.41421356237309513.0 13.0

Edge cases

print(math.hypot(-3, -4), math.hypot(-5), math.hypot())print(math.isfinite(math.hypot(1e200, 1e200)), math.hypot(1e-200, 1e-200) > 0)print(math.hypot(math.inf, math.nan), math.hypot(math.nan, 1), math.hypot(math.nan, 0))print(vec.norm([3, 4]), math.dist([0, 0], [3, 4]))
output
5.0 5.0 0.0true trueinf NaN NaN5.0 5.0

Errors

There is no domain error. An argument that is not a number, a list included, is a runtime error, not an error value: it stops the request.

this one fails
print(math.hypot([3, 4]))
output
line 1: `math.hypot` expects a number as argument 1, got list
this one fails
print(math.hypot(3, null))
output
line 1: `math.hypot` expects a number as argument 2, got null

Chaining

In a pipeline the first number comes first, so x | math.hypot(y) is the length of (x, y):

print(3 | math.hypot(4))print([[3, 4], [5, 12]] | map((p) => math.hypot(p[0], p[1])))
output
5.0[5.0, 13.0]

See also

math.dist, vec.norm, math.sqrt, math.atan2

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On math.hypot(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo