Skip to content
OneKitly

Compare Squared Distances and Skip the Square Root

Published 11/3/2026 · 3 min read · Developer tools

Daniel Okonkwo

Daniel Okonkwo — Front-end developer and tech writer at OneKitly

Web performance · File formats

Checked against 2 sources

View profile →
In short

A vector's magnitude is the square root of the sum of its squared components, which is Pythagoras extended to as many dimensions as you like. Some inputs land on whole numbers: (3, 4, 0) gives 5, (5, 12, 0) gives 13, and (3, 4, 12) gives exactly 13 in three dimensions. Most do not — (1, 1, 0) gives 1.41421 and (1, 1, 1) gives 1.73205, both irrational. The practical point is that the square root is often unnecessary: if you are asking which of two points is closer, comparing the sums of squares gives the same answer, because squaring preserves order among non-negative numbers.

Advertisement

The vector (3, 4, 12) has a magnitude of exactly 13. When you only need to know which of two points is nearer, the square root is a step you can leave out entirely.

The cheapest optimisation in graphics code

Collision checks, nearest-neighbour searches and visibility culling all ask comparison questions rather than measurement questions, and none of them need the actual distance. Testing whether two circles overlap means comparing the squared distance between their centres against the squared sum of their radii — no roots anywhere. On modern hardware a square root is no longer the catastrophe it was in the 1990s, but it remains several times the cost of a multiply, and these tests run millions of times per frame. The habit survives because the saving is free: the code is not harder to read, and the comparison is exact rather than subject to rounding.

When you genuinely need the root

Whenever the distance is added to something, reported to a user, or used to scale another quantity. Squared distances cannot be summed to give a path length, cannot be compared against a threshold expressed in metres, and cannot be normalised into a direction. The rule that separates the two cases is whether the number leaves the comparison: if it is only ever weighed against another distance, keep it squared; the moment it participates in arithmetic with anything that is not also a squared distance, take the root. Mixing the two conventions in one codebase without saying which is which is a reliable source of bugs that only appear at particular scales.

Magnitude
Vectors that land on whole numbers, and ones that do not
VectorMagnitudeSum of squares
(3, 4, 0)525
(6, 8, 0)10100
(3, 4, 12)13169
(1, 1, 0)1.414212
(1, 1, 1)1.732053

Worked with our own calculator

Vector magnitude calculator

Given

x
6
y
8
z
5

Result

Magnitude
11.18

These figures are produced by the calculator below, not typed in by hand — they are recomputed whenever the tool changes.

Run it on your own figures →
Advertisement

Frequently asked questions

Why does (3, 4, 12) come out exactly 13?
Because 9 plus 16 plus 144 is 169, and 169 is 13 squared. It is one of the smallest Pythagorean quadruples, the three-dimensional cousin of the 3-4-5 triangle, and it works because the first two components already form a 3-4-5 with a hypotenuse of 5, which then pairs with 12 to make the 5-12-13 triangle. Stacking known triples this way generates infinitely many quadruples, which is why examples that land on whole numbers are easy to construct once you know one — and vanishingly rare if you pick components at random.
Does the formula change in more than three dimensions?
Not at all — you add one more square inside the root for each dimension, and everything else is identical. That indifference to dimension count is what makes the same arithmetic serve a physics simulation with three coordinates and a recommendation system comparing vectors with a thousand. What does change with dimension is behaviour rather than formula: in high dimensions almost all randomly chosen vectors have similar magnitudes and are nearly perpendicular to one another, an effect that makes distance a much weaker signal there than intuition from three dimensions suggests.

Articles you may find interesting

All guides →

Related tools

Sources

Spotted a mistake in this article?