Angle Between Two Vectors
Vectors & Settings
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.
Preview (2D)
In 2D mode, shows a, b, and the measured angle.
About the Angle Between Vectors
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.”
- Undefined when any vector is zero. You still get dot product and magnitudes.
- Units: Dot products carry squared units if vectors have units; the angle is unitless (radians/degrees).
- Numeric safety: The ratio is clamped to
[-1,1]beforearccos.
Angle Between Two Vectors — Concepts, Examples, and Pitfalls
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.
Cosine Similarity (Normalized Alignment)
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.
Computing Safely (Numerical Details)
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.
Geometric Intuition
- Dot product sign: positive → acute angle; negative → obtuse; near zero → close to perpendicular.
- Projection view:
proj_b(a) = (a·b / ||b||²) bisolates the “along-b” part ofa. A small projection relative to||a||indicates a near-right angle. - 3D cross link: In 3D,
||a × b|| = ||a||·||b||·sin θ. Large cross magnitude means a large sine, hence an angle near90°.
Step-by-Step Example (2D)
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°.
Common Pitfalls
- Zero vectors: If
||a|| = 0or||b|| = 0, the angle is undefined. - Units & scaling: Multiplying a vector by a positive scalar does not change the angle; mixing units (e.g., meters vs. feet) can distort interpretations.
- Rounding: For nearly parallel or nearly opposite vectors, use adequate precision to avoid large angular errors.
Where It’s Used
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.