🏆 US-Registered Digital Marketing Agency
Advertisement
Advertisement
DEVELOPER

Cosine Similarity Calculator — two vectors, any dimension

Paste two numeric vectors and get the cosine similarity, the cosine distance, the dot product, both magnitudes and the angle between them.

Separate values with commas, spaces, tabs or new lines. Negative numbers and decimals are fine. Everything is computed in your browser — no data is sent anywhere.
Must have the same number of components as vector A unless you allow zero-padding below.
Zero-padding is what a sparse text vectoriser does implicitly, but it is the wrong choice for measured data.
Similarity values often sit above 0.99, so four decimals or more is usually needed to tell candidates apart.
Cosine similarity
0
 
0
Cosine distance
0
Dot product
0
Magnitude |A| × |B|
Angle between vectors
Similarity on a 0 to 1 scale
0%
Vector detail:
Advertisement

The cosine similarity calculator above measures how closely two vectors point in the same direction, regardless of how long either one is. It returns a value from −1 to 1, where 1 means the vectors are perfectly aligned, 0 means they are orthogonal and share nothing, and −1 means they point in exactly opposite directions. It also reports cosine distance, the raw dot product, the two magnitudes and the angle in degrees, because those intermediate numbers are what tell you whether the headline figure is trustworthy. Every calculation happens in your browser; nothing you paste is transmitted or stored.

Arb Digital publishes this alongside its other free developer and mathematics tools because cosine similarity has quietly become one of the most widely computed numbers in software. It is the default comparison in text retrieval, in recommendation systems, and in every vector database that stores embeddings for semantic search, so being able to check a value by hand is genuinely useful when a pipeline starts returning results that look wrong.

What This Cosine Similarity Calculator Does

Given two vectors of the same dimension, it computes the cosine of the angle between them. The key property is scale invariance: multiplying every component of a vector by any positive constant leaves the similarity unchanged, because both the dot product and the magnitude scale by the same factor and the two cancel. That is exactly what you want when comparing documents of different lengths, users with different levels of activity, or embeddings whose norms vary for reasons unrelated to meaning.

The tool accepts any dimension. Two components or two thousand, it makes no difference to the arithmetic. Values can be separated by commas, spaces, tabs or line breaks, so you can paste a column out of a spreadsheet or a bracketed list from a JSON payload without reformatting it first. It reports cosine distance as 1 minus similarity, which is the convention most libraries use, and it shows the angle in degrees for anyone who finds a geometric figure easier to reason about than a decimal near 1.

If you need general vector arithmetic — addition, cross products, projections — the vector calculator covers that ground. This page does one comparison thoroughly rather than many operations shallowly.

How to Use It

  1. Paste vector A. Any delimiter works. Empty entries and stray brackets are ignored.
  2. Paste vector B. Give it the same number of components, or switch the mismatch handling to zero-padding if you know that is appropriate.
  3. Increase the decimal places if the values are close. Embedding comparisons routinely differ in the fourth or fifth decimal, and rounding to two hides the ranking entirely.
  4. Read the magnitudes as well as the similarity. A magnitude of zero makes the similarity undefined, and the tool will tell you rather than silently returning zero.
  5. Check the angle. Ninety degrees means no shared direction; anything under about twenty-five degrees is a strong match in most applications.

The Formula and How It's Calculated

Cosine similarity between vectors A and B is the dot product divided by the product of the two Euclidean norms:

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

where A · B is the sum of the element-wise products, and each norm is the square root of the sum of squares of that vector's components. Take A = [1, 2, 3] and B = [4, 5, 6], the default in the box above. The dot product is (1×4) + (2×5) + (3×6) = 4 + 10 + 18 = 32. The norm of A is √(1 + 4 + 9) = √14 = 3.741657. The norm of B is √(16 + 25 + 36) = √77 = 8.774964. Their product is 32.832910, so the similarity is 32 ÷ 32.832910 = 0.974632. The corresponding angle is arccos(0.974632) = 12.933 degrees, and the cosine distance is 1 − 0.974632 = 0.025368.

Notice what happens if you double every component of B, giving [8, 10, 12]. The dot product doubles to 64 and the norm of B doubles to 17.549929, so the ratio is unchanged at 0.974632. That invariance is the whole reason cosine similarity is preferred over raw dot product when vector lengths carry no meaning. Stanford's Introduction to Information Retrieval chapter on dot products derives the same measure in the context of the vector space model and works through why the length normalisation is necessary when comparing documents.

Advertisement

Why Similarity Above 0.9 Is Often Meaningless

This trips up almost everyone working with text embeddings for the first time. Compare two completely unrelated sentences using a modern embedding model and you will frequently get a cosine similarity of 0.75 or higher. Compare two vaguely related ones and you get 0.85. Two near-duplicates give 0.95. The scale does not span the full range from 0 to 1, because embedding vectors from a trained model occupy a relatively narrow cone in the space rather than being spread uniformly over the sphere.

The practical consequence is that an absolute threshold like “accept anything above 0.8” is meaningless without calibration against the specific model you are using. What matters is the ranking and the gap between candidates, not the raw value. If your top result scores 0.91 and your tenth scores 0.89, the retrieval is not discriminating and no threshold will fix it. Measure the distribution of scores on pairs you know are unrelated, and set your cut-off relative to that baseline. This is also why raising decimal places on this page is not pedantry: in that regime the fourth decimal is where the signal lives.

Cosine Similarity Against Euclidean Distance and Dot Product

