How to calculate cosine similarity

By

Paste two JSON arrays into the cosine similarity calculator. Read the cosine, the dot product, both magnitudes, and the Euclidean and Manhattan distances.

Free toolThis tutorial uses the Cosine Similarity Calculator. It runs in your browser, so open it in another tab and follow along.

Cosine similarity is the cosine of the angle between two vectors. You get it from the dot product divided by the product of the two lengths.

We’ll use the cosine similarity calculator. Paste two JSON arrays. The page runs in the browser, and the arrays are not uploaded or stored in the URL.

Compare the sample vectors

  1. Open the cosine similarity calculator.
  2. Click Load sample, or paste your own arrays.
  3. Read the result. Copy if you want the same text in a note.

Vector A is [1, 2, 3]. Vector B is [1, 2, 4].

The page prints:

Dimensions: 3
Cosine similarity: 0.991460133984
Dot product: 17
Magnitude A: 3.74165738677
Magnitude B: 4.58257569496
Euclidean distance: 1
Manhattan distance: 1
Angle: 7.49329295309 degrees

The dot product is 1*1 + 2*2 + 3*4, which is 17. The length of A is the square root of 1 + 4 + 9. The length of B is the square root of 1 + 4 + 16. Cosine similarity is 17 divided by those two lengths.

The vectors differ only in the last coordinate, by 1. Euclidean distance is the straight-line gap, so it is 1. Manhattan distance adds the absolute gaps, so it is also 1. The angle is the arccosine of the cosine, in degrees.

A cosine of 1 means the vectors point the same way. 0 means they are perpendicular. -1 means they point in opposite directions. A higher cosine means the directions are closer. It does not, by itself, mean one chunk answers a question.

How this page calculates cosine similarityPaste two JSON arrays of the same length. The page computes the dot product and both magnitudes, then divides. A zero-length vector leaves the cosine undefined.NoYesYesNoPaste two JSONarraysSame length, finitenumbers?Show the errorDot product and bothlengthsEither length is 0?Cosine and angleundefinedCosine, angle, anddistances

What has to match

Both arrays need the same number of values. [1, 2] and [1, 2, 3] stop with the two lengths in the message.

Each value has to be a finite JSON number. A string, null, NaN, or Infinity is refused, and the message names the position.

An empty array is refused. So is an array longer than 8,192 numbers. That limit is there so a huge paste does not lock the tab.

A vector of all zeros has no direction. The cosine and the angle are undefined. The dot product and the distances are still reported.

If floating point pushes a cosine a tiny bit outside -1 to 1, the angle uses the clamped value, and the result says so.

Numbers are printed with toPrecision(12), then shortened. The same pair of arrays always prints the same lines.

Where this shows up

Embedding APIs return arrays like these. Cosine similarity is one way to compare two of them while you debug a retrieval step. The embeddings cost calculator prices the API call. The RAG chunk calculator counts chunks and storage. Neither of those compares two vectors.

RAG chunking explained covers chunk size and overlap. The JavaScript Math library is where Math.sqrt and Math.acos live. Don’t let the LLM do the math is the case for doing this arithmetic in your own code. The tokenizer counts tokens. It does not compare vectors.

Try the Cosine Similarity Calculator

Paste two JSON arrays and compare them in the browser. You get cosine similarity, the dot product, magnitudes, and Euclidean and Manhattan distances.

Open the Cosine Similarity Calculator →

  • Embeddings Cost Estimator: Estimate embedding API cost, chunk counts, vector storage size, and rough monthly bills for your corpus.
  • RAG Chunk Size Calculator: Chunk count, embedding cost, vector storage size, and whether top-k retrieved chunks actually fit your context window.
  • LLM Tokenizer Visualizer: See the real BPE tokens, token IDs, and character ratio for pasted text.

More tool tutorials →

Want me to talk about your product? You can sponsor this site.