In trigonometry, the **law of cosines** (also known as the **cosine formula** or **cosine rule**) relates the lengths of the sides of a triangle to the cosine of one of its angles. For a triangle with sides and opposite respective angles and (see Fig. 1), the law of cosines states:

The law of cosines generalizes the Pythagorean theorem, which holds only for right triangles: if is a right angle then and the law of cosines reduces to

The law of cosines is useful for solving a triangle when all three sides or two sides and their included angle are given.

The theorem is used in solution of triangles, i.e., to find (see Figure 3):

- the third side of a triangle if two sides and the angle between them is known:
- the angles of a triangle if the three sides are known:
- the third side of a triangle if two sides and an angle opposite to one of them is known (this side can also be found by two applications of the law of sines):
^{[a]}

These formulas produce high round-off errors in floating point calculations if the triangle is very acute, i.e., if *c* is small relative to *a* and *b* or *γ* is small compared to 1. It is even possible to obtain a result slightly greater than one for the cosine of an angle.

The third formula shown is the result of solving for *a* in the quadratic equation *a*^{2} − 2*ab* cos *γ* + *b*^{2} − *c*^{2} = 0. This equation can have 2, 1, or 0 positive solutions corresponding to the number of possible triangles given the data. It will have two positive solutions if *b* sin *γ* < *c* < *b*, only one positive solution if *c* = *b* sin *γ*, and no solution if *c* < *b* sin *γ*. These different cases are also explained by the side-side-angle congruence ambiguity.

Book II of Euclid's *Elements*, compiled c. 300 BC from material up to a century or two older, contains a geometric theorem corresponding to the law of cosines but expressed in the contemporary language of rectangle areas; Hellenistic trigonometry developed later, and sine and cosine per se first appeared centuries afterward in India.

The cases of obtuse triangles and acute triangles (corresponding to the two cases of negative or positive cosine) are treated separately, in Propositions II.12 and II.13:^{[1]}

Proposition 12.

In obtuse-angled triangles the square on the side subtending the obtuse angle is greater than the squares on the sides containing the obtuse angle by twice the rectangle contained by one of the sides about the obtuse angle, namely that on which the perpendicular falls, and the straight line cut off outside by the perpendicular towards the obtuse angle.

Proposition 13 contains an analogous statement for acute triangles. In his (now-lost and only preserved through fragmentary quotations) commentary, Heron of Alexandria provided proofs of the converses of both II.12 and II.13.^{[2]}

Using notation as in Fig. 2, Euclid's statement of proposition II.12 can be represented more concisely (though anachronistically) by the formula

To transform this into the familiar expression for the law of cosines, substitute and

Proposition II.13 was not used in Euclid's time for the solution of triangles, but later it was used that way in the course of solving astronomical problems by al-Bīrūnī (11th century) and Johannes de Muris (14th century).^{[3]} Something equivalent to the spherical law of cosines was used (but not stated in general) by al-Khwārizmī (9th century), al-Battānī (9th century), and Nīlakaṇṭha (15th century).^{[4]}

Jamshīd al-Kāshī, a 15th century Persian mathematician and astronomer who computed the most accurate trigonometric tables of his era, wrote about the solution of triangles in his *Miftāḥ al-ḥisāb* (*Key of Arithmetic*, 1427), including the following method for finding the third side given two sides and their included angle:^{[5]}

Another caseis when two sides and the angle between them are known and the rest are unknown. We multiply one of the sides by the sine of the [known] angle one time and by the sine of its complement the other time converted and we subtract the second result from the other side if the angle is acute and add it if the angle is obtuse. We then square the result and add to it the square of the first result. We take the square root of the sum to get the remaining side....— Al-Kāshī'sMiftāḥ al-ḥisāb,

translation by Nuh Aydin, Lakhdar Hammoudi, and Ghada Bakbouk^{[6]}

Using modern algebraic notation and conventions this might be written

when is acute or

when is obtuse. (When is obtuse, the modern convention is that is negative and is positive; historically sines and cosines were considered to be line segments with non-negative lengths.) By squaring both sides, expanding the squared binomial, and then applying the Pythagorean trigonometric identity we obtain the familiar law of cosines:

In France, the law of cosines is sometimes referred to as the *théorème d'Al-Kashi*.^{[7]}^{[8]}

Al-Kāshī's method is essentially the same as the method recommended for solving such triangles by Naṣīr al-Dīn al-Ṭūsī's *Kitāb al-Shakl al-qattāʴ* (*Book on the Complete Quadrilateral*, c. 1250), but with the steps described explicitly instead of leaving the details to the reader.^{[9]}

The theorem was first written using algebraic notation by François Viète in the 16th century. At the beginning of the 19th century, modern algebraic notation allowed the law of cosines to be written in its current symbolic form.^{[10]}

