induwara.lk
induwara.lkAI · Machine learning

Cosine Similarity Calculator

Find the cosine similarity between two vectors or two texts, in your browser. See the similarity score, the cosine distance, the angle in degrees, and the full dot-product and magnitude working behind every result. No signup, nothing uploaded.

By Induwara AshinsanaUpdated Jun 10, 2026
Cosine similarity calculator

Numbers separated by commas, spaces, or new lines.

Must have the same number of components as A.

Examples
Cosine similarity
1.0000
Range −1 to 1 · 3-D
Cosine distance
0.0000
1 − similarity
Angle
0.00°
0.0000 rad
Interpretation
Identical direction
Decimals

Cross-check. The quotient form (A·B)/(‖A‖‖B‖) gives 1.0000; the independent unit-vector form ·B̂ — how scikit-learn computes it — gives 1.0000. They reconcile, as they must.

Step-by-step working

Componentaᵢbᵢaᵢ·bᵢaᵢ²bᵢ²
#11.00002.00002.00001.00004.0000
#22.00004.00008.00004.000016.0000
#33.00006.000018.00009.000036.0000
Totals28.000014.000056.0000
‖A‖ = √14.0000 = 3.7417
‖B‖ = √56.0000 = 7.4833
cos = 28.0000 / (3.7417 × 7.4833) = 1.0000

Method: cos(A,B) = (A·B) / (‖A‖·‖B‖); distance = 1 − cos; angle = arccos(cos) — scikit-learn cosine_similarity and Manning, Raghavan & Schütze, Introduction to Information Retrieval, Ch. 6. Nothing leaves this page.

How it works

Cosine similaritymeasures the angle between two vectors while ignoring how long they are. Two vectors that point the same way score 1, vectors at right angles score 0, and vectors pointing in opposite directions score −1. The definition is the one used by scikit-learn and by Manning, Raghavan & Schütze's Introduction to Information Retrieval, Chapter 6.

For two equal-length vectors A = [a₁…aₙ] and B = [b₁…bₙ], the similarity is the dot product divided by the product of the two magnitudes:

cos(A, B) = (A · B) / (‖A‖ · ‖B‖) = Σ aᵢbᵢ / ( √(Σ aᵢ²) · √(Σ bᵢ²) )

The tool computes this in four steps:

  1. Dot product. Multiply the vectors component by component and add the results: A · B = Σ aᵢbᵢ.
  2. Magnitudes.Take the square root of each vector's sum of squares: ‖A‖ = √(Σ aᵢ²) and likewise for ‖B‖.
  3. Divide. The similarity is (A·B) / (‖A‖·‖B‖). If either magnitude is zero the vector has no direction, so the result is undefined and the tool shows a clear message instead of a divide-by-zero.
  4. Derive distance and angle. Cosine distance is 1 − similarity, and the angle is arccos(similarity) in degrees, clamping the input to [−1, 1] first to guard against floating-point drift.

Text mode applies the vector-space model from the same IR textbook. Each text is tokenised into words, a shared vocabulary is built from both texts, and every text becomes a raw term-frequency vector — one count per vocabulary word — over that shared vocabulary. Those two vectors are then fed into exactly the steps above. Because both texts are projected onto the same vocabulary, their vectors are always the same length. This version uses raw counts, not TF-IDF weighting, so the working stays transparent; for model-based semantic comparison, the embedding tools linked below go further. As a credibility check, the calculator also computes the similarity a second way — by normalising each vector to unit length and taking the dot product, which is how scikit-learn implements it internally — and confirms the two routes agree.

Worked examples

Parallel vectors — A = [1, 2, 3], B = [2, 4, 6] (B = 2·A)

  1. Dot product: 1·2 + 2·4 + 3·6 = 2 + 8 + 18 = 28
  2. ‖A‖ = √(1 + 4 + 9) = √14 = 3.741657
  3. ‖B‖ = √(4 + 16 + 36) = √56 = 7.483315
  4. cos = 28 / (3.741657 × 7.483315) = 28 / 28 = 1.0000
  5. distance = 0.0000, angle = arccos(1) = 0.00° → Identical direction

Orthogonal vectors — A = [1, 0], B = [0, 1]

  1. Dot product: 1·0 + 0·1 = 0
  2. ‖A‖ = √1 = 1, ‖B‖ = √1 = 1
  3. cos = 0 / (1 × 1) = 0.0000
  4. distance = 1.0000, angle = arccos(0) = 90.00°
  5. No shared direction → Unrelated / orthogonal

Text mode — “the cat sat” vs “the cat ran” (case-insensitive)

  1. Vocabulary = [the, cat, sat, ran]
  2. TF A = [1, 1, 1, 0], TF B = [1, 1, 0, 1]
  3. Dot product: 1·1 + 1·1 + 1·0 + 0·1 = 2
  4. ‖A‖ = √3 = 1.732051, ‖B‖ = √3 = 1.732051
  5. cos = 2 / 3 = 0.6667, angle = arccos(2/3) = 48.19° → Moderately similar

Frequently asked questions

Sources & references

The formulas on this page were last cross-checked against these sources on 2026-06-10. Cosine similarity is a stable mathematical definition, so this tool needs no rate or schedule updates — only the worked examples are periodically re-reconciled.

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.