Formula: Cosine Similarity & L2 Systems Engineering Formula
- a1: A1 (val)
- a2: A2 (val)
- a3: A3 (val)
- b1: B1 (val)
- b2: B2 (val)
- b3: B3 (val)
Compare two vectors using Cosine Similarity (A·B / ||A||·||B||) and L2 distance.
Use cosine when only the direction of a vector carries meaning, such as topic mix in text, and lengths vary for unrelated reasons. Use L2 distance when the size of each component matters, such as measured quantities. The A1 = 4.5 preset shows them disagreeing: cosine rises to 0.9952 while distance grows to 1.063.
It means an angle of roughly 18 degrees between the vectors. The lower preset gives 0.9477 and 18.62 degrees. Whether that counts as similar depends on the data. In 3D it is quite close; in a high-dimensional embedding space it might be a very strong match. Compare against other scores from the same model and data set, and pick a cut-off from examples you have labelled yourself.
Subtract the cosine similarity from 1. With the defaults, 1 minus 0.9933 is 0.0067. Use the four-decimal cosine in the explanation line rather than the rounded result, or you will get 0.01, which loses most of the detail. Cosine distance is zero for identical directions and 2 for opposite ones.
No. The angle between two vectors is measured the short way round, from 0 up to a straight half turn. The page computes it with the arccosine, whose output always falls in that range, so you never see a reflex angle. The half turn itself means the vectors point in exactly opposite directions, as when B is set to the negative of A, where the cosine reads minus 1.
The dot product includes both vector lengths. At the default it is 27.5, while the cosine is 0.9933. Divide the dot product by the two norms, about 5.220 and 5.304, and you get the cosine. Doubling either vector doubles the dot product but leaves the cosine unchanged.
No. The dot product, cosine similarity, angle and L2 distance are all symmetric, so swapping the two vectors returns the same four figures. Only the sign of each component difference changes inside the distance formula, and squaring removes it. You can enter whichever vector you like as A.
Set the third component of both vectors to 0. The formulas then reduce to the 2D versions exactly, because a zero component adds nothing to the dot product, the norms or the squared differences. For example, A = (1, 0, 0) and B = (0, 1, 0) gives the familiar 90 degree angle between the x and y axes.
Because cosine similarity is undefined for it. The formula divides by the vector's length, and a zero vector has length zero and no direction. The page shows a message asking for at least one non-zero component instead of a misleading number. The L2 distance would still equal the other vector's length, which you can compute by hand.
You can, with the three channels as components. L2 distance gives a simple measure of colour difference, and cosine tells you whether two colours have the same hue mix at different brightness. Neither matches human colour perception closely, so treat the result as a rough check, not a print or design standard. Two greys of different brightness, for instance, score a cosine of 1.
Near 0 degrees the cosine curve is almost flat. Going from the default to the lower preset moves the angle from 6.65 to 18.62 degrees, nearly three times larger, while the cosine only falls from 0.9933 to 0.9477. For close matches the angle line is the more sensitive figure, and the easier one to explain to someone else.
No, the formulas handle them correctly. They are also the only way to reach negative cosine values. With all-positive inputs, both vectors sit in the same corner of space, so the angle can never exceed 90 degrees and the cosine never drops below 0.
The default compares A = (3, 4, 1.5) with B = (3.5, 3.8, 1.2). Each number is one component, so you can read them as three features of two items, or as two arrows in ordinary 3D space.
The page reports four figures. The dot product A · B is 27.5. Cosine similarity is 0.9933, shown in the result line rounded to 0.99. The angle between the arrows is 6.65 degrees. The Euclidean, or L2, distance between their tips is 0.616.
All four agree on the story: these two vectors point in almost the same direction and sit close together. A cosine of 1 would mean identical direction, 0 would mean the arrows are at right angles, and minus 1 would mean they point exactly opposite. At 0.9933 you are very near the top of that scale.
The page description also mentions cosine distance, defined as 1 minus cos θ. It is not printed as its own line, but it is a single subtraction: 1 minus 0.9933 leaves 0.0067. Some vector search tools report that figure instead of the similarity, where smaller means closer.
The dot product multiplies matching components and adds them. 3 × 3.5 is 10.5, 4 × 3.8 is 15.2, and 1.5 × 1.2 is 1.8. Their sum is 27.5, as the page shows.
Each length, or L2 norm, is the square root of the sum of squared components. For A that is the square root of 9 + 16 + 2.25, which is the root of 27.25, about 5.220. For B it is the root of 12.25 + 14.44 + 1.44, which is the root of 28.13, about 5.304.
Cosine similarity divides the dot product by the product of the lengths. 5.220 × 5.304 is about 27.69, and 27.5 divided by that is 0.9933. The angle is the arccosine of that value, 6.65 degrees.
The distance is a different calculation. Subtract B from A component by component to get (minus 0.5, 0.2, 0.3). Square and add: 0.25 + 0.04 + 0.09 is 0.38. The square root is 0.616. If your own hand result differs, the usual culprit is forgetting to square a negative difference, or taking the root of the dot product instead of the sum of squares.
The three presets show the most important difference between the two measures. They change only A1, the first component of A, while B stays fixed.
Dropping A1 to 1.8 gives cos θ of 0.9477, an angle of 18.62 degrees and an L2 distance of 1.738. Both measures say the vectors moved apart, which makes sense: A now leans away from B.
Raising A1 to 4.5 is more interesting. Cosine similarity goes up, to 0.9952, and the angle shrinks to 5.64 degrees. Yet the L2 distance also goes up, to 1.063, from 0.616 at the default. A now points even more nearly along B, but it has grown longer, so its tip is further from B's tip. Cosine sees the first effect only. L2 sees both.
You can push this to the extreme. Set B to (6, 8, 3), exactly twice A. The page returns a cosine of 1 and an angle of 0, because the direction is identical, while the L2 distance is 5.22, the full length of A. Make B ten times A and cosine still says 1, while L2 climbs to 46.98.
This is why text search often uses cosine. A long document and a short one about the same topic can have very different vector lengths but similar directions. When magnitude carries meaning, such as quantities or intensities, L2 is the more honest measure.
Three special cases are worth running once to calibrate your eye.
Opposite: set B to (minus 3, minus 4, minus 1.5), the negative of A. The page shows cos θ of minus 1, a straight angle (a half turn), a dot product of minus 27.25 and an L2 distance of 10.44, which is twice the length of A. The vectors are as dissimilar as cosine can measure.
Perpendicular: set A to (1, 0, 0) and B to (0, 1, 0). The dot product is 0, cosine is 0, the angle is 90 degrees and the distance is 1.414, the square root of 2. In feature terms, the two items share nothing in common along any axis.
Identical direction: any B that is a positive multiple of A gives a cosine of 1 and an angle of 0, as shown above. Only B equal to A exactly also gives an L2 distance of 0.
Negative cosine values come up only when components can be negative. Word counts and other non-negative features never produce them, so for those inputs cosine similarity ranges from 0 to 1.
Cosine similarity is undefined when either vector has zero length, because the formula divides by the product of the norms. A vector of all zeros has no direction, so there is no angle to measure and no similarity to report.
The page handles this honestly. Set A to (0, 0, 0) and it shows no result, only a message that cosine similarity is undefined for a zero vector and that each vector needs at least one non-zero component. A single non-zero value is enough: A = (1, 0, 0) is a valid direction along the first axis.
The other measures would still have meaning. The dot product with a zero vector is 0, and the L2 distance would simply be the length of the other vector, since the zero vector sits at the origin. For the default B that length is about 5.3. Work those out by hand if you need them.
In real data, zero vectors come from empty documents, missing features or a row that failed to load. Libraries differ in how they treat them: some return 0, some return an error, some return a blank. Check what your own tool does, because a silent 0 would look like two perpendicular vectors.
The headline value rounds to two decimals. For the higher preset, the true cosine is 0.9952, and the result line shows 1. The explanation under the result keeps four decimals and the angle line shows 5.64 degrees, so the vectors are clearly not parallel.
This matters when you are comparing near neighbours. In embedding search, the useful differences between good and great matches often sit in the third or fourth decimal place. Two candidates at 0.9933 and 0.9952 both display as roughly 1 here, yet their angles, 6.65 and 5.64 degrees, are visibly different.
The angle is often the easier figure to compare. Cosine values bunch up near 1 for similar vectors, because the cosine curve is flat at small angles. A change of one degree there barely moves the cosine but is plain to see in the angle line. If you need to rank close matches, read the four-decimal cosine in the explanation or the angle, not the rounded headline.
Many embedding models return vectors already scaled to length 1. For unit vectors there is a fixed link between the two measures: the squared L2 distance equals 2 times (1 minus cos θ). That means sorting neighbours by cosine similarity, highest first, gives the same order as sorting by L2 distance, lowest first.
You can test the idea with this page by entering unit vectors yourself. The perpendicular case above, (1, 0, 0) and (0, 1, 0), has a cosine of 0 and an L2 distance of 1.414. Squaring 1.414 gives about 2, and 2 times (1 minus 0) is 2. It matches.
If your vectors are not normalised, the two rankings can disagree, as the A1 = 4.5 preset shows: cosine called it closer than the default while L2 called it further away. So before switching a search system between the two, find out whether the stored vectors have unit length.
The dot product alone is a third option. For unit vectors it equals the cosine, and some systems use it directly to save the division. For vectors of mixed length it rewards long vectors, which is rarely what you want for similarity.
This calculator takes exactly three components per vector. That suits geometry problems, 3D direction vectors, RGB colour values and small feature sets for teaching. Real text or image embeddings usually have far more dimensions, and you cannot paste those here.
The formulas do not change with length. The dot product is still a sum of products, each norm is still the root of a sum of squares, and the distance is still the root of summed squared differences. A spreadsheet or a few lines of code will apply them to any length.
What does change is intuition. In high-dimensional spaces, random vectors tend to sit at angles close to 90 degrees from one another, so cosine values that look low in 3D can be meaningful there. Judge a similarity score against other scores from the same model and data, not against the 3D examples on this page.
Use this page to check a hand calculation, to see how a single component change moves each measure, or to explain to a colleague why cosine and distance disagree on a pair.