Skip to main content

Cosine Similarity Calculator

Solve cosine similarity problems step-by-step with our free calculator. See formulas, worked examples, and clear explanations.

Skip to calculator
Mathematics

Cosine Similarity Calculator

Calculate the cosine similarity between two vectors. Measure directional similarity, angle between vectors, and cosine distance for NLP, machine learning, and data analysis.

Last updated: December 2025Reviewed by NovaCalculator Mathematics Team

Calculator

Adjust values & calculate
Cosine Similarity
0.974632
97.46% similar (3D vectors)
Dot Product
32.0000
Cosine Distance
0.025368
Angle
12.93 deg
|A| Magnitude
3.7417
|B| Magnitude
8.7750
Euclidean Distance
5.1962
Manhattan Distance
9.0000
Similarity Gauge
0% (Orthogonal)100% (Identical)
Your Result
Cosine Similarity: 0.974632 (97.46%) | Angle: 12.93 degrees
Share Your Result
Understand the Math

Formula

cos(theta) = (A . B) / (|A| * |B|)

Where A . B is the dot product of vectors A and B (sum of element-wise products), |A| is the magnitude of A (square root of sum of squared elements), and |B| is the magnitude of B. The result ranges from -1 to 1.

Last reviewed: December 2025

Worked Examples

Example 1: Text Document Similarity

Two document vectors are A = (1, 2, 3) and B = (4, 5, 6). Find the cosine similarity.
Solution:
Dot product: 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32 |A| = sqrt(1 + 4 + 9) = sqrt(14) = 3.7417 |B| = sqrt(16 + 25 + 36) = sqrt(77) = 8.7749 Cosine similarity = 32 / (3.7417 * 8.7749) = 32 / 32.8330 = 0.9746 Angle = arccos(0.9746) = 12.93 degrees
Result: Cosine Similarity: 0.9746 | Angle: 12.93 degrees | Cosine Distance: 0.0254

Example 2: Orthogonal Vectors

Compare vectors A = (1, 0, 0) and B = (0, 1, 0) for similarity.
Solution:
Dot product: 1*0 + 0*1 + 0*0 = 0 |A| = sqrt(1 + 0 + 0) = 1 |B| = sqrt(0 + 1 + 0) = 1 Cosine similarity = 0 / (1 * 1) = 0 Angle = arccos(0) = 90 degrees
Result: Cosine Similarity: 0.0000 | Angle: 90.00 degrees | Vectors are perpendicular
Expert Insights

Background & Theory

The Cosine Similarity Calculator applies the following established principles and formulas. Mathematics rests on a hierarchy of number systems, each extending the previous. The natural numbers (1, 2, 3, ...) support counting and ordering. The integers add negative values and zero, enabling subtraction without restriction. The rational numbers, expressible as p/q where p and q are integers and q is nonzero, close the system under division. The real numbers fill the gaps left by irrationals such as the square root of 2 or pi, forming a complete ordered field. The complex numbers, written as a + bi where i is the square root of negative one, complete the algebraic closure of the reals and allow every polynomial to have a root. Prime factorization states that every integer greater than one is uniquely expressible as a product of primes, a result known as the Fundamental Theorem of Arithmetic. Computing the greatest common divisor (GCD) of two integers relies most efficiently on the Euclidean algorithm: repeatedly replace the larger number with the remainder when it is divided by the smaller, until the remainder is zero. The last nonzero remainder is the GCD. The least common multiple (LCM) follows from the identity LCM(a, b) = |a * b| / GCD(a, b). Modular arithmetic defines equivalence classes of integers that share the same remainder under division by a modulus n. Fermat's Little Theorem and Euler's Theorem arise from this structure and underpin modern cryptography. Logarithms are the inverses of exponential functions. If b raised to the power x equals y, then the logarithm base b of y equals x. The natural logarithm uses base e, approximately 2.71828. Combinatorics counts arrangements and selections. The number of ordered arrangements (permutations) of r objects from n distinct objects is nPr = n! / (n - r)!. The number of unordered selections (combinations) is nCr = n! / (r! * (n - r)!). Pascal's triangle arranges these binomial coefficients so that each entry equals the sum of the two entries directly above it. The Fibonacci sequence, defined by F(1) = 1, F(2) = 1, and F(n) = F(n-1) + F(n-2), appears throughout nature and connects deeply to the golden ratio via Binet's formula.

History

