The law of cosines, c2 = a2 + b2 − 2abcosC, connects the three sides of any triangle with one of its angles. It is the Pythagorean theorem for triangles without a right angle. When C = 90°, cosC = 0, the extra term disappears, and the formula becomes a2 + b2 = c2.
The law is also called the cosine rule. You use it in two situations: when you know two sides and the angle between them and want the third side, and when you know all three sides and want an angle.
The law of cosines formula
Label the triangle the standard way: the angles are A, B and C, and each side gets the lowercase letter of the angle opposite it. So side c is across from angle C, and angle C sits between sides a and b.
The law has one version for each angle. They all follow the same pattern: the side on the left is opposite the angle on the right, and the other two sides appear both squared and in the product.
a2= b2 + c2 − 2bccosAb2= a2 + c2 − 2accosBc2= a2 + b2 − 2abcosCRearranged to find an angle from three sides, the third version becomes
cosC = a2 + b2 − c2 over 2abKey ideaThe law of cosines is a2 + b2 = c2 with a correction, −2abcosC, that accounts for the angle between a and b not being 90°.
How it extends the Pythagorean theorem
Compare the law with the Pythagorean theorem formula. The first part, a2 + b2, is exactly the theorem. Everything the angle does is packed into the last term, and the sign of cosC decides which way it pushes:
- C = 90°: cos90° = 0, so c2 = a2 + b2. The theorem is the special case.
- C acute (less than 90°): cosC is positive, so you subtract something and c2 < a2 + b2. Closing the angle pulls the ends of a and b together.
- C obtuse (more than 90°): cosC is negative, so "minus a negative" adds something and c2 > a2 + b2.
This is why you can classify a triangle by comparing c2 with a2 + b2: the comparison reveals the sign of cosC. The page on acute, right and obtuse triangles turns that into a test (the right triangle checker runs it for you), and the converse of the Pythagorean theorem is the case where the two sides are equal: c2 = a2 + b2 forces cosC = 0, so C = 90°.
The formula also respects the limits you would expect. As C closes toward 0°, cosC → 1 and c2 → a2 + b2 − 2ab = (a − b)2, so the third side shrinks toward |a − b|. As C opens toward 180°, cosC → −1 and c → a + b. In between, c always stays strictly between |a − b| and a + b, which is the triangle inequality.
Proof: drop an altitude
The cleanest proof uses nothing but right triangles. Draw the altitude from B to the line through C and A, meeting it at D. That splits the problem into two right triangles, and the Pythagorean theorem does the rest.
- Work in triangle BDC. Its hypotenuse is a, so by basic trigonometry the altitude is BD = asinC and the piece of the base next to C is CD = acosC.
- Find the rest of the base. The other piece is DA = b − acosC.
- Apply the Pythagorean theorem to triangle BDA. Its legs are BD and DA, and its hypotenuse is c.
The last line uses the Pythagorean identity sin2C + cos2C = 1, which is itself the theorem applied on the unit circle. So the law of cosines is the Pythagorean theorem used twice.
When C is obtuse, D lands outside the triangle, beyond C. The distance CD is then −acosC (a positive number, because cosC is negative), and DA = b + CD = b − acosC. It is the same expression, so the same algebra goes through. When C = 90°, D coincides with C and the proof collapses to the theorem itself.
You can also prove the law with coordinates. Put C at the origin, A at (b, 0) and B at (acosC, asinC); the distance formula for AB gives exactly the expansion above.
The idea is old. Euclid's Elements (about 300 BCE) proves geometric versions in Book II, Proposition 12 for obtuse triangles and Proposition 13 for acute ones, stated with areas of rectangles instead of cosines. The 15th-century Persian mathematician Jamshīd al-Kāshī gave it in a form suited to calculation, which is why it is often called le théorème d'Al-Kashi in France.
Finding a side (SAS)
When you know two sides and the included angle (the angle between them), the law of cosines gives the third side directly. This situation is called SAS, for side-angle-side.
Example 1: two sides and a 37° angle
In triangle ABC, a = 8, b = 11 and C = 37°. Find c.
c2= 82 + 112 − 2(8)(11)cos37°= 64 + 121 − 176(0.79864)≈ 185 − 140.560 = 44.440c≈ 6.67c ≈ 6.67, rounded to the nearest hundredth. Keep the full value of cos37° in your calculator until the end.
Example 2: two roads at 120°
Two straight roads leave a town at an angle of 120°. A cyclist rides 6 km along one and a runner goes 10 km along the other. How far apart are they?
d2= 62 + 102 − 2(6)(10)cos120°= 36 + 100 − 120(−0.5)= 136 + 60 = 196d= 14They are exactly 14 km apart. Because the angle is obtuse, the correction is added: 14 km is more than the √136 ≈ 11.66 km you would get with a right angle. Distance problems like this are common in navigation.
Example 3: the right-angle check
Use the law of cosines with a = 6, b = 8 and C = 90°.
c2 = 36 + 64 − 96cos90° = 100 − 0 = 100, c = 10c = 10, the same answer the Pythagorean theorem calculator gives. For a right angle the two formulas are identical.
Finding an angle (SSS)
When all three sides are known (SSS), rearrange the law to get the cosine of an angle, then use cos−1 (arccos) on your calculator. Arccos returns angles from 0° to 180°, so it handles obtuse angles correctly.
Example 4: a triangle with sides 5, 7 and 8
Find all three angles of the triangle with a = 5, b = 8 and c = 7.
cosC= 52 + 82 − 72 over 2(5)(8) = 40 over 80 = 0.5 ⇒ C = 60°cosA= 82 + 72 − 52 over 2(8)(7) = 88 over 112 ≈ 0.7857 ⇒ A ≈ 38.21°B= 180° − 60° − 38.21° = 81.79°The angles are 60° (exactly), about 38.21° and about 81.79°. Computing B with the law of cosines as well gives cosB = 10 over 70, the same 81.79°, which is a good check.
Example 5: spotting an obtuse angle
A triangle has sides 4, 5 and 8. Find its largest angle.
The largest angle is opposite the longest side, 8.
cosC = 42 + 52 − 82 over 2(4)(5) = 41 − 64 over 40 = −0.575 ⇒ C ≈ 125.10°The largest angle is about 125.10°. The negative cosine already told you it was obtuse, because 42 + 52 = 41 is less than 82 = 64.
TipFind the largest angle first. If it turns out acute, the whole triangle is acute. If the formula ever gives a cosine above 1 or below −1 (try sides 2, 3 and 6), the three lengths cannot form a triangle at all.
Law of cosines or law of sines?
The law of sines, a over sinA = b over sinB = c over sinC, is the other tool for non-right triangles. It is simpler whenever you know an angle together with the side opposite it. Choose by what you are given:
| You know | Start with |
|---|---|
| Two sides and the angle between them (SAS) | Law of cosines, for the third side |
| All three sides (SSS) | Law of cosines, for an angle |
| Two angles and any side (ASA or AAS) | Angle sum (180°), then the law of sines |
| Two sides and an angle not between them (SSA) | Law of sines, checking for zero, one or two possible triangles |
| A right triangle | The Pythagorean theorem or SOH CAH TOA |
One trap deserves a warning. Sine cannot tell an angle from its supplement: sin83.24° and sin96.76° are the same number. In Example 1, the angle opposite the side of 11 is really about 96.76°, but the law of sines with sin−1 would report 83.24°. The safe habit is to use the law of sines only for the smallest unknown angle (which can never be obtuse) and get the last angle from the 180° sum, or simply to use the law of cosines for every angle.
If your triangle does have a right angle, you don't need either law; the guide to choosing between the Pythagorean theorem and trigonometry shows the faster route, and the right triangle calculator solves it for you.
Common mistakes
- Subtracting too early. Work out 2abcosC as one product, then subtract it from a2 + b2. Writing (a2 + b2 − 2ab)cosC is a different, wrong formula.
- Radian mode. cos37 in radians is about 0.7654, not 0.7986. In Example 1 that slip gives c ≈ 7.09 instead of 6.67. Check that your calculator shows DEG.
- The wrong angle. The angle in the formula must be the one between the two sides you are squaring, which is also the angle opposite the side you are finding.
- Losing the sign. For an obtuse angle cosC is negative, so the correction is added. Dropping the minus sign makes the opposite side too short.
- Using it when you don't need to. In a right triangle, a2 + b2 = c2 is quicker and has fewer places to slip. See common mistakes with the Pythagorean theorem for the basics.
Frequently asked questions
Is the law of cosines the same as the Pythagorean theorem?
It is a generalization of it. The law of cosines works in every triangle, and when the angle C is 90° its extra term −2abcosC equals zero, leaving exactly a2 + b2 = c2. Every right triangle problem could be done with the law of cosines, but the theorem is shorter.
When do you use the law of cosines?
Use it when you know two sides and the angle between them (to find the third side) or when you know all three sides (to find an angle). If you know an angle and the side opposite it, the law of sines is usually quicker.
Does the law of cosines work for right triangles?
Yes, for any of the three angles. For the right angle it reduces to the Pythagorean theorem. For an acute angle it still gives correct results: in the 3-4-5 triangle, 32 = 42 + 52 − 2(4)(5)cosA gives cosA = 0.8, so A ≈ 36.87°.
How do you find an angle with the law of cosines?
Rearrange it to cosC = a2 + b2 − c2 over 2ab, substitute the three sides, then press cos−1. For sides 5, 8 and 7 (with c = 7) you get cosC = 0.5 and C = 60°.
Who discovered the law of cosines?
Euclid's Elements, written about 300 BCE, already contains geometric versions (Book II, Propositions 12 and 13) phrased with rectangles rather than cosines. The 15th-century Persian mathematician Jamshīd al-Kāshī stated it in a form suited to calculation, and in France it is often called al-Kashi's theorem.
Why can't the law of cosines give a negative value for c2?
Because cosC is always less than 1 for a real angle between 0° and 180°, so a2 + b2 − 2abcosC is always greater than a2 + b2 − 2ab = (a − b)2, which is never negative. The formula automatically produces a third side between |a − b| and a + b.
Sources and further reading
- Euclid, Elements, Book II, Propositions 12 and 13, in D. E. Joyce's online edition: Proposition II.12.
- Wikipedia, Law of cosines (history, proofs and the al-Kāshī attribution).
Keep exploring
- LearnThe Pythagorean Inequality: Acute, Right or Obtuse?Classify any triangle as acute, right or obtuse from its side lengths with the Pythagorean inequality: compare c² with a² + b². Proof, animation and examples.
- LearnThe Pythagorean IdentityThe Pythagorean identity sin²θ + cos²θ = 1 derived from the unit circle, the related tan/sec and cot/csc identities, and how to find cos from sin by quadrant.
- LearnPythagorean Theorem or Trigonometry: Which Should You Use?When to use the Pythagorean theorem vs trigonometry (SOH CAH TOA): a decision flowchart, side-by-side worked examples and multi-step problems that need both.
- LearnConverse of the Pythagorean TheoremThe converse of the Pythagorean theorem explained: the statement, a clear proof, how to test any three side lengths, and many yes-or-no worked examples.
- CalculatorsRight Triangle Calculator and SolverRight triangle solver: enter any two values, such as two sides or a side and an angle, to get all three sides, both angles, area, perimeter, altitude and radii.
- LearnThe Distance FormulaThe distance formula explained: derive it from the Pythagorean theorem, use it with negative coordinates, avoid sign mistakes and extend it to 3D.