Part I ยท Ch. 2 โ€” Distance and Angle

Part I ยท Chapter 2 of 4

Distance and Angle

Two rulers for the same pair of points, and how to choose between them


Which of these two is closer?

We left the last chapter with an instrument that mixes two things together. The dot product is alignment multiplied by length multiplied by length, so a big score can mean "these point the same way" or it can mean "one of these is enormous," and from the number alone you cannot tell which.

That is a real problem, because the question a working system asks all day is the closeness question. Which stored memory is nearest this one? Which document best matches this query? Which of last week's sentences most resembles the sentence he just said? To answer it we need to pull length and direction apart, measure each on its own, and then be deliberate about which one we are using. There are two rulers. This chapter is about both, and about what happens when you put everything on the same sphere so that only one of them is left.

The first ruler: the gap between two points

The most ordinary answer to "how far apart" is the one a tape measure gives: the straight-line gap. To get it, walk from one point to the other and measure the trip.

The trip from u to v is the difference vector $\mathbf{v} - \mathbf{u}$, and its length is the distance we want. Length comes from Pythagoras: square each entry, add, take the square root.

$$\|\mathbf{v} - \mathbf{u}\| = \sqrt{(v_1 - u_1)^2 + (v_2 - u_2)^2}$$

In words: subtract matching entries to get the trip from one point to the other, square each of those differences, add them up, and take the square root. That is the straight-line distance โ€” what people mean by Euclidean distance.

The gap, by hand

Our running pair is still $\mathbf{v} = [3, 2]$ and $\mathbf{u} = [1, 3]$. Subtract entry by entry: $3 - 1 = 2$ across, and $2 - 3 = -1$ up, so the trip from u to v is $[2, -1]$ โ€” two to the right and one down.

Now its length: $2^2 + (-1)^2 = 4 + 1 = 5$, and the square root of 5 is about 2.24.

Notice that the minus sign vanished when we squared it. Distance does not care which of the two points you started from; the trip the other way, $[-2, 1]$, has exactly the same length.

In words: these two points sit about 2.24 units apart, and that number would be the same if we had measured from v to u instead.

This ruler is honest, familiar and often exactly right. It is also, for most of what models do, the wrong one โ€” and the reason why takes one more figure.

The second ruler: the angle between two headings

Two square panels, each a plane with a faint grid. Left panel, headed 'u, as it came': a mint arrow labeled v to 3 comma 2, an amber arrow labeled u to 1 comma 3, a dashed segment between their tips marked 'gap 2.24', and an arc at the origin marked 38 degrees. Right panel, headed 'u, stretched three times', drawn at a smaller scale: the same mint arrow v, an amber arrow labeled 3u to 3 comma 9 lying along the identical direction, a dashed segment between the tips marked 'gap 7.00', and the same 38 degree arc.
Two rulers disagree, and the disagreement is length. Stretching u to three times its size more than triples the gap while leaving the angle untouched. The two panels are drawn at different scales; the 38 degrees is the same 38 degrees.

Take u and stretch it to three times its length: $3\mathbf{u} = [3, 9]$. It has not turned at all โ€” it lies along the very same line, as the figure shows. But the gap from v has gone from 2.24 to 7.00, because $\mathbf{v} - 3\mathbf{u} = [0, -7]$.

By the first ruler, v and u were close and v and 3u are distant. By any sensible reading of the picture, u and 3u are the same direction and should score identically against v. So we need the second ruler, and we already have it. Rearrange the shadow formula from the last chapter to put the angle on its own:

$$\cos\theta = \frac{\mathbf{v} \cdot \mathbf{u}}{\|\mathbf{v}\| \, \|\mathbf{u}\|}$$

In words: take the dot product and divide out both lengths. What is left is pure alignment โ€” a number from +1 for identical headings, through 0 at a right angle, to โˆ’1 for dead opposite. This is called cosine similarity.

The heading, by hand โ€” and what stretching does to it

