Reference · Math
math.hypot
math.hypot(num...) -> floatWhat 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))5.0 1.41421356237309513.0 13.0Edge cases
- Signs do not matter; one number gives its absolute value, and no numbers give
0.0. math.sqrt(x * x + y * y)overflows for1e200;math.hypotdoes not, and1e-200does not vanish either.- An infinity gives
inf, even next to aNaN. Otherwise aNaNgivesNaN, as in Python. - The numbers are separate arguments, not a list: for a list use vec.norm, and for two points math.dist.
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]))5.0 5.0 0.0true trueinf NaN NaN5.0 5.0Errors
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.
print(math.hypot([3, 4]))line 1: `math.hypot` expects a number as argument 1, got listprint(math.hypot(3, null))line 1: `math.hypot` expects a number as argument 2, got nullChaining
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])))5.0[5.0, 13.0]See also
math.dist, vec.norm, math.sqrt, math.atan2
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