Euclid proved this theorem by applying the Pythagorean theorem to each of the two right triangles in Fig. 2 (*AHB* and *CHB*). Using *d* to denote the line segment *CH* and *h* for the height *BH*, triangle *AHB* gives us

and triangle *CHB* gives

Expanding the first equation gives

Substituting the second equation into this, the following can be obtained:

This is Euclid's Proposition 12 from Book 2 of the *Elements*.^{[11]} To transform it into the modern form of the law of cosines, note that

Euclid's proof of his Proposition 13 proceeds along the same lines as his proof of Proposition 12: he applies the Pythagorean theorem to both right triangles formed by dropping the perpendicular onto one of the sides enclosing the angle *γ* and uses the square of a difference to simplify.

Using more trigonometry, the law of cosines can be deduced by using the Pythagorean theorem only once. In fact, by using the right triangle on the left hand side of Fig. 6 it can be shown that:

using the trigonometric identity

This proof needs a slight modification if *b* < *a* cos(*γ*). In this case, the right triangle to which the Pythagorean theorem is applied moves *outside* the triangle *ABC*. The only effect this has on the calculation is that the quantity *b* − *a* cos(*γ*) is replaced by *a* cos(*γ*) − *b*. As this quantity enters the calculation only through its square, the rest of the proof is unaffected. However, this problem only occurs when *β* is obtuse, and may be avoided by reflecting the triangle about the bisector of *γ*.

Referring to Fig. 6 it is worth noting that if the angle opposite side *a* is *α* then:

This is useful for direct calculation of a second angle when two sides and an included angle are given.

The altitude through vertex C is a segment perpendicular to side c. The distance from the foot of the altitude to vertex A plus the distance from the foot of the altitude to vertex B is equal to the length of side c (see Fig. 5). Each of these distances can be written as one of the other sides multiplied by the cosine of the adjacent angle,^{[12]}

(This is still true if *α* or *β* is obtuse, in which case the perpendicular falls outside the triangle.) Multiplying both sides by *c* yields

The same steps work just as well when treating either of the other sides as the base of the triangle:

Taking the equation for and subtracting the equations for and

This proof is independent of the Pythagorean theorem, insofar as it is based only on the right-triangle definition of cosine and obtains squared side lengths algebraically. Other proofs typically invoke the Pythagorean theorem explicitly, and are more geometric, treating *a* cos *γ* as a label for the length of a certain line segment.^{[12]}

Unlike many proofs, this one handles the cases of obtuse and acute angles *γ* in a unified fashion.

Consider a triangle with sides of length *a*, *b*, *c*, where *θ* is the measurement of the angle opposite the side of length *c*. This triangle can be placed on the Cartesian coordinate system with side *a* aligned along the *x* axis and angle *θ* placed at the origin, by plotting the components of the 3 points of the triangle as shown in Fig. 4:

By the distance formula,^{[13]}

Squaring both sides and simplifying

An advantage of this proof is that it does not require the consideration of separate cases depending on whether the angle γ is acute, right, or obtuse. However, the cases treated separately in *Elements* II.12–13 and later by al-Ṭūsī, al-Kāshī, and others could themselves be combined by using concepts of signed lengths and areas and a concept of signed cosine, without needing a full Cartesian coordinate system.

Referring to the diagram, triangle *ABC* with sides *AB* = *c*, *BC* = *a* and *AC* = *b* is drawn inside its circumcircle as shown. Triangle *ABD* is constructed congruent to triangle *ABC* with *AD* = *BC* and *BD* = *AC*. Perpendiculars from *D* and *C* meet base *AB* at *E* and *F* respectively. Then:

Now the law of cosines is rendered by a straightforward application of Ptolemy's theorem to cyclic quadrilateral *ABCD*:

Plainly if angle *B* is right, then *ABCD* is a rectangle and application of Ptolemy's theorem yields the Pythagorean theorem:

One can also prove the law of cosines by calculating areas. The change of sign as the angle *γ* becomes obtuse makes a case distinction necessary.

Recall that

*a*^{2},*b*^{2}, and*c*^{2}are the areas of the squares with sides*a*,*b*, and*c*, respectively;- if
*γ*is acute, then*ab*cos*γ*is the area of the parallelogram with sides*a*and*b*forming an angle of*γ′*= π/2 −*γ*; - if
*γ*is obtuse, and so cos*γ*is negative, then −*ab*cos*γ*is the area of the parallelogram with sides*a*and*b*forming an angle of*γ′*=*γ*− π/2.

**Acute case.** Figure 7a shows a heptagon cut into smaller pieces (in two different ways) to yield a proof of the law of cosines. The various pieces are

- in pink, the areas
*a*^{2},*b*^{2}on the left and the areas 2*ab*cos*γ*and*c*^{2}on the right; - in blue, the triangle
*ABC*, on the left and on the right; - in grey, auxiliary triangles, all congruent to
*ABC*, an equal number (namely 2) both on the left and on the right.