We know $\mathbf{v} \cdot \mathbf{u} = 9$ from the last chapter, with $\|\mathbf{v}\| = \sqrt{13} \approx 3.61$ and $\|\mathbf{u}\| = \sqrt{10} \approx 3.16$. So the alignment score is $9 \div (3.61 \times 3.16) \approx 0.79$.

Now do it again with the stretched arrow. The dot product triples: $\mathbf{v} \cdot 3\mathbf{u} = 3 \times 3 + 2 \times 9 = 9 + 18 = 27$. But so does the length: $\|3\mathbf{u}\| = \sqrt{9 + 81} = \sqrt{90} \approx 9.49$, which is $3 \times 3.16$. Divide: $27 \div (3.61 \times 9.49) \approx 0.79$.

In words: exactly the same 0.79. The factor of three appeared on the top and on the bottom and cancelled itself out. That cancellation is the whole point of dividing by the lengths.

So we have a genuine choice, and it is not a matter of taste. It is a question about the data: does length carry information here, or is it noise?

In a great many systems, length is noise. A long document and a short one about the same subject produce vectors of different sizes for reasons that have nothing to do with subject. A word that appears constantly gets a bigger vector than a rare one, and "common" is not "meaningful." In those cases the length is telling you about the container, not the contents, and dividing it out is not a trick โ€” it is removing a known distortion.

In other systems length is exactly the signal. Inside an attention head, a token that should dominate the sentence gets to shout, and the raw dot product lets it. Strip the length out there and you have thrown away the model's way of saying "this one matters more."

Put everyone on the same sphere

There is a cleaner way to think about dividing out the length, and it is the picture this whole book runs on. Instead of adjusting the score afterward, move the points first.

To normalize a vector, divide it by its own length. The result is a unit vector โ€” the hat notation from the last chapter โ€” pointing exactly the same way, with the length set to 1.

$$\hat{\mathbf{v}} = \frac{\mathbf{v}}{\|\mathbf{v}\|}$$

In words: divide every entry of v by v's own length. The arrow keeps its heading and loses its size, which parks its tip on the circle of radius 1 around the origin.

Two panels side by side, each with a faint dashed circle of radius one around the origin. Left panel, headed 'before': five arrows from the origin at assorted lengths and headings, three of them leaning the same way at three different lengths, annotated 'same heading, three lengths'. Right panel, headed 'after': the same five arrows now all stopping exactly on the circle, with the three that shared a heading collapsed onto one marked point, annotated 'three arrows, one point'.
Normalizing is a move, not a formula. Every point slides along its own heading until it lands on the circle. Three arrows that differed only in size become one point.

Normalizing our pair

$\|\mathbf{v}\| = \sqrt{13} \approx 3.606$, so $\hat{\mathbf{v}} = [3 \div 3.606,\; 2 \div 3.606] \approx [0.832,\; 0.555]$. Check it: $0.832^2 + 0.555^2 \approx 0.692 + 0.308 = 1.000$. Length 1, as promised.

Likewise $\|\mathbf{u}\| = \sqrt{10} \approx 3.162$, so $\hat{\mathbf{u}} \approx [0.316,\; 0.949]$.

And the dot product of the two unit arrows: $0.832 \times 0.316 + 0.555 \times 0.949 \approx 0.263 + 0.527 = 0.790$ โ€” the alignment score itself, arriving with no division left to do.

In words: once both arrows sit on the circle, the plain dot product is the alignment score. Normalizing does the dividing once, up front, instead of every time you compare.

That last line is why real systems store normalized vectors. A search index holding a million passages can normalize all of them once, when they are written, and then answer every future query with a bare dot product โ€” the cheapest operation a computer owns.

On the sphere, the two rulers agree

Here is the fact that ties the chapter together, and it is the one most people who use these tools have never been shown. Once both points sit on the unit circle, the two rulers give the same ranking. Not similar rankings โ€” the same one, always.

Work out the squared gap between two unit arrows and watch what survives:

$$\|\hat{\mathbf{v}} - \hat{\mathbf{u}}\|^2 = 2 - 2\cos\theta$$

In words: the squared straight-line distance between two points on the unit circle is 2 minus twice their alignment score. Distance and alignment are not two ideas โ€” they are one quantity, read with opposite signs.

Three checkpoints, all hand-checkable

Take $\hat{\mathbf{a}} = [1, 0]$ and swing a partner around the circle.

Same heading, partner $[1, 0]$: alignment is 1, so the formula says the squared gap is $2 - 2 = 0$. And indeed the two points are the same point, gap 0.

Right angle, partner $[0, 1]$: alignment is 0, so the squared gap is $2 - 0 = 2$, and the gap is the square root of 2, about 1.41. Check it directly โ€” the trip between them is $[-1, 1]$, whose length is $\sqrt{1 + 1} = \sqrt{2}$. It matches.

Dead opposite, partner $[-1, 0]$: alignment is โˆ’1, so the squared gap is $2 + 2 = 4$ and the gap is 2 โ€” straight across a circle of radius 1, which is the diameter. It matches.

In words: as the alignment score slides from +1 down to โˆ’1, the gap climbs steadily from 0 up to 2, and it never once doubles back. Rank by one and you have ranked by the other.

A mint curve on axes labeled 'angle between the two headings' from 0 to 180 degrees along the bottom and 'straight-line gap on the unit circle' from 0 to 2 up the side. The curve rises from 0 at 0 degrees, through 1.41 at 90 degrees, to exactly 2 at 180 degrees, climbing the whole way without ever falling. Amber points mark those three checkpoints, and a note reads 'the climb never doubles back, so one ranking serves both rulers'.
One climbing curve, so one ranking. On the unit circle, a wider angle always means a bigger gap, so sorting by either ruler returns the same order.

See it move

Use the scale mode below to do the experiment from this chapter with your own hands. Drag the amber arrow to a heading you like, then run the scale slider up and down. Watch the list of numbers change while the heading refuses to.

Where this shows up

  • Every vector database. When a system stores embeddings for later search, it almost always normalizes them on the way in. That single decision is what lets the search itself be nothing but dot products. RAG and Vector Search in the LLM book builds the retrieval loop on top of exactly this.
  • Drift detection. "Something has changed in how this person writes" is a distance between two averaged directions, measured over time. If the vectors were not normalized, the measurement would mostly report that he wrote longer emails this month.
  • Deduplication. "Have I already been told this?" is a threshold on one of these two rulers. Because they rank identically on the sphere, a team can argue about which one they are using and still ship the same behavior.
  • Clustering. Algorithms that group points, like the k-means routine in the AI/ML book's classical toolbox, are built on a distance. Run them on unnormalized text vectors and the first cluster you find is often just "the long ones."

All four of those live or die on distances measured in a space with many more than two dimensions. We have been drawing on a page because a page is checkable. The next chapter is about what stops being true when we leave it.

What you now know

  • Euclidean distance measures the straight-line gap between two points, by subtracting matching entries, squaring, adding and taking the square root.
  • Cosine similarity measures only the angle between two headings, because dividing by both lengths cancels any stretching.
  • Choosing between them is really one question about your data: does magnitude carry information, or is it an artifact of the container?
  • Normalizing divides a vector by its own length, parking every point on the unit sphere, after which a plain dot product is already the alignment score.
  • On that sphere the squared gap equals 2 minus twice the alignment score, so the two rulers produce the same ranking โ€” and normalizing permanently destroys whatever the magnitudes were carrying.

Where we're headed

Every picture so far has been drawn on a flat page, and every intuition you have built came from looking at one. Real models work in hundreds or thousands of dimensions, and the honest news is that the page lies about what happens there. The next chapter runs the experiment โ€” the same random directions, sampled at 2, 10, 100 and 1000 dimensions โ€” and watches the answer change shape in front of us. It is the chapter most explanations skip, and it is the one that turns a user of these tools into someone who can invent with them.