Calculus, Linear Algebra & Discrete Math Vectors & Vector Operations Dot product identity a·b = |a||b|cos θ

Angle Between Two Vectors Calculator

Enter the components of two vectors and this calculator returns the angle between them in both degrees and radians, along with everything the formula needs on the way: the dot product, both magnitudes and the value of cos θ. It also reports the acute angle between the two lines the vectors lie along, which is the number you want whenever direction is irrelevant, and it says plainly whether the pair is acute, perpendicular, obtuse or parallel. Leave the z components at zero for a two-dimensional problem.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
aₓFirst component of vector a.3
aᵧSecond component of vector a.4
a_zThird component of vector a. Leave at 0 for a 2D problem.0
bₓFirst component of vector b.0
bᵧSecond component of vector b.4
b_zThird component of vector b. Leave at 0 for a 2D problem.3

It returns

  • Angle between the vectors — Measured with both tails at the same point; always between 0° and 180°.
  • Angle in radians
  • Angle between the lines — The acute version, min(θ, 180° − θ), for when direction does not matter.
  • cos θ — Also known as the cosine similarity of the two vectors.
  • Dot product a·b
  • |a|
  • |b|

The formula

θ=arccos(ab|a||b|)
ab=|a||b|cosθ

In plain text: θ = arccos( (a·b) / (|a| |b|) )

  • θAngle between the vectors, from 0° to 180° (degrees or radians)
  • a·bDot product: aₓbₓ + a_yb_y + a_zb_z
  • |a|Magnitude of a: √(aₓ² + a_y² + a_z²)
  • |b|Magnitude of b, computed the same way

Valid in any number of dimensions. The ratio always lies in [−1, 1] by the Cauchy–Schwarz inequality, so the arccosine is always defined.

Updated Category Vectors & Vector Operations Verified against published test cases Reading time 10 min

What the angle between two vectors means

The angle between two vectors is the angle you would measure at the point where their tails meet, taken in the plane the two of them span. It runs from 0° when they point the same way to 180° when they point opposite ways, and it is 90° exactly when they are perpendicular. Two vectors always lie in a common plane, however many dimensions they live in, which is why the question has an answer at all in three dimensions and beyond.

The quantity that carries the answer is the dot product. Its defining geometric identity is

a · b = |a| |b| cos θ

and rearranging for cos θ is the whole method. The dot product is computed from coordinates without knowing the angle, and the magnitudes are computed by Pythagoras, so the angle falls out of arithmetic that never mentions geometry.

Note carefully what the sign tells you before you take any arccosine. A positive dot product means an acute angle, zero means perpendicular, negative means obtuse. In mechanics that sign is often all you need: work is F · d, so a force with a negative dot product against the displacement is taking energy out of the system rather than putting it in.

Why the formula works, and why arccos is safe here

Write the law of cosines for the triangle formed by a, b and b − a:

|b − a|² = |a|² + |b|² − 2|a||b| cos θ

Now expand the left side in coordinates. Every squared term reproduces |a|² and |b|², and what is left over is exactly −2(aₓbₓ + aₖbₖ + a₣b₣). Comparing the two expressions gives the identity a · b = |a||b|cos θ directly - the dot product is not a definition dropped from nowhere, it is what the law of cosines forces.

Dividing by the two magnitudes normalises both vectors to unit length, so cos θ depends only on direction. Scale either vector by any positive number and the angle is unchanged; scale by a negative number and the angle becomes its supplement, because reversing an arrow swings it through 180°.

The Cauchy-Schwarz inequality guarantees that |a · b| ≤ |a||b|, so the ratio always lies in [−1, 1] and the arccosine is always defined. In floating-point arithmetic that guarantee can be violated in the last bit for nearly parallel vectors, which is why this calculator clamps the ratio before taking the arccosine - without the clamp, a ratio of 1.0000000000000002 returns NaN.

Arccos does lose precision for very small angles, because the cosine is flat near θ = 0: an error of 10−16 in the cosine becomes an error of about 10−8 radians in the angle. Where tiny angles matter - alignment tolerances, collinearity tests - the numerically better route is θ = atan2(|a × b|, a · b), which uses the cross product and stays accurate across the whole range.

Worked example: a = (3, 4, 0) and b = (0, 4, 3)