The equality of areas on the left and on the right gives

**Obtuse case.** Figure 7b cuts a hexagon in two different ways into smaller pieces, yielding a proof of the law of cosines in the case that the angle *γ* is obtuse. We have

- in pink, the areas
*a*^{2},*b*^{2}, and −2*ab*cos*γ*on the left and*c*^{2}on the right; - in blue, the triangle
*ABC*twice, on the left, as well as on the right.

The equality of areas on the left and on the right gives

The rigorous proof will have to include proofs that various shapes are congruent and therefore have equal area. This will use the theory of congruent triangles.

Using the geometry of the circle, it is possible to give a more geometric proof than using the Pythagorean theorem alone. Algebraic manipulations (in particular the binomial theorem) are avoided.

**Case of acute angle γ, where a > 2b cos γ.** Drop the perpendicular from

Then use the *tangent secant theorem* (Euclid's *Elements*: Book 3, Proposition 36), which says that the square on the tangent through a point *B* outside the circle is equal to the product of the two lines segments (from *B*) created by any secant of the circle through *B*. In the present case: *BH*^{2} = *BC*·*BP*, or

Substituting into the previous equation gives the law of cosines:

Note that *h*^{2} is the power of the point *B* with respect to the circle. The use of the Pythagorean theorem and the tangent secant theorem can be replaced by a single application of the power of a point theorem.

**Case of acute angle γ, where a < 2b cos γ.** Drop the perpendicular from

Now use the *chord theorem* (Euclid's *Elements*: Book 3, Proposition 35), which says that if two chords intersect, the product of the two line segments obtained on one chord is equal to the product of the two line segments obtained on the other chord. In the present case: *BH*^{2} = *BC*·*BP*, or

Substituting into the previous equation gives the law of cosines:

Note that the power of the point *B* with respect to the circle has the negative value −*h*^{2}.

**Case of obtuse angle γ.** This proof uses the power of a point theorem directly, without the auxiliary triangles obtained by constructing a tangent or a chord. Construct a circle with center

which is the law of cosines.

Using algebraic measures for line segments (allowing negative numbers as lengths of segments) the case of obtuse angle (*CK* > 0) and acute angle (*CK* < 0) can be treated simultaneously.

The law of cosines can be proven algebraically from the law of sines and a few standard trigonometric identities.^{[14]} To start, three angles of a triangle sum to a straight angle ( radians). Thus by the angle sum identities for sine and cosine,

Squaring the first of these identities, then substituting from the second, and finally replacing the Pythagorean trigonometric identity, we have:

The law of sines holds that

so to prove the law of cosines, we multiply both sides of our previous identity by

This concludes the proof.

Denote

Therefore,

Taking the dot product of each side with itself:

Using the identity

leads to

The result follows.

When *a* = *b*, i.e., when the triangle is isosceles with the two sides incident to the angle *γ* equal, the law of cosines simplifies significantly. Namely, because *a*^{2} + *b*^{2} = 2*a*^{2} = 2*ab*, the law of cosines becomes

or

Given an arbitrary tetrahedron whose four faces have areas A, B, C, and D, with dihedral angle between faces A and B, etc., a higher-dimensional analogue of the law of cosines is:^{[15]}

When the angle, *γ*, is small and the adjacent sides, *a* and *b*, are of similar length, the right hand side of the standard form of the law of cosines is subject to catastrophic cancellation in numerical approximations. In situations where this is an important concern, a mathematically equivalent version of the law of cosines, similar to the haversine formula, can prove useful:

In the limit of an infinitesimal angle, the law of cosines degenerates into the circular arc length formula, *c* = *a* *γ*.

Main articles: Spherical law of cosines and Hyperbolic law of cosines |

Versions similar to the law of cosines for the Euclidean plane also hold on a unit sphere and in a hyperbolic plane. In spherical geometry, a triangle is defined by three points **u**, **v**, and **w** on the unit sphere, and the arcs of great circles connecting those points. If these great circles make angles *A*, *B*, and *C* with opposite sides *a*, *b*, *c* then the spherical law of cosines asserts that both of the following relationships hold:

In hyperbolic geometry, a pair of equations are collectively known as the hyperbolic law of cosines. The first is

where sinh and cosh are the hyperbolic sine and cosine, and the second is

As in Euclidean geometry, one can use the law of cosines to determine the angles *A*, *B*, *C* from the knowledge of the sides *a*, *b*, *c*. In contrast to Euclidean geometry, the reverse is also possible in both non-Euclidean models: the angles *A*, *B*, *C* determine the sides *a*, *b*, *c*.

The Law of Cosines can be generalized to all polyhedra by considering any polyhedron with vector sides and invoking the Divergence Theorem
.^{[16]}