Cosine works in any dimension
The same dot-over-magnitudes formula compares 2D arrows, 3D forces, or 1,536-D text embeddings—angle is dimension-agnostic.
Formula: θ = arccos\((a·b)/(||a||·||b||)\) for non-zero vectors. We clamp the ratio to [-1,1] to avoid NaN from rounding.
Press Enter in any field to compute.
In 2D mode, shows a, b, and the measured angle.
The angle measures alignment. cos θ = (a·b)/(||a||·||b||) equals 1 when perfectly aligned, −1 when opposite, and 0 when perpendicular. This metric is widely used as “cosine similarity.”
[-1,1] before arccos.
The angle between two vectors captures how strongly they point in the same direction.
If vectors a and b are both nonzero, the angle θ is defined by the dot-product identity
a · b = ||a||·||b||·cos θ. Solving for θ gives
θ = arccos((a · b)/(||a||·||b||)). This value lies in [0, π] radians (i.e., [0°, 180°]):
0° means perfectly aligned, 90° means perpendicular, and 180° means opposite directions.
The quantity (a · b)/(||a||·||b||) is called cosine similarity. It ranges from −1 to 1
and measures alignment independently of vector length. In data science and information retrieval, cosine similarity compares
text embeddings; in robotics and graphics, it measures orientation agreement between directions or normals.
Floating-point rounding can push the ratio slightly outside [−1, 1], which would make arccos undefined.
Robust implementations clamp the ratio to [−1, 1] before taking arccos. Note that the angle is
undefined if either vector has zero magnitude—there is no meaningful direction to compare.
proj_b(a) = (a·b / ||b||²) b isolates the “along-b” part of a.
A small projection relative to ||a|| indicates a near-right angle.||a × b|| = ||a||·||b||·sin θ. Large cross magnitude means a large sine, hence an angle near 90°.
Let a = (3, 1) and b = (2, 4). Then a · b = 3·2 + 1·4 = 10,
||a|| = √(3² + 1²) = √10, and ||b|| = √(2² + 4²) = √20.
Cosine similarity is 10 / (√10 · √20) = 10 / √200 = 10 / (10√2) = 1/√2 ≈ 0.7071.
Therefore θ = arccos(0.7071) ≈ 0.7854 rad = 45°.
||a|| = 0 or ||b|| = 0, the angle is undefined.
You’ll meet vector angles in physics (work = F · d uses the cosine of the angle), computer graphics (lighting, normal comparisons),
navigation (bearing differences), sports analytics (motion direction), and machine learning (similarity between high-dimensional embeddings).
Because the concept is unitless and purely geometric, it is a reliable, widely applicable way to quantify directional agreement.
Tip: For diagnostics, look at both the angle and the cosine similarity—together they reveal not only whether vectors are aligned, but how strongly they align relative to their lengths.
The same dot-over-magnitudes formula compares 2D arrows, 3D forces, or 1,536-D text embeddings—angle is dimension-agnostic.
Multiply either vector by a positive number and the angle stays identical. That’s why cosine similarity ignores “loudness” and keeps only direction.
In 3D, ||a×b|| = ||a||·||b||·sin θ. Tiny cross magnitude? Nearly parallel. Max cross magnitude? Near 90° and the parallelogram area is huge.
In 2D, the scalar “cross” aₓbᵧ − aᵧbₓ is positive if you rotate counter-clockwise from a to b, negative if clockwise—a quick left/right test.
Smooth rotations (spherical linear interpolation) literally move along the angle between directions. Flight sims and VR cameras use this for buttery turns.