Both vectors are 3-4-5 right triangles laid in different planes, which makes every number in this example exact until the final arccosine.

  1. Dot product. a · b = (3)(0) + (4)(4) + (0)(3) = 0 + 16 + 0 = 16.
  2. Magnitude of a. |a| = √(3² + 4² + 0²) = √(9 + 16) = √25 = 5.
  3. Magnitude of b. |b| = √(0 + 16 + 9) = √25 = 5.
  4. Cosine. cos θ = 16 / (5 × 5) = 16/25 = 0.64.
  5. Angle. θ = arccos(0.64) = 0.876298 radians. Multiply by 180/π = 57.29578: 0.876298 × 57.29578 = 50.21°.

The dot product is positive, so the answer had to come out below 90° - a useful check before you reach for a calculator. Since θ is already acute, the angle between the two lines carrying these vectors is the same 50.21°.

Change b to (0, −4, −3) and every step repeats with the sign flipped: the dot product becomes −16, the cosine −0.64, and the angle 180 − 50.21 = 129.79°. The two arrows now point broadly apart, but the lines they lie along are unchanged, and the line angle is still 50.21°. That is the distinction the calculator's line-angle output exists to make.

Reading the result

Decide first whether you want the angle between the vectors or between the lines. Vectors are directed, so their angle spans the full 0° to 180°. Lines are not, so two intersecting lines make two supplementary angles and convention takes the acute one, 0° to 90°. Surveying bearings, crystal-plane angles and the angle between two edges of a solid are line angles; force directions, velocities and surface normals are vector angles.

Cosine values worth recognising by sight: 1 is parallel, √3/2 ≈ 0.866 is 30°, √2/2 ≈ 0.707 is 45°, 0.5 is 60°, 0 is perpendicular, and the negatives of those are their supplements. In data work the cosine itself is often the answer rather than the angle: cosine similarity between two feature vectors is precisely this ratio, and practitioners compare 0.91 against 0.87 without ever converting to degrees.

Watch the conditioning. When two vectors are nearly parallel, the cosine sits on a flat part of the curve and small errors in the components produce disproportionately large errors in the angle. Near 90°, the opposite holds - the cosine changes fastest there, so the angle is well determined. If you need an alignment check to a fraction of a degree, use the cross-product route described above rather than the arccosine.

Also check the units of what you are comparing. The formula is dimensionless only if both vectors carry the same units in every component. Mixing metres in one component with millimetres in another silently rotates the vector, and the resulting angle is meaningless - a common error when reading coordinates from a mixed-unit drawing.

Reference: cosine values and the angles they correspond to

Exact cosine values for the standard angles, with the corresponding line angle.
cos θθ (degrees)θ (radians)Angle between the linesRelationship
10Parallel, same direction
√3/2 ≈ 0.866030°π/6 ≈ 0.523630°Acute
√2/2 ≈ 0.707145°π/4 ≈ 0.785445°Acute
0.560°π/3 ≈ 1.047260°Acute
090°π/2 ≈ 1.570890°Perpendicular, dot product zero
−0.5120°2π/3 ≈ 2.094460°Obtuse
−√2/2 ≈ −0.7071135°3π/4 ≈ 2.356245°Obtuse
−√3/2 ≈ −0.8660150°5π/6 ≈ 2.618030°Obtuse
−1180°π ≈ 3.1416Antiparallel

The line angle in the fourth column is min(θ, 180° − θ), which is what you want whenever direction is irrelevant.

Mistakes that give a plausible wrong angle

  • Using position instead of direction. To find the angle at vertex B of a triangle, you need the vectors BA and BC - both starting at B. Using the position vectors OA and OC measures the angle at the origin instead, which is a different number entirely.
  • Forgetting to normalise. The dot product on its own is not a cosine. Doubling one vector doubles the dot product while leaving the angle untouched, so the division by both magnitudes is not optional.
  • Reading radians as degrees. Every programming language's acos returns radians. The 50.21° in the worked example is 0.8763 radians, and mistaking one for the other is the most common error in student code.
  • Reporting an obtuse angle where a line angle was wanted. If the answer exceeds 90° and the question was about lines rather than arrows, subtract from 180°.
  • Feeding a zero vector. The origin has no direction. The formula divides by zero and the calculator returns nothing rather than a misleading 90°.
  • Mixing units between components. Every component of a vector must be in the same unit, or the direction itself is wrong before any angle is computed.

