In spherical trigonometry, the **law of cosines** (also called the **cosine rule for sides**^{[1]}) is a theorem relating the sides and angles of spherical triangles, analogous to the ordinary law of cosines from plane trigonometry.

Given a unit sphere, a "spherical triangle" on the surface of the sphere is defined by the great circles connecting three points **u**, **v**, and **w** on the sphere (shown at right). If the lengths of these three sides are *a* (from **u** to **v**), *b* (from **u** to **w**), and *c* (from **v** to **w**), and the angle of the corner opposite *c* is *C*, then the (first) spherical law of cosines states:^{[2]}^{[1]}

Since this is a unit sphere, the lengths *a*, *b*, and *c* are simply equal to the angles (in radians) subtended by those sides from the center of the sphere. (For a non-unit sphere, the lengths are the subtended angles times the radius, and the formula still holds if *a*, *b* and *c* are reinterpreted as the subtended angles). As a special case, for *C* = π/2, then cos *C* = 0, and one obtains the spherical analogue of the Pythagorean theorem:

If the law of cosines is used to solve for *c*, the necessity of inverting the cosine magnifies rounding errors when *c* is small. In this case, the alternative formulation of the law of haversines is preferable.^{[3]}

A variation on the law of cosines, the second spherical law of cosines,^{[4]} (also called the **cosine rule for angles**^{[1]}) states:

where *A* and *B* are the angles of the corners opposite to sides *a* and *b*, respectively. It can be obtained from consideration of a spherical triangle dual to the given one.

Let **u**, **v**, and **w** denote the unit vectors from the center of the sphere to those corners of the triangle. The angles and distances do not change if the coordinate system is rotated, so we can rotate the coordinate system so that is at the north pole and is somewhere on the prime meridian (longitude of 0). With this rotation, the spherical coordinates for are where θ is the angle measured from the north pole not from the equator, and the spherical coordinates for are The Cartesian coordinates for are and the Cartesian coordinates for are The value of is the dot product of the two Cartesian vectors, which is

Let **u**, **v**, and **w** denote the unit vectors from the center of the sphere to those corners of the triangle. We have **u** · **u** = 1, **v** · **w** = cos *c*, **u** · **v** = cos *a*, and **u** · **w** = cos *b*. The vectors **u** × **v** and **u** × **w** have lengths sin *a* and sin *b* respectively and the angle between them is *C*, so

- sin
*a*sin*b*cos*C*= (**u**×**v**) · (**u**×**w**) = (**u**·**u**)(**v**·**w**) − (**u**·**v**)(**u**·**w**) = cos*c*− cos*a*cos*b*,

using cross products, dot products, and the Binet–Cauchy identity (**p** × **q**) · (**r** × **s**) = (**p** · **r**)(**q** · **s**) − (**p** · **s**)(**q** · **r**).

Let **u**, **v**, and **w** denote the unit vectors from the center of the sphere to those corners of the triangle. Consider the following rotational sequence where we first rotate the vector **v** to **u** by an angle followed by another rotation of vector **u** to **w** by an angle after which we rotate the vector **w** back to **v** by an angle The composition of these three rotations will form an identity transform.^{[clarification needed]} That is, the composite rotation maps the point **v** to itself. These three rotational operations can be represented by quaternions:

where and are the unit vectors representing the axes of rotations, as defined by the right-hand rule, respectively. The composition of these three rotations is unity, Right multiplying both sides by conjugates we have where and This gives us the identity

The quaternion product on the right-hand side of this identity is given by

Equating the scalar parts on both sides of the identity, we have

Here Since this identity is valid for any arc angles, suppressing the halves, we have

We can also recover the sine law by first noting that and then equating the vector parts on both sides of the identity as

The vector is orthogonal to both the vectors and and as such Taking dot product with respect to on both sides, and suppressing the halves, we have Now and so we have Dividing each side by we have

Since the right-hand side of the above expression is unchanged by cyclic permutation, we have

The first and second spherical laws of cosines can be rearranged to put the sides (*a*, *b*, *c*) and angles (*A*, *B*, *C*) on opposite sides of the equations:

For *small* spherical triangles, i.e. for small *a*, *b*, and *c*, the spherical law of cosines is approximately the same as the ordinary planar law of cosines,

To prove this, we will use the small-angle approximation obtained from the Maclaurin series for the cosine and sine functions:

Substituting these expressions into the spherical law of cosines nets:

or after simplifying:

The big O terms for *a* and *b* are dominated by *O*(*a*^{4}) + *O*(*b*^{4}) as *a* and *b* get small, so we can write this last expression as:

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).^{[7]}