Cosine Similarity Calculator
Cosine similarity of two vectors or embeddings — the cosine, the angle between them, cosine distance, dot product, both norms and the Euclidean distance.
How it works
cos θ = (A·B) ÷ (‖A‖·‖B‖) = Σaᵢbᵢ ÷ (√Σaᵢ² · √Σbᵢ²); θ = arccos(cos θ); cosine distance = 1 − cos θ
Cosine similarity compares the direction of two vectors and ignores their length, which is why it is the standard measure for text vectors (word counts, TF-IDF) and for embeddings from language models in semantic search and retrieval-augmented generation. It runs from −1 (opposite) through 0 (orthogonal, unrelated) to 1 (same direction); with non-negative data such as word counts it stays between 0 and 1.
Worked example
The ten-term count vectors A = (3, 2, 0, 5, 0, 0, 0, 2, 0, 0) and B = (1, 0, 0, 0, 0, 0, 0, 1, 0, 2) have a dot product of 5 and norms 6.4807 and 2.4495, so the cosine similarity is 0.3150 — an angle of 71.64°.
Sources: Cosine similarity — Wikipedia · scikit-learn — cosine_similarity
Frequently asked questions
Cosine similarity or Euclidean distance?
Use cosine when only direction matters (documents of different length, embeddings); use Euclidean distance when magnitude matters too. For unit-length vectors the two give the same ranking.
What is a “good” cosine similarity for embeddings?
It depends on the model: many sentence-embedding models give 0.8+ for paraphrases and 0.2–0.4 for unrelated text, so compare scores within the same model.
Can cosine similarity be negative?
Yes, when the vectors point in opposite directions. With non-negative inputs (counts, TF-IDF) it is always between 0 and 1.
Is cosine distance a true metric?
No — it does not satisfy the triangle inequality. The angular distance θ/π does.
Related calculators
This calculator is for educational purposes. Double-check important results before acting on them.