These three are related, and picking the wrong one produces subtly bad results rather than obvious errors. Euclidean distance measures the straight-line gap between the two points, so it responds to magnitude as well as direction; two documents on the same subject but of very different lengths sit far apart under Euclidean distance and close together under cosine. Raw dot product responds to both direction and magnitude, so it rewards long vectors, which is sometimes exactly what you want — in a recommender, a longer vector can encode a more confident or more active profile.

The useful identity is this: if both vectors are normalised to unit length, cosine similarity and dot product become the same number, and Euclidean distance becomes a monotonic function of it, specifically distance² = 2 − 2×cosine. Gilbert Strang's 18.06 Linear Algebra course on MIT OpenCourseWare covers the underlying geometry of vector spaces, norms and orthogonality in full, with video lectures and problem sets. That is why vector databases normalise on ingestion and then use the cheaper inner-product operation at query time; the ranking is identical and the arithmetic is faster. If your index is normalised and your query vector is not, though, the ranking breaks, and the symptom is results that look almost right but are ordered oddly. Checking one pair by hand here is the fastest way to catch it.

When Cosine Similarity Is the Wrong Tool

Three cases where it misleads. First, when magnitude genuinely carries the information: comparing sales figures, sensor readings or budgets by direction alone throws away the thing you cared about. A store selling [10, 20, 30] units and one selling [100, 200, 300] have a cosine similarity of exactly 1, which is true about their product mix and useless about their size.

Second, when the data contains meaningful zeros that are not really zeros. In sparse text vectors, a zero means “this word did not appear”, which is fine. In a ratings matrix, a zero often means “not rated yet”, and treating that as a genuine value pulls similarity toward whatever the missing pattern happens to be. Adjusted cosine similarity, which subtracts each user's mean rating first, exists precisely to handle that.

Third, when the vectors can contain negative values but you interpret the result as a percentage. Cosine similarity ranges over −1 to 1 for general vectors and only over 0 to 1 when all components are non-negative, which is the usual case for term counts. The bar on this page maps the full range onto its track, so a negative similarity shows as an empty bar and the numeric value tells the real story. For summarising the spread of the underlying numbers, the standard deviation calculator is a useful companion, and the matrix calculator handles the case where you have many vectors arranged as rows rather than a single pair.

Building something that needs semantic search?

Arb Digital builds fast, well-engineered websites and web applications — including search that returns the right result rather than the closest-looking one.

See Web Design Services Talk to Arb Digital

Common Mistakes to Avoid

  • Applying a fixed threshold across different models. A score of 0.8 means something entirely different from one embedding model to the next. Calibrate against known-unrelated pairs first.
  • Comparing vectors of different dimensions by padding. Zero-padding is valid for sparse vocabularies and wrong for measured features, where it invents data that was never observed.
  • Ignoring a zero vector. If either magnitude is zero the similarity is undefined, not zero. Returning zero silently hides an empty document or a failed embedding call.
  • Rounding to two decimals. Real comparisons frequently differ in the fourth decimal place, and rounding early destroys the ranking you were trying to produce.
  • Mixing normalised and unnormalised vectors in one index. The ranking becomes incoherent, and the results look plausible enough that the bug survives for months.

Related Free Tools From Arb Digital

For general vector operations there is the vector calculator, and for row-and-column work the matrix calculator. When you are evaluating a classifier rather than comparing vectors, the confusion matrix calculator gives precision, recall and F1. The standard deviation calculator summarises a single distribution, the words to tokens converter estimates how much text you are about to embed, and the vector database cost calculator estimates what storing those embeddings will cost. Everything else is in the free online tools hub.

Frequently Asked Questions

What does a cosine similarity of 1, 0 or −1 mean?

A value of 1 means the vectors point in exactly the same direction, 0 means they are orthogonal and share no direction at all, and −1 means they point in exactly opposite directions. Magnitude has no effect on any of these values.

How do I calculate cosine similarity by hand?

Multiply the vectors element by element and add the results to get the dot product, then divide that by the product of the two Euclidean norms. For [1, 2, 3] and [4, 5, 6] the dot product is 32 and the norms are 3.741657 and 8.774964, giving 0.974632.

What is cosine distance?

Cosine distance is defined as 1 minus cosine similarity, so it ranges from 0 for identical direction up to 2 for opposite direction. It is the convention most libraries use when they need a value that grows as items become less alike.

Why do unrelated sentences still score above 0.7?

Because embedding vectors from a trained model occupy a narrow cone of the space rather than spreading evenly, so the usable range is compressed. Rank the candidates and look at the gaps between them instead of applying an absolute threshold.

Is cosine similarity the same as the dot product?

Only when both vectors have been normalised to unit length. In that case the norms are both 1, the denominator disappears, and the two measures give identical values. Otherwise the dot product also responds to magnitude.

Can cosine similarity be negative?

Yes, whenever the vectors contain negative components and point more than ninety degrees apart. If every component is non-negative, as with term counts, the value is confined to the range 0 to 1.

What happens if one vector is all zeros?

The similarity is mathematically undefined, because the denominator becomes zero. This tool reports that condition rather than returning zero, since a zero vector usually indicates an empty input or a failed embedding rather than a genuine result.

Is anything I paste here uploaded?

No. All arithmetic runs in your browser as local JavaScript. The page makes no network request, stores nothing and logs nothing, so vectors you paste never leave your device.

Advertisement
Advertisement

Take it further