How do we measure distance and similarity between vectors?

Created: Updated: 5 min read

Two vectors are measured as close or far apart using a distance or similarity function that turns their numerical difference into a single comparable number, and which specific function gets used changes what “nearest” actually means for a given dataset — Euclidean distance, cosine similarity, and inner product are the three that come up constantly in nearest-neighbor search, and they don’t always agree on which vector counts as closest.

What does it mean for two vectors to be far apart or similar in the first place?

A vector is just an ordered list of numbers, and geometrically it can be thought of as a point in space with one coordinate per number in the list — a 3-dimensional vector is a point in ordinary 3D space, and a 768-dimensional vector is a point in a space with 768 coordinates, which can’t be visualized directly but behaves according to the same underlying mathematics. A distance function takes two such points and returns a number that grows as the points get further apart, while a similarity function does the reverse, returning a larger number when two points are more alike. The two are often interchangeable in practice — a similarity score can usually be turned into a distance by negating or inverting it — but the choice of which underlying geometric relationship to measure is not interchangeable, since Euclidean distance, cosine similarity, and inner product each capture something different about how two vectors relate to each other.

What is Euclidean distance, and why is it the default choice?

Euclidean distance, often called L2 distance, measures the straight-line distance between two points exactly the way a ruler would in ordinary space: take the difference between the two vectors in each dimension, square each difference, add them all up, and take the square root of the total. This is the most direct generalization of everyday distance to many dimensions, and it’s the natural choice whenever the raw magnitude of a vector carries real meaning — two points that are close together in Euclidean distance are close together in every dimension simultaneously, not just pointed in a similar overall direction.

How does cosine similarity differ from measuring straight-line distance?

Cosine similarity ignores magnitude entirely and measures only the angle between two vectors, returning a value that’s largest when the vectors point in exactly the same direction and smallest when they point in opposite directions, regardless of how long either vector is. This distinction matters enormously for embeddings produced by machine learning models, where a vector’s direction typically encodes meaning while its length often reflects something incidental, like how much text went into producing it — two document embeddings pointing in nearly the same direction usually represent similar content even if one document was much longer than the other, and Euclidean distance would penalize that length difference in a way cosine similarity does not. Cosine distance is simply cosine similarity flipped into a distance-like number that decreases as vectors get more alike, and the two terms are often used loosely to mean the same underlying comparison.

What is inner product, and how does it relate to cosine similarity?

The inner product, also called the dot product, multiplies two vectors dimension by dimension and sums the results, and unlike cosine similarity it does not remove the effect of vector length — a longer vector pointing in a similar direction can produce a larger inner product than a shorter vector pointing in exactly the same direction. This makes inner product the right tool when magnitude is meaningful and larger really should mean “more” — recommendation systems, for instance, often want the raw score to reflect both how well a user’s preferences align with an item and how strongly that item is associated with those preferences overall, which is exactly what maximum inner-product search is built around. When every vector in a comparison happens to be normalized to the same length, inner product and cosine similarity rank vectors identically, which is why many systems normalize their vectors once and then use the cheaper inner-product calculation everywhere instead of computing a full cosine similarity for every comparison.

Why does it matter that the same distance function is used consistently?

An index such as HNSW is built around a specific notion of “close,” since the neighbor connections formed during construction are only meaningful under whichever distance function was used to create them — searching that same graph later with a different distance function means asking it to answer a question it wasn’t built to answer, and the results can degrade in ways that are easy to misdiagnose as a bug elsewhere in the system. This is a more common mistake in practice than it sounds: a team might build an index using raw inner product, then normalize vectors differently at query time, or mix up cosine distance and cosine similarity so that the system optimizes for the opposite of what was intended. Keeping the distance function, and any normalization step that feeds into it, identical between the moment an index is built and the moment it’s queried is one of the simplest and most overlooked requirements for getting correct results out of any nearest-neighbor system.

The next page on this site looks at why these otherwise sound distance functions start behaving strangely once the number of dimensions gets large, which is the specific problem that makes brute-force and simple tree-based search fall apart. If you want a fuller reference on the mechanics of any one distance function covered here, the vector-and-distance-math glossary section has a dedicated page for each — Euclidean distance, cosine similarity, and inner product among them — going deeper into the formulas and edge cases than this overview does.