The law of cosines
Name the angles of a triangle A, B and C and the sides opposite them a, b and c. Then
c² = a² + b² − 2ab·cos C
It works for every triangle, not only right-angled ones. With C = 90° the cosine is 0 and the formula becomes Pythagoras, a² + b² = c². When C is wider than 90°, its cosine is negative and c becomes longer than Pythagoras would give. That is the intuition: the further the two sides open up, the longer the side between their ends.
Find the third side (two sides and the angle between them)
With sides a and b and the angle C between them (SAS), take the square root:
=SQRT(B2^2+B3^2-2*B2*B3*COS(RADIANS(B4)))
For the fence corner, 48² + 65² − 2 × 48 × 65 × cos 72° gives c = 67.83 m. The same two sides and angle also give the area of the plot, ½·a·b·sin C = 1,483.6 m², again with RADIANS inside SIN.
Worked example: a fence corner, a roof truss and an impossible triangle
Three small blocks: the third side from two sides and an angle, all three angles from three sides, and a test of whether three lengths can form a triangle at all.
| A | B | Formula in B | |
|---|---|---|---|
| 1 | Fence corner (SAS): two sides, angle between | Value | |
| 2 | Side a (m) | 48.0 | |
| 3 | Side b (m) | 65.0 | |
| 4 | Angle C between a and b (°) | 72.0 | |
| 5 | Side c opposite C (m) | 67.83 | =SQRT(B2^2+B3^2-2*B2*B3*COS(RADIANS(B4))) |
| 6 | Wrong: COS(B4) without RADIANS (m) | 112.09 | =SQRT(B2^2+B3^2-2*B2*B3*COS(B4)) |
| 7 | Area, ½·a·b·sin C (m²) | 1,483.6 | =0.5*B2*B3*SIN(RADIANS(B4)) |
| 8 | |||
| 9 | Roof truss (SSS): three sides | Value | |
| 10 | Side a (m) | 5.20 | |
| 11 | Side b (m) | 6.80 | |
| 12 | Side c, the longest (m) | 9.10 | |
| 13 | Angle C opposite c (°) | 97.74 | =DEGREES(ACOS((B10^2+B11^2-B12^2)/(2*B10*B11))) |
| 14 | Angle A opposite a (°) | 34.49 | =DEGREES(ACOS((B11^2+B12^2-B10^2)/(2*B11*B12))) |
| 15 | Angle B = 180 − A − C (°) | 47.77 | =180-B13-B14 |
| 16 | Check: B from the law of cosines (°) | 47.77 | =DEGREES(ACOS((B10^2+B12^2-B11^2)/(2*B10*B12))) |
| 17 | |||
| 18 | Can these sides form a triangle? | Value | |
| 19 | Side a | 3.0 | |
| 20 | Side b | 4.0 | |
| 21 | Side c | 8.0 | |
| 22 | (a² + b² − c²) ÷ (2ab) | -1.625 | =(B19^2+B20^2-B21^2)/(2*B19*B20) |
| 23 | Valid triangle? | No | =IF(ABS(B22)<=1,"Yes","No") |
Angles are entered and shown in degrees, lengths in metres. Any unit works as long as all three sides use the same one.
Find an angle (three sides)
Rearranged, the law gives any angle from the three sides (SSS):
C = ACOS((a² + b² − c²) ÷ (2ab))
In Excel, ACOS returns radians, so wrap it in DEGREES: =DEGREES(ACOS((B10^2+B11^2-B12^2)/(2*B10*B11))). For a roof truss with sides of 5.20 m, 6.80 m and 9.10 m, the angle opposite the longest side is 97.74°. Swap the letters to get the other angles: A = 34.49°, and B = 180 − A − C = 47.77°. Calculating B with its own law of cosines formula gives the same 47.77°, a good check that the formulas are right.
The RADIANS trap
COS, SIN and TAN in Excel expect radians, and ACOS, ASIN and ATAN return radians. Type COS(72) and Excel takes the cosine of 72 radians, more than 11 full turns, which happens to be negative. That is why the wrong formula in the example returns 112.09 m instead of 67.83 m, without any error message. Convert with RADIANS(angle) or angle*PI()/180 on the way in, and with DEGREES() or *180/PI() on the way out. Google Sheets works the same way.
When the sides cannot form a triangle
If the value inside ACOS is below −1 or above 1, the three lengths cannot form a triangle and Excel returns #NUM!. Sides of 3, 4 and 8 give (9 + 16 − 64) ÷ 24 = −1.625: the two short sides together are shorter than the long one. Test for it before calling ACOS, as the example does with =IF(ABS(B22)<=1,"Yes","No"), or check the triangle inequality directly: each side must be shorter than the other two together. With measured lengths, rounding can push a nearly flat triangle just past 1, so clamping the value with MIN(1,MAX(-1,x)) may be needed.
Law of cosines or law of sines?
Use the law of cosines for two sides and the angle between them (SAS) or for three sides (SSS). Use the law of sines, a ÷ sin A = b ÷ sin B = c ÷ sin C, when you know one side and two angles, for example to measure across a river from a baseline on one bank. Once the law of cosines has given you one angle, the law of sines is often the quicker way to the rest. On a sphere, such as distances on the Earth, flat-triangle formulas no longer hold: see the haversine formula in Excel.
Common mistakes
- Forgetting RADIANS.
COS(72)is the cosine of 72 radians. WriteCOS(RADIANS(72)). - Reading ACOS as degrees. ACOS returns radians, a number between 0 and π. Wrap it in DEGREES.
- Using an angle that is not between the two sides. In c² = a² + b² − 2ab·cos C, C must be the angle between a and b, opposite c.
- Mixing up which side is c. In the angle formula, c is the side opposite the angle you want. Swap the letters for each angle.
- A leading minus before a power. In Excel
=-B2^2is (−B2)², which is positive. Write-(B2^2), or keep the minus between terms, as ina^2+b^2-c^2. - Mixing units. All three sides must be in the same unit. Metres and feet in one triangle give a wrong angle without any error.
Frequently asked questions
How do you use the law of cosines in Excel?
For a side: =SQRT(a^2+b^2-2*a*b*COS(RADIANS(C))) with the angle C in degrees. For an angle: =DEGREES(ACOS((a^2+b^2-c^2)/(2*a*b))), where c is the side opposite the angle.
Why is my COS result wrong in Excel?
Excel's COS, SIN and TAN expect the angle in radians. Convert degrees first with RADIANS(angle) or angle*PI()/180.
When do you use the law of cosines instead of the law of sines?
Use the law of cosines when you know two sides and the angle between them, or all three sides. Use the law of sines when you know a side and two angles, or two sides and an angle that is not between them.
Does the law of cosines work for right triangles?
Yes. With a 90° angle the cosine is 0, the last term disappears and the formula becomes the Pythagorean theorem.