Euclidean distance calculator
From [0, 0] to [3, 4] the distance is exactly 5, and from [0, 0, 0] to [1, 2, 2] it is 3. Enter two vectors of the same length, in any number of dimensions, and get the distance and its square. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- First text
- 0, 0
- Second text
- 3, 4
- Distance
- 5
- Squared distance
- 25
- Dimensions
- 2
The Euclidean distance calculator answers one precise question: given two vectors described by their numeric components, how far apart are they along a straight line in n-dimensional space? It applies the generalized Pythagorean formula, the square root of the sum of the squared per-component differences, after validating that both vectors have the same length and that every component is a finite number.
The Euclidean formula generalized to n dimensions
Name the first vector a and the second vector b, each a list of n numeric components. The straight-line separation between them is the hypotenuse of an n-dimensional right triangle: subtract the components pairwise, square each difference, add the squares, and take the square root of the total. In two dimensions this is exactly the classroom formula from analytic geometry; in three it measures distance in physical space; and in higher dimensions it is the default notion of closeness used throughout statistics, machine learning, and information retrieval.
The value is always non-negative, equals zero exactly when the two vectors coincide component by component, and is unchanged if you swap the two vectors, because each difference is squared. Classic sanity checks make the formula memorable: from [0, 0] to [3, 4] the distance is exactly 5, and from [0, 0, 0] to [1, 2, 2] it is exactly 3.
Because the computation uses only subtraction, multiplication, addition, and one square root, it runs in time linear in the number of components with no trigonometric tables, no iterative solvers, and no external libraries.
What you can do with it
Embedding similarity checks
Turn two equal-length embedding vectors into a distance (or squared distance) for ranking, dedup, and anomaly demos.
Physics and engineering displacement
Compute straight-line displacement magnitudes in 2D or 3D from coordinate components with strict input validation.
FAQ
What formula does this calculator use?
The generalized Euclidean formula: distance = √(Σ (b[i] − a[i])²) over all components. The response includes method: "euclidean" to confirm the metric.
What happens if the vectors have different lengths?
Mismatched vectors are never truncated or padded silently.
What happens if a component is not a finite number?
Missing vectors, empty arrays, non-numeric text, NaN, and Infinity are all rejected before any arithmetic runs.
Does the order of the two vectors matter?
No. Distance is symmetric: swapping a and b yields the same non-negative value, because each per-component difference is squared.
Can I compare vectors with more than three components?
Yes. Both vectors can have any number of components as long as the count matches. The rule is the same in every dimension: subtract component by component, square each difference, add them up and take the square root.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/math/euclidean-distance \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"a":[0,0,0],"b":[3,4,0]}'const res = await fetch("https://api.kit.forhosting.com/math/euclidean-distance", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"a": [
0,
0,
0
],
"b": [
3,
4,
0
]
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/math/euclidean-distance",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"a": [
0,
0,
0
],
"b": [
3,
4,
0
]
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/math/euclidean-distance", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"a":[0,0,0],"b":[3,4,0]}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"a":[0,0,0],"b":[3,4,0]}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/math/euclidean-distance", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"a": [
0,
0,
0
],
"b": [
3,
4,
0
]
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "math.euclidean_distance",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
The response returns the non-negative distance together with the squared distance, the shared dimension count, the normalized component arrays, and a method tag of euclidean so automated consumers can document which metric was used. Vectors of different lengths, missing fields, non-numeric text, NaN, and Infinity are all rejected with a clear invalid_input error rather than a silent NaN that masquerades as a valid answer. The same pure module powers the free browser tool and the paid API, so results are byte-identical in both places. There are no network calls, no random sampling, and no hidden unit conversions: just deterministic IEEE-754 arithmetic on the numbers you provide, at $0.002 per successful API item.
The Euclidean formula generalized to n dimensions
Extremely large components can still overflow double-precision range when squared; in that case the request fails with a clear overflow message instead of returning Infinity in JSON.
Inputs, validation, and the shape of the result
Send two required vectors, a and b, as arrays of finite numbers with the same number of components. Once both vectors pass, the service accumulates the sum of squared differences, guards that the total stayed finite, and takes the square root. Numeric strings such as "2.5" inside the arrays are accepted and normalized, and comma-separated text is tolerated as a convenience for form paste-ins, but JSON arrays of numbers are the canonical contract. Validation runs before any arithmetic: a missing vector, an empty array, a non-numeric component, NaN, or Infinity all fail with an invalid_input error that names the offending position, and failed validation is not charged. The length check is strict and explicit: if a has three components and b has two, the request fails with a message stating both lengths, because silently truncating or padding vectors produces distances that look plausible and are wrong. The JSON response returns distance rounded to twelve decimal places for stable serialization, squared_distance for algorithms that only need ordering and can skip the root, the shared dimensions count, echoes of the normalized a and b arrays, and method set to the string euclidean for logging and documentation. API usage costs $0.002 per successful item.
Where vector Euclidean distance earns its keep
Euclidean distance between arbitrary vectors is the workhorse metric anywhere entities are described by feature lists. In search and recommendation work, embeddings of documents, images, or products are long numeric vectors; the distance between two of them quantifies how dissimilar the items are, and the squared_distance field lets ranking code avoid a square root when only ordering matters. Because the capability is a pure parse engine, there is no randomness and no clock dependence, so the same input always produces the same output in the browser and in the API. If you need Manhattan, cosine, or Minkowski metrics, or geographic great-circle distance on the globe, those are different intents served by different tools; this one stays deliberately focused on the straight-line Euclidean case between two equal-length vectors at $0.002 per item.
Integration use cases
ML and data-science fixtures
Validate k-NN, k-means, or nearest-neighbor code against a deterministic Euclidean distance between two feature vectors.
Technical questions
What does it cost?
The browser tool is free. API usage costs $0.002 per successful item; requests that fail validation are not charged.