Drag each answer to line up with its matching prompt
Cosine similarity
u · v = sum of u_i × v_i; on unit norm vectors equals cosine
Dot product (inner product)
Number of bit positions where two binary vectors differ; used for binary quantized embeddings
Euclidean (L2) distance
(u · v) / (||u|| × ||v||); measures angle between vectors, ignores magnitude
Hamming distance
||u - v|| = sqrt of sum of squared differences; on unit norm vectors monotone equivalent to cosine
Cosine measures angle, dot product is the unnormalized inner product, Euclidean measures straight-line distance, Hamming counts bit differences for binary vectors.
Imagine four ways to ask 'how alike are these two things'. One asks 'do they point in the same direction' (cosine). One adds up how much they agree position by position (dot product). One asks 'how far apart are they' (Euclidean). And one asks, for two strings of light switches, 'how many switches differ' (Hamming). Each is useful in a different setting, and a couple of them turn into the same answer when you do a little preprocessing.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Similarity metrics are the boring middle of every embedding system: necessary, easy to confuse, and unforgiving when chosen wrong. Four metrics dominate 2026 production: cosine, dot product, Euclidean, and Hamming. Three are for real-valued vectors and one is for binary. The four are not orthogonal options. They collapse to two under common preconditions, and most retrieval stacks end up using one of two configurations.
This deep dive defines each metric formally, walks through the algebraic identities that link them, and ends with the practical question of which metric to choose given the precision tier of your vectors.
The four definitions
Cosine similarity
Range: [-1, 1]. Scale-invariant: cos(u, v) = cos(αu, βv) for any positive α, β. Measures the angle between the two vectors.
Dot product (inner product)
Range: unbounded for general real vectors; [-1, 1] when both vectors are unit norm. Not scale-invariant. On unit-norm vectors it equals cosine.
Euclidean (L2) distance
Range: [0, ∞) in general; [0, 2] on unit-norm vectors. Measures the straight-line distance between vector tips.
Hamming distance
For binary vectors u, v ∈ {0, 1}^d:
Range: [0, d]. The integer count of bit positions where the two vectors differ.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Pinecone supports cosine, dot, and Euclidean metric types per index, defaulting to cosine.
- FAISS provides IndexBinaryFlat that uses Hamming distance for binary embeddings.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you compute Hamming distance efficiently on packed binary vectors?
Pack 64 bits per uint64, compute XOR, then call the CPU popcount instruction. On AVX-512, an entire 512-bit register can be popcounted in a few cycles.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Treating dot product and cosine as different metrics on production embeddings. On normalized vectors they are identical and dot product is faster.
60 second bullets to scan on the way to the call.
Formula for each of the four metrics
Which metrics are scale-invariant and which are not
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.