Where this calculation shows up

In mechanics, the work done by a constant force is W = F · d = |F||d|cos θ, so the angle is what converts a force into the component that actually does work. In lighting and rendering, Lambert's cosine law makes surface brightness proportional to the cosine of the angle between the surface normal and the light direction - which is why a single dot product of two unit vectors is the innermost operation in a shading loop.

In information retrieval and machine learning, cosine similarity between document or embedding vectors is exactly the quantity computed here, chosen over Euclidean distance precisely because it ignores magnitude and compares direction alone. In crystallography, the angle between lattice planes; in navigation, the angle between a heading and a track; in surveying, the angle between two sight lines - all the same arithmetic.

The complementary operation is the cross product, whose magnitude is |a||b| sin θ. Together they pin the angle down without ambiguity, and their ratio gives the tangent. If you want not the angle but the amount of one vector lying along another, the vector projection is the right tool, and it is built from the same dot product. For converting the answer between units, the degrees-to-radians calculator handles the 180/π factor, and for the distance between the two tips rather than the angle between them, use the distance formula.

Frequently asked questions

How do I find the angle at a vertex of a triangle?

Build two vectors that both start at that vertex. For the angle at B in triangle ABC, use BA = A − B and BC = C − B, then enter those two component sets. Subtracting in the wrong order reverses a vector and gives you the supplement, so check that both vectors point away from the vertex you are measuring at.

Can I use this for 2D vectors?

Yes - leave both z components at zero and everything works unchanged. The formula never assumes three dimensions; it is the same expression in any number of them, which is why the same calculation works for 300-dimensional embedding vectors in machine learning as for arrows on a page.

What is the difference between the angle between vectors and the angle between lines?

Vectors carry direction, so their angle runs from 0° to 180°. Lines do not, so a pair of intersecting lines forms two supplementary angles, and the convention is to quote the acute one, between 0° and 90°. When the vector angle exceeds 90°, the line angle is 180° minus it. Use the vector angle for forces and velocities, and the line angle for edges, bearings and geometric configurations.

Why is my answer in radians?

Because arccos returns radians in essentially every programming language and scientific calculator in radian mode. Multiply by 180/π = 57.29578 to get degrees. This calculator reports both, so you can copy whichever your application needs - but if you are writing your own code, check the mode before comparing against a printed table.

What does a negative dot product mean?

That the angle is obtuse: the vectors point broadly in opposite directions and the projection of either onto the other is negative. In physics that sign is meaningful on its own - a force with a negative dot product against displacement removes energy, as friction does. You do not need the arccosine to read it.

Why do I get an error for a nearly parallel pair?

You should not - the calculator clamps the cosine ratio into [−1, 1] before taking the arccosine, which is what prevents a floating-point value of 1.0000000000000002 from producing NaN. What does degrade near parallel is precision: the cosine is flat there, so the angle is poorly determined. Use the cross-product formula θ = atan2(|a × b|, a · b) when small angles matter.

Does the order of the two vectors matter?

No. The dot product is commutative and both magnitudes appear symmetrically, so swapping a and b gives an identical angle. This is unlike the cross product, which reverses sign when you swap the operands. It also means the angle carries no sense of rotation - if you need a signed angle in a plane, use atan2 on the components instead.

How is this related to cosine similarity?

Cosine similarity is the cos θ value this calculator reports. Text and embedding vectors are compared by that number directly, on a scale where 1 means identical direction, 0 means unrelated and −1 means opposite. Converting to degrees adds nothing for that use, which is why the raw cosine is what gets reported in machine-learning work.

References

  • Calculus: Early Transcendentals, 9th ed. (Section 12.3, The Dot Product) — James Stewart, Daniel Clegg and Saleem Watson, Cengage Learning
  • Introduction to Linear Algebra, 6th ed. (Chapter 1, Vectors and Dot Products) — Gilbert Strang, Wellesley-Cambridge Press
  • Accuracy and Stability of Numerical Algorithms, 2nd ed. — Nicholas J. Higham, SIAM