ZestCalc

Cosine Similarity Calculator

Compare vectors by the angle between them, the way search engines and embedding models do. Exact cosines like √2/2, every pair at once, and a picture of why length doesn't count.

Vectors

Enter two to eight vectors with the same number of entries, such as embeddings or word counts, or start from an example.

Entries2
a=a =
b=b =

Similarity

Updates as you type.

cos⁡(a,b)=22≈0.7071\cos(a, b) = \frac{\sqrt{2}}{2} \approx 0.7071
Angle
θ=45∘=π4\theta = 45^\circ = \frac{\pi}{4}
Dot product
a⋅b=5a \cdot b = 5
Lengths
∣a∣=10,∣b∣=5|a| = \sqrt{10},\quad |b| = \sqrt{5}
Euclidean distance
∣a−b∣=5|a - b| = \sqrt{5}

They point broadly the same way, so the similarity is positive.

What cosine similarity measures

Cosine similarity compares two vectors by the angle between them and ignores how long they are. It's the dot product divided by both lengths, which is the cosine of the angle θ\theta:

cos⁡(a,b)=a⋅b∣a∣ ∣b∣\cos(a, b) = \frac{a \cdot b}{|a|\,|b|}

The result is always between −1-1 and 11. It's 11 when the vectors point the same way, 00 when they're perpendicular, and −1-1 when they point in opposite directions. Doubling a vector doubles its dot product with everything, but it also doubles its length, so the two cancel and the cosine stays put. The dot product itself, and the angle formula behind it, are worked through in the Dot Product and Angle Calculator.

This calculator keeps the answer exact, so you see 22\frac{\sqrt{2}}{2} rather than only 0.70710.7071. With three or more vectors, it fills in the cosine for every pair. Stack unit-length vectors as the columns of a matrix UU and that whole grid is one matrix product, UTUU^{T}U.

Why machine learning uses it

Many models turn things into vectors whose direction carries the meaning and whose length mostly doesn't.

  • Embeddings. Language models map words, sentences, and documents to vectors. Two texts about the same thing point in similar directions, so semantic search and retrieval rank results by cosine similarity with the query.
  • Word counts. A document can be a vector of how often each word appears. A long article and a short note on the same topic have very different lengths but similar proportions, so their cosine is high while their distance apart is large. The "Word counts" example shows three documents over a five-word vocabulary.
  • Recommendations. Users and items become vectors of ratings or learned features, and "people like you also liked" is a nearest-neighbor search by cosine.

When every vector is scaled to length 1 first, which most embedding libraries do, cosine similarity and the dot product give the same number, and ranking by distance gives the same order too. The difference only matters when lengths vary.

Similarity, distance, and how to read the grid

The cosine distance 1−cos⁡(a,b)1 - \cos(a, b) turns similarity into a distance-like number from 0 to 2. It isn't a true distance, but it's widely used for clustering. The Euclidean distance ∣a−b∣|a - b| shown beside it does depend on length: in the "Same direction" example, aa and bb have cosine 1 yet sit a whole ∣a∣|a| apart.

In the grid, each row and column is one vector, the diagonal is always 1, and the grid is symmetric because the angle from aa to bb is the same as from bb to aa. Darker cells are more similar, and red ones point apart. A zero vector has no direction, so its row stays empty. The idea of measuring how much one vector lies along another continues in the Vector Projection Calculator.