StatGardenREF. DESK
Calculators/Maths/Cosine similarity
Maths

Cosine similarity calculator

Angular similarity between two vectors, from −1 (opposite) to 1 (same direction).

Published 4 August 2026 · Updated 24 September 2026

What this calculator does

Cosine similarity measures whether two vectors point the same way, and deliberately ignores how long they are. A value of 1 means identical direction, 0 means at right angles, and −1 means directly opposed. It is the cosine of the angle between the two vectors, which is where the name comes from.

Ignoring magnitude is the useful part. Two documents, one short and one long, that cover the same topics in the same proportions will score close to 1 even though one has far larger word counts throughout. That is why the measure turns up across text search, recommendation systems and embedding comparisons, where the shape of a vector carries the meaning and its overall size usually does not.

The formula

Formulacos θ = (a · b) / (‖a‖‖b‖) = Σaᵢbᵢ / (√Σaᵢ² · √Σbᵢ²)

The dot product of the two vectors is divided by the product of their magnitudes. The dot product multiplies matching components and adds the results; each magnitude is the square root of the sum of its own squared components. Dividing by both magnitudes is what strips scale out of the answer and leaves only direction behind.

TermMeaning
Cosine similarityThe dot product divided by both magnitudes, a value from −1 to 1.
Dot productThe sum of the products of matching components, which is positive when the vectors broadly agree and negative when they oppose.
Magnitude ‖a‖The length of a vector, the square root of the sum of its squared components.
Angle θThe angle between the two vectors, the inverse cosine of the similarity.

The inputs explained

FieldWhat to enter
Vector A (comma separated)The first vector, as numbers separated by commas. Any length works.
Vector B (comma separated)The second vector, which must have the same number of components as the first.

When to use it

Comparing documents or embeddings

Text turned into a vector of term counts or an embedding is compared by direction rather than length, so that a long document and a short one on the same subject score as similar. Cosine similarity is the standard measure for exactly this.

Ranking recommendations

A user preference vector compared against item vectors gives a similarity score for each item, and sorting by that score produces a ranking that is unaffected by how active any particular user has been.

Checking whether two directions are orthogonal

A similarity of 0 means the vectors are at right angles and share no component in common. This is a quick way to confirm that two axes, two features or two basis vectors are genuinely independent of one another.

Worked examples

Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.

What does cosine similarity give for vectors in different relationships?

One vector held fixed at (1, 2, 3), compared against five others chosen to sit at different angles to it.

Compared against A = (1, 2, 3)
Vector BCosine similarityAngle between vectorsDot product
(1, 2, 3)1.0000.00°14.000
(2, 4, 6)1.0000.00°28.000
(3, 2, 1)0.714344.42°10.000
(2, -1, 0)090.00°0
(-1, -2, -3)-1.000180.00°-14.000
The second row is the first row doubled. Its dot product doubles from 14 to 28, but the similarity stays at exactly 1 and the angle stays at 0 degrees, because scaling a vector does not turn it. The fourth row is perpendicular to A, giving a dot product of 0 and an angle of 90 degrees, and the last row is A reversed, giving −1 at 180 degrees.

Questions

What counts as a good cosine similarity score?

It depends entirely on the data, so there is no universal threshold. What is reliable is the ordering: a higher score means a closer direction. In practice a threshold is chosen by looking at scores for pairs already known to be related and pairs known not to be.

What is the difference between cosine similarity and cosine distance?

Cosine distance is 1 minus cosine similarity, so identical directions give a distance of 0 rather than a similarity of 1. The two carry the same information, and which one appears depends on whether the surrounding code expects larger values to mean closer or further apart.

Can cosine similarity be negative?

Yes, whenever the vectors have components that can be negative. If every component of both vectors is zero or positive, as with raw word counts, the dot product cannot be negative and the score stays between 0 and 1.

Why does scaling a vector leave the result unchanged?

Multiplying a vector by a constant multiplies the dot product by that constant and multiplies its magnitude by the same constant. The two effects cancel in the division, so only direction survives.

For the dot product on its own, see the dot product calculator. For how much of one vector lies along another, see the vector projection calculator.