The law of cosines is a theorem in Euclidean geometry relating the lengths of a triangle’s sides to the cosine of one of its angles. If a triangle has side lengths , , and , opposite angles , , and , respectively, then
Here is the angle between the sides of lengths and . The theorem applies to acute, right, and obtuse triangles and generalizes the Pythagorean theorem. It is a basic tool in trigonometry for finding an unknown side from two sides and their included angle, or an unknown angle from three sides.
Statement and interpretation
For a nondegenerate triangle, the side lengths are positive and each interior angle lies strictly between and radians. The three equivalent forms of the law are
Each formula uses two sides and the angle between those sides to determine the opposite side. Substituting a nonincluded angle generally gives an incorrect result.
The term describes the departure from the right-angle case:
- If , then , so .
- If , then , so .
- If , then , so .
Thus the theorem gives both the Pythagorean theorem and its converse for a right triangle.
The limiting cases also have geometric meaning. When , the formula gives ; when , it gives . These are degenerate configurations in which the vertices lie on a straight line.
Proofs
Coordinate proof
Choose a Cartesian coordinate system with the vertex of angle at the origin. Place the other two vertices at
The side opposite is the segment . By the formula for Euclidean distance,
using the identity for the trigonometric functions.
This proof handles acute and obtuse angles without separate cases because the coordinate can be positive, zero, or negative.
Vector proof
Let and be vectors from the vertex of angle to the other two vertices, with lengths and . The opposite side has length . Expanding the dot product gives
The same argument works in any real inner product space, with the angle between nonzero vectors defined by
Consequently, the identity is not restricted to a two-dimensional coordinate system, although any three noncollinear points in Euclidean space lie in a plane.
Solving triangles
Finding a side
When two side lengths and their included angle are known, the opposite side is
For example, if , , and , then
so
Two positive sides and an included angle strictly between and determine a triangle uniquely up to congruence, allowing reflection.
Finding an angle
Rearranging the law gives
Hence
where is the inverse cosine with values in .
For a triangle with side lengths , , and , the angle opposite the side of length satisfies
giving .
Three positive lengths form a nondegenerate triangle precisely when they satisfy the strict triangle inequalities:
These conditions ensure that each computed cosine lies strictly between and . Arbitrary positive lengths need not describe a triangle.
Consequences and applications
Classifying a triangle from its sides
Let be the longest side. Since is then the largest angle, the law gives a complete classification:
For an equilateral triangle, setting gives , so each angle is .
Distances and vector magnitudes
The theorem calculates the distance between two points when their distances from a common observation point and the angle between their directions are known. This is useful in surveying, navigation, and geometric reconstruction.
In polar coordinates, points with coordinates and have separation
A related vector identity,
determines the magnitude of a resultant when two vectors and the angle between them are known. It is widely used for forces, velocities, and displacements in classical mechanics.
Recovering inner products from distances
Rearranging the vector form yields
Thus lengths and pairwise distances determine inner products. This identity underlies methods that reconstruct geometric configurations from distance data.
For unit vectors, it becomes
Consequently, Euclidean distance and cosine similarity are directly related when vectors have been normalized.
Numerical considerations
A mathematically correct formula can lose accuracy when evaluated using floating-point arithmetic. In particular, if and are nearly equal and is small, the expression
subtracts nearly equal quantities. This can cause substantial cancellation.
An equivalent expression is
It avoids that particular subtraction and is often more suitable for small angles, although overflow, underflow, and the accuracy of the input data still matter.
When recovering an angle, rounding may produce a cosine value slightly outside . Restricting such a value to that interval can handle small rounding errors, but should not conceal side lengths that genuinely violate the triangle inequalities.
Angle recovery is also sensitive near and , because
The magnitude of this derivative becomes large near . This sensitivity is an intrinsic conditioning issue, distinct from the numerical stability of a particular implementation.
Historical development
Geometric equivalents of the law appear in Book II of Euclid’s *Elements*, compiled around 300 BCE. Propositions 12 and 13 treat obtuse and acute triangles through relations among squares, rectangles, and projections. They do not use modern trigonometric notation.
The development of trigonometric calculation later made the relation a practical tool for solving triangles. The fifteenth-century mathematician Jamshīd al-Kāshī gave a formulation equivalent to the modern rule. The name Al-Kashi’s theorem remains in use in some mathematical traditions, especially French-language education.
Analogues in curved geometry
The Euclidean formula does not apply unchanged to triangles whose sides are curved-surface shortest paths. In non-Euclidean geometry, corresponding cosine laws incorporate curvature.
For a spherical triangle on a sphere of radius , with sides formed by shorter great-circle arcs of lengths , , and ,
For a triangle in the hyperbolic plane of constant curvature ,
In both formulas, is the included angle between the sides of lengths and . Expanding the functions for side lengths small compared with recovers the Euclidean law of cosines at leading order.