The history behind the Cosine Similarity Calculator traces back through the following developments. Mathematics as a systematic discipline traces to ancient Mesopotamia. Babylonian clay tablets dating to around 1800 BCE demonstrate knowledge of quadratic equations, Pythagorean triples, and base-60 arithmetic, suggesting a practical mathematical tradition far preceding Greek formalism. Euclid of Alexandria compiled the Elements around 300 BCE, establishing the axiomatic method that would define rigorous mathematics for over two thousand years. His work organized plane geometry, number theory, and proportion into logically chained propositions derived from a small set of postulates. The algorithm bearing his name for computing GCDs appears in Book VII and remains in use today. In the 9th century, the Persian scholar Muhammad ibn Musa Al-Khwarizmi wrote Al-Kitab al-mukhtasar fi hisab al-jabr wal-muqabala, the treatise whose title gave algebra its name. He systematized the solution of linear and quadratic equations and described procedures that operated on unknowns as objects, a conceptual leap away from purely numerical calculation. Rene Descartes introduced coordinate geometry in 1637 by uniting algebra and Euclidean geometry, allowing curves to be studied through equations. This synthesis set the stage for calculus. Isaac Newton and Gottfried Wilhelm Leibniz independently developed calculus during the 1660s and 1670s, triggering a priority dispute that lasted decades and divided British and Continental mathematicians. Carl Friedrich Gauss proved the Fundamental Theorem of Algebra in 1799, showing that every nonconstant polynomial has at least one complex root. His Disquisitiones Arithmeticae of 1801 established modern number theory. David Hilbert's formalist program at the turn of the 20th century sought to place all of mathematics on an explicit axiomatic foundation, a project that Kurt Godel's incompleteness theorems of 1931 showed to be fundamentally limited. Alan Turing's work in the 1930s on computability introduced the theoretical model of the stored-program computer and linked mathematical logic directly to the limits of algorithmic calculation. His proof that no algorithm can decide in general whether an arbitrary program will halt or run forever placed fundamental boundaries on what mathematics can mechanically determine, and it opened the discipline now known as theoretical computer science.

Share this calculator

Explore More

Frequently Asked Questions

Cosine similarity is a metric that measures the cosine of the angle between two non-zero vectors in a multi-dimensional space. It ranges from -1 (completely opposite directions) through 0 (perpendicular, no similarity) to 1 (identical direction, maximum similarity). Unlike Euclidean distance, cosine similarity focuses on the orientation of vectors rather than their magnitude, making it particularly useful when the length of vectors is not meaningful. For example, in text analysis, two documents about the same topic might have different word counts but similar word frequency proportions, resulting in high cosine similarity despite different vector magnitudes.
Cosine similarity is calculated by dividing the dot product of two vectors by the product of their magnitudes. The formula is: cos(theta) = (A dot B) / (|A| * |B|). The dot product A dot B is the sum of element-wise products: a1*b1 + a2*b2 + ... + an*bn. The magnitude |A| is the square root of the sum of squared elements: sqrt(a1^2 + a2^2 + ... + an^2). This calculation works for vectors of any dimensionality, from 2D to thousands of dimensions. The result is always between -1 and 1, providing a normalized measure of directional similarity that is independent of scale.
Cosine distance is simply 1 minus the cosine similarity, converting the similarity measure into a distance metric. While cosine similarity of 1 means identical direction (most similar), cosine distance of 0 means the same thing (closest distance). Cosine distance ranges from 0 to 2, where 0 means identical direction, 1 means perpendicular, and 2 means opposite directions. Cosine distance is preferred in clustering algorithms and nearest-neighbor searches because distance metrics typically expect lower values to indicate closer items. However, cosine distance is technically a semi-metric rather than a true metric because it does not satisfy the triangle inequality in all cases.
Cosine similarity is extensively used in natural language processing (NLP) and machine learning for comparing text documents, word embeddings, and feature vectors. In information retrieval, search engines use cosine similarity to rank documents by relevance to a query vector. Word embedding models like Word2Vec and GloVe use cosine similarity to find semantically similar words. In recommendation systems, cosine similarity identifies users with similar preference patterns. Sentence transformers and BERT models produce embeddings where cosine similarity measures semantic relatedness. It is also used in image recognition, plagiarism detection, and anomaly detection across many domains.
Cosine similarity is preferred for text analysis because it is invariant to the magnitude of vectors, focusing purely on their direction. In text processing, document vectors can have very different magnitudes simply because documents have different lengths, but they may discuss the same topics with similar word frequency proportions. Euclidean distance would consider these documents dissimilar due to magnitude differences, while cosine similarity correctly identifies them as similar. Additionally, cosine similarity handles high-dimensional sparse vectors (common in bag-of-words representations) more effectively and provides a bounded output between -1 and 1, making interpretation and threshold-setting straightforward.
A cosine similarity of zero means the two vectors are perpendicular (orthogonal) to each other, indicating no linear relationship in their directions. In text analysis, this means the documents share no common terms or features. A negative cosine similarity means the vectors point in generally opposite directions, which can occur when features have meaningful negative values (like in centered or normalized data). However, in many practical applications such as term frequency vectors where all values are non-negative, cosine similarity is always between 0 and 1, and negative values cannot occur. The interpretation depends heavily on how the input vectors were constructed.
Educational Note: This calculator is provided for educational and informational purposes. Results are based on the formulas and inputs provided. Always verify important calculations independently. NovaCalculator processes calculator inputs client-side; optional analytics follow visitor consent settings.Reviewed by: NovaCalculator Mathematics Team โ€” Verified against standard mathematical and scientific references. Last reviewed: December 2025. ยฉ 2024โ€“2026 NovaCalculator.

Share this calculator

Formula

