Reference · Math

math.dist

signature
math.dist(list, list) -> float

What it does

The Euclidean distance between two points, each a list of numbers of the same length, as a float, as Python's math.dist. Ints and floats mix freely. As with math.hypot, the squares are never formed outright, so large coordinates do not overflow.

Examples

print(math.dist([0, 0], [3, 4]), math.dist([1, 2, 3], [4, 6, 3]))print(math.dist([1], [4]), math.dist([1.5, 2], [1, 2]))
output
5.0 5.03.0 0.5

Edge cases

print(math.dist([3, 4], [0, 0]), math.dist([3, 4], [3, 4]))print(math.dist([4], [1]))print(math.dist([], []), math.dist(null, null))print(math.isfinite(math.dist([1e200], [-1e200])), math.dist([math.inf], [0]))print(math.dist([math.nan, 1], [0, 0]), math.dist([math.nan], [0]))
output
5.0 0.03.00.0 0.0true infNaN NaN

Errors

Points of different lengths are a runtime error, not an error value: it stops the request, and ?? does not catch it. An item that is not a number, or an argument that is not a list, is a runtime error too.

this one fails
print(math.dist([1, 2], [1]))
output
line 1: `math.dist`: both points must have the same number of dimensions (2 and 1)
this one fails
print(math.dist([1, "2"], [1, 2]))
output
line 1: `math.dist` expects a list of numbers, but item 1 is str
this one fails
print(math.dist(1, 2))
output
line 1: `math.dist` expects a list as argument 1, got int

Chaining

In a pipeline the first point comes first. The nearest of several points is the smallest distance, or the point itself with sort_by:

let here = [1, 1]let points = [[5, 5], [2, 0], [1, 3]]print(here | math.dist([4, 5]))print(points | map((p) => math.dist(here, p)) | min)print(points | sort_by((p) => math.dist(here, p)) | first)
output
5.01.4142135623730951[2, 0]

See also

math.hypot, vec.norm, vec.sub, math.sqrt, sort_by

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

Thread 0 posts

On math.dist(): 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