Skip to main content
math

Cosine Similarity Calculator

Calculate the cosine similarity between two vectors to measure how closely they point in the same direction, independent of magnitude.

Reviewed by Chase FloiedUpdated

This free online cosine similarity calculator provides instant results with no signup required. All calculations run directly in your browser — your data is never sent to a server. Enter your values below and see results update in real time as you type. Perfect for everyday calculations, homework, or professional use.

The x component of the first vector

The y component of the first vector

The x component of the second vector

The y component of the second vector

How to Use This Calculator

1

Enter your input values

Fill in all required input fields for the Cosine Similarity Calculator. Most fields include unit selectors so you can work in your preferred unit system — metric or imperial, whichever matches your problem.

2

Review your inputs

Double-check that all values are correct and that you have selected the right units for each field. Incorrect units are the most common source of calculation errors and can produce results that are off by factors of 2, 10, or more.

3

Read the results

The Cosine Similarity Calculator instantly computes the output and displays results with units clearly labeled. All calculations happen in your browser — no loading time and no data sent to a server.

4

Explore parameter sensitivity

Try adjusting individual input values to see how the output changes. This is a quick and effective way to develop intuition about how different parameters influence the result and to identify which inputs have the largest effect.

Formula Reference

Cosine Similarity Calculator Formula

See calculator inputs for the governing equation

Variables: All variables and their units are labeled in the calculator interface above. Input fields accept values in multiple unit systems — select your preferred unit from the dropdown next to each field.

When to Use This Calculator

  • Use the Cosine Similarity Calculator when you need a quick mathematical result without writing out all the steps manually, saving time on repetitive calculations.
  • Use it to verify hand calculations on tests or assignments and catch arithmetic mistakes.
  • Use it when teaching or explaining mathematical concepts to others, demonstrating how changing inputs affects the result.
  • Use it to explore the behavior of mathematical functions across a range of inputs.

About This Calculator

The Cosine Similarity Calculator is a free mathematical calculation tool for students, educators, and professionals who need quick, reliable results. Calculate the cosine similarity between two vectors to measure how closely they point in the same direction, independent of magnitude. The underlying algorithms implement well-established mathematical formulas and numerical methods. Results are computed instantly in the browser. This tool is useful for learning, verification of hand calculations, and rapid exploration of mathematical relationships. All computation happens locally — no data is sent to a server.

About Cosine Similarity Calculator

Cosine similarity measures the cosine of the angle between two non-zero vectors in a multi-dimensional space. Unlike Euclidean distance, cosine similarity captures orientation rather than magnitude, making it invaluable in text analysis, recommendation systems, and machine learning. A value of 1 means the vectors point in the same direction, 0 means they are orthogonal, and -1 means they point in opposite directions. This calculator computes the cosine similarity for two-dimensional vectors by first finding their dot product and magnitudes, then dividing the dot product by the product of magnitudes. The metric is widely used in natural language processing where documents are represented as high-dimensional vectors and similarity between documents is measured by cosine similarity rather than raw distance. Understanding cosine similarity is essential for anyone working with vector spaces, information retrieval, or data science applications where directional alignment matters more than absolute scale.

The Math Behind It

Cosine similarity derives from the geometric definition of the dot product: A·B = |A||B|cos(θ). Rearranging gives cos(θ) = (A·B)/(|A||B|). For two-dimensional vectors A = (a₁, a₂) and B = (b₁, b₂), the dot product is a₁b₁ + a₂b₂, and each magnitude is the square root of the sum of squared components. The resulting value always falls in the range [-1, 1]. Cosine similarity is not a true distance metric because it does not satisfy the triangle inequality, but the related cosine distance (1 - similarity) is often used as a dissimilarity measure. In higher dimensions, the formula generalizes naturally: the dot product sums over all component-wise products, and the magnitude extends to the n-dimensional Euclidean norm. One key property is scale invariance: multiplying either vector by a positive scalar does not change the cosine similarity, which is why it excels in contexts where magnitude is irrelevant, such as comparing term-frequency vectors of documents of different lengths. The angular distance, defined as arccos of cosine similarity divided by π, provides a proper metric on the unit sphere and is sometimes preferred when a true metric is required.

Formula Reference

Cosine Similarity

cos(θ) = (A·B) / (|A| × |B|)

Variables: A, B = input vectors; θ = angle between them

Worked Examples

Example 1: Similarity of two direction vectors

Find the cosine similarity between A = (3, 4) and B = (1, 2).

Step 1:Compute the dot product: A·B = 3×1 + 4×2 = 3 + 8 = 11
Step 2:Compute |A| = sqrt(3² + 4²) = sqrt(9 + 16) = sqrt(25) = 5
Step 3:Compute |B| = sqrt(1² + 2²) = sqrt(1 + 4) = sqrt(5) ≈ 2.2361
Step 4:Cosine similarity = 11 / (5 × 2.2361) = 11 / 11.1803 ≈ 0.9839

The cosine similarity is approximately 0.9839, indicating the vectors point in nearly the same direction.

Example 2: Orthogonal vectors

Find the cosine similarity between A = (1, 0) and B = (0, 1).

Step 1:Dot product: 1×0 + 0×1 = 0
Step 2:|A| = 1, |B| = 1
Step 3:Cosine similarity = 0 / (1 × 1) = 0

The cosine similarity is 0, confirming the vectors are perpendicular.

Common Mistakes & Tips

  • !Forgetting that cosine similarity ranges from -1 to 1, not 0 to 1, when vectors can have negative components.
  • !Dividing by zero when one of the input vectors is the zero vector — cosine similarity is undefined for zero vectors.
  • !Confusing cosine similarity with cosine distance; cosine distance = 1 - cosine similarity.
  • !Assuming cosine similarity measures magnitude differences — it only measures directional alignment.

Related Concepts

Used in These Calculators

Calculators that build on or apply the concepts from this page:

Frequently Asked Questions

Can cosine similarity be negative?

Yes. A negative cosine similarity means the vectors point in generally opposite directions. The minimum value is -1, which occurs when two vectors are exactly anti-parallel.

Why is cosine similarity preferred over Euclidean distance in NLP?

In text analysis, document vectors can have very different magnitudes depending on document length. Cosine similarity ignores magnitude and focuses on the pattern of term usage, making it more robust for comparing documents of different sizes.

What happens if one vector is the zero vector?

Cosine similarity is undefined because the magnitude of a zero vector is 0, which causes division by zero. In practice, zero vectors are filtered out or handled as a special case.