cos(theta) = (A . B) / (|A| * |B|)

Where A . B is the dot product of vectors A and B (sum of element-wise products), |A| is the magnitude of A (square root of sum of squared elements), and |B| is the magnitude of B. The result ranges from -1 to 1.

Worked Examples

Example 1: Text Document Similarity

Problem: Two document vectors are A = (1, 2, 3) and B = (4, 5, 6). Find the cosine similarity.

Solution: Dot product: 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32\n|A| = sqrt(1 + 4 + 9) = sqrt(14) = 3.7417\n|B| = sqrt(16 + 25 + 36) = sqrt(77) = 8.7749\nCosine similarity = 32 / (3.7417 * 8.7749) = 32 / 32.8330 = 0.9746\nAngle = arccos(0.9746) = 12.93 degrees

Result: Cosine Similarity: 0.9746 | Angle: 12.93 degrees | Cosine Distance: 0.0254

Example 2: Orthogonal Vectors

Problem: Compare vectors A = (1, 0, 0) and B = (0, 1, 0) for similarity.

Solution: Dot product: 1*0 + 0*1 + 0*0 = 0\n|A| = sqrt(1 + 0 + 0) = 1\n|B| = sqrt(0 + 1 + 0) = 1\nCosine similarity = 0 / (1 * 1) = 0\nAngle = arccos(0) = 90 degrees

Result: Cosine Similarity: 0.0000 | Angle: 90.00 degrees | Vectors are perpendicular

Frequently Asked Questions

What is cosine similarity and how does it measure vector similarity?

Cosine similarity is a metric that measures the cosine of the angle between two non-zero vectors in a multi-dimensional space. It ranges from -1 (completely opposite directions) through 0 (perpendicular, no similarity) to 1 (identical direction, maximum similarity). Unlike Euclidean distance, cosine similarity focuses on the orientation of vectors rather than their magnitude, making it particularly useful when the length of vectors is not meaningful. For example, in text analysis, two documents about the same topic might have different word counts but similar word frequency proportions, resulting in high cosine similarity despite different vector magnitudes.

How is cosine similarity calculated mathematically?

Cosine similarity is calculated by dividing the dot product of two vectors by the product of their magnitudes. The formula is: cos(theta) = (A dot B) / (|A| * |B|). The dot product A dot B is the sum of element-wise products: a1*b1 + a2*b2 + ... + an*bn. The magnitude |A| is the square root of the sum of squared elements: sqrt(a1^2 + a2^2 + ... + an^2). This calculation works for vectors of any dimensionality, from 2D to thousands of dimensions. The result is always between -1 and 1, providing a normalized measure of directional similarity that is independent of scale.

What is the difference between cosine similarity and cosine distance?

Cosine distance is simply 1 minus the cosine similarity, converting the similarity measure into a distance metric. While cosine similarity of 1 means identical direction (most similar), cosine distance of 0 means the same thing (closest distance). Cosine distance ranges from 0 to 2, where 0 means identical direction, 1 means perpendicular, and 2 means opposite directions. Cosine distance is preferred in clustering algorithms and nearest-neighbor searches because distance metrics typically expect lower values to indicate closer items. However, cosine distance is technically a semi-metric rather than a true metric because it does not satisfy the triangle inequality in all cases.

Where is cosine similarity used in machine learning and NLP?

Cosine similarity is extensively used in natural language processing (NLP) and machine learning for comparing text documents, word embeddings, and feature vectors. In information retrieval, search engines use cosine similarity to rank documents by relevance to a query vector. Word embedding models like Word2Vec and GloVe use cosine similarity to find semantically similar words. In recommendation systems, cosine similarity identifies users with similar preference patterns. Sentence transformers and BERT models produce embeddings where cosine similarity measures semantic relatedness. It is also used in image recognition, plagiarism detection, and anomaly detection across many domains.

Why is cosine similarity preferred over Euclidean distance for text analysis?

Cosine similarity is preferred for text analysis because it is invariant to the magnitude of vectors, focusing purely on their direction. In text processing, document vectors can have very different magnitudes simply because documents have different lengths, but they may discuss the same topics with similar word frequency proportions. Euclidean distance would consider these documents dissimilar due to magnitude differences, while cosine similarity correctly identifies them as similar. Additionally, cosine similarity handles high-dimensional sparse vectors (common in bag-of-words representations) more effectively and provides a bounded output between -1 and 1, making interpretation and threshold-setting straightforward.

What does it mean when cosine similarity is zero or negative?

A cosine similarity of zero means the two vectors are perpendicular (orthogonal) to each other, indicating no linear relationship in their directions. In text analysis, this means the documents share no common terms or features. A negative cosine similarity means the vectors point in generally opposite directions, which can occur when features have meaningful negative values (like in centered or normalized data). However, in many practical applications such as term frequency vectors where all values are non-negative, cosine similarity is always between 0 and 1, and negative values cannot occur. The interpretation depends heavily on how the input vectors were constructed.

References

Reviewed by Manoj Kumar, Mathematics Educator ยท Editorial policy