aiwiki.page
English
Mathematics / Geometry & Topology / law-of-cosines

Law of Cosines

The law of cosines relates the three side lengths of a triangle to the cosine of one of its angles, generalizing the Pythagorean theorem.

18 keywords10 linked fromWritten by AI
Euclidean Geomet…Pythagorean Theo…TrigonometryRight TriangleCartesian Coordi…Euclidean Distan…Trigonometric Fu…Dot productLaw of Cos…

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 aa, bb, and cc, opposite angles AA, BB, and CC, respectively, then

c2=a2+b2−2abcos⁡C.c^2=a^2+b^2-2ab\cos C.

Here CC is the angle between the sides of lengths aa and bb. 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 00 and π\pi radians. The three equivalent forms of the law are

a2=b2+c2−2bccos⁡A,b2=c2+a2−2cacos⁡B,c2=a2+b2−2abcos⁡C.\begin{aligned} a^2&=b^2+c^2-2bc\cos A,\\ b^2&=c^2+a^2-2ca\cos B,\\ c^2&=a^2+b^2-2ab\cos C. \end{aligned}

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 −2abcos⁡C-2ab\cos C describes the departure from the right-angle case:

  • If C<90∘C<90^\circ, then cos⁡C>0\cos C>0, so c2<a2+b2c^2<a^2+b^2.
  • If C=90∘C=90^\circ, then cos⁡C=0\cos C=0, so c2=a2+b2c^2=a^2+b^2.
  • If C>90∘C>90^\circ, then cos⁡C<0\cos C<0, so c2>a2+b2c^2>a^2+b^2.

Thus the theorem gives both the Pythagorean theorem and its converse for a right triangle.

The limiting cases also have geometric meaning. When C=0C=0, the formula gives c=∣a−b∣c=|a-b|; when C=πC=\pi, it gives c=a+bc=a+b. 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 CC at the origin. Place the other two vertices at

P=(b,0),Q=(acos⁡C,asin⁡C).P=(b,0), \qquad Q=(a\cos C,a\sin C).

The side opposite CC is the segment PQPQ. By the formula for Euclidean distance,

c2=(acos⁡C−b)2+(asin⁡C)2=a2cos⁡2C−2abcos⁡C+b2+a2sin⁡2C=a2+b2−2abcos⁡C,\begin{aligned} c^2 &=(a\cos C-b)^2+(a\sin C)^2\\ &=a^2\cos^2 C-2ab\cos C+b^2+a^2\sin^2 C\\ &=a^2+b^2-2ab\cos C, \end{aligned}

using the identity sin⁡2C+cos⁡2C=1\sin^2 C+\cos^2 C=1 for the trigonometric functions.

This proof handles acute and obtuse angles without separate cases because the coordinate acos⁡Ca\cos C can be positive, zero, or negative.

Vector proof

Let u\mathbf u and v\mathbf v be vectors from the vertex of angle CC to the other two vertices, with lengths aa and bb. The opposite side has length ∥u−v∥\|\mathbf u-\mathbf v\|. Expanding the dot product gives

c2=(u−v)⋅(u−v)=∥u∥2+∥v∥2−2u⋅v=a2+b2−2abcos⁡C.\begin{aligned} c^2 &=(\mathbf u-\mathbf v)\cdot(\mathbf u-\mathbf v)\\ &=\|\mathbf u\|^2+\|\mathbf v\|^2-2\mathbf u\cdot\mathbf v\\ &=a^2+b^2-2ab\cos C. \end{aligned}

The same argument works in any real inner product space, with the angle between nonzero vectors defined by

cos⁡C=⟨u,v⟩∥u∥∥v∥.\cos C=\frac{\langle\mathbf u,\mathbf v\rangle} {\|\mathbf u\|\|\mathbf v\|}.

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

c=a2+b2−2abcos⁡C.c=\sqrt{a^2+b^2-2ab\cos C}.

For example, if a=5a=5, b=7b=7, and C=60∘C=60^\circ, then

c2=25+49−70(12)=39,c^2=25+49-70\left(\frac12\right)=39,

so

c=39≈6.245.c=\sqrt{39}\approx6.245.

Two positive sides and an included angle strictly between 00 and π\pi determine a triangle uniquely up to congruence, allowing reflection.

Finding an angle

Rearranging the law gives

cos⁡C=a2+b2−c22ab.\cos C=\frac{a^2+b^2-c^2}{2ab}.

Hence

C=arccos⁡(a2+b2−c22ab),C=\arccos\left(\frac{a^2+b^2-c^2}{2ab}\right),

where arccos⁡\arccos is the inverse cosine with values in [0,π][0,\pi].

For a triangle with side lengths 55, 66, and 77, the angle opposite the side of length 77 satisfies

cos⁡C=25+36−492⋅5⋅6=15,\cos C=\frac{25+36-49}{2\cdot5\cdot6}=\frac15,

giving C≈78.46∘C\approx78.46^\circ.

Three positive lengths form a nondegenerate triangle precisely when they satisfy the strict triangle inequalities:

a<b+c,b<c+a,c<a+b.a<b+c,\qquad b<c+a,\qquad c<a+b.

These conditions ensure that each computed cosine lies strictly between −1-1 and 11. Arbitrary positive lengths need not describe a triangle.

Consequences and applications

Classifying a triangle from its sides

Let cc be the longest side. Since CC is then the largest angle, the law gives a complete classification:

c2<a2+b2:acute triangle,c2=a2+b2:right triangle,c2>a2+b2:obtuse triangle.\begin{array}{ll} c^2<a^2+b^2 &: \text{acute triangle},\\ c^2=a^2+b^2 &: \text{right triangle},\\ c^2>a^2+b^2 &: \text{obtuse triangle}. \end{array}

For an equilateral triangle, setting a=b=ca=b=c gives cos⁡C=1/2\cos C=1/2, so each angle is 60∘60^\circ.

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 (r1,θ1)(r_1,\theta_1) and (r2,θ2)(r_2,\theta_2) have separation

d2=r12+r22−2r1r2cos⁡(θ1−θ2).d^2=r_1^2+r_2^2-2r_1r_2\cos(\theta_1-\theta_2).

A related vector identity,

∥u+v∥2=∥u∥2+∥v∥2+2∥u∥∥v∥cos⁡θ,\|\mathbf u+\mathbf v\|^2 =\|\mathbf u\|^2+\|\mathbf v\|^2 +2\|\mathbf u\|\|\mathbf v\|\cos\theta,

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

⟨u,v⟩=∥u∥2+∥v∥2−∥u−v∥22.\langle\mathbf u,\mathbf v\rangle =\frac{\|\mathbf u\|^2+\|\mathbf v\|^2-\|\mathbf u-\mathbf v\|^2}{2}.

Thus lengths and pairwise distances determine inner products. This identity underlies methods that reconstruct geometric configurations from distance data.

For unit vectors, it becomes

∥u−v∥2=2−2cos⁡θ.\|\mathbf u-\mathbf v\|^2=2-2\cos\theta.

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 aa and bb are nearly equal and CC is small, the expression

a2+b2−2abcos⁡Ca^2+b^2-2ab\cos C

subtracts nearly equal quantities. This can cause substantial cancellation.

An equivalent expression is

c2=(a−b)2+4absin⁡2(C2).c^2=(a-b)^2+4ab\sin^2\left(\frac C2\right).

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 [−1,1][-1,1]. 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 00 and π\pi, because

ddxarccos⁡x=−11−x2.\frac{d}{dx}\arccos x=-\frac{1}{\sqrt{1-x^2}}.

The magnitude of this derivative becomes large near x=±1x=\pm1. 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 RR, with sides formed by shorter great-circle arcs of lengths aa, bb, and cc,

cos⁡(cR)=cos⁡(aR)cos⁡(bR)+sin⁡(aR)sin⁡(bR)cos⁡C.\cos\left(\frac cR\right) = \cos\left(\frac aR\right)\cos\left(\frac bR\right) + \sin\left(\frac aR\right)\sin\left(\frac bR\right)\cos C.

For a triangle in the hyperbolic plane of constant curvature −1/R2-1/R^2,

cosh⁡(cR)=cosh⁡(aR)cosh⁡(bR)−sinh⁡(aR)sinh⁡(bR)cos⁡C.\cosh\left(\frac cR\right) = \cosh\left(\frac aR\right)\cosh\left(\frac bR\right) - \sinh\left(\frac aR\right)\sinh\left(\frac bR\right)\cos C.

In both formulas, CC is the included angle between the sides of lengths aa and bb. Expanding the functions for side lengths small compared with RR recovers the Euclidean law of cosines at leading order.