Pythagoras with a correction term
The law of cosines says c² = a² + b² − 2ab·cos C. Read it as the Pythagorean theorem plus a correction. If C is 90°, cos C is zero, the correction vanishes and you are left with c² = a² + b². Open the angle past 90° and cos C goes negative, so the term −2ab·cos C becomes positive and the opposite side grows. Close it below 90° and the term subtracts, shrinking the opposite side. The formula measures exactly how far a triangle departs from being right-angled.
That makes it the tool for the two cases the law of sines cannot begin from. SAS: two sides and the angle between them, which fixes the triangle completely, so the third side follows with no ambiguity. SSS: three sides and no angles, which also fixes the triangle, and rearranging gives every angle.
Both cases turn up constantly outside the classroom. A surveyor who has taped two distances from a station and measured the angle between them wants the closing distance — SAS. Anyone who has taped three sides of a plot and needs the corner angles is doing SSS. In machining, the distance between two features on a bolt circle is SAS with equal sides. In navigation, a course change of known angle after two known legs is SAS again.
Both directions, and the numerical detail that matters
Finding a side (SAS). Square both known sides, add them, subtract 2ab·cos C, then take the square root. Cosine of an obtuse angle is negative, so the subtraction of a negative adds — a sign slip here is the most common error in the whole calculation. At C = 120°, cos C = −0.5 and the formula becomes c² = a² + b² + ab.
Finding an angle (SSS). Rearranged, cos C = (a² + b² − c²)/(2ab), then C = arccos of that. The sign of the numerator alone tells you the triangle's type before you take the inverse cosine: positive means C is acute, zero means right, negative means obtuse. That is the algebraic form of the converse of the Pythagorean theorem.
Arccos returns a unique angle between 0° and 180°, which is exactly the range an interior angle can occupy. That is why the SSS case has no ambiguity, unlike the SSA case handled by the law of sines, where arcsin cannot distinguish an angle from its supplement. Solving for the largest angle first with the cosine rule and only then switching to the sine rule is a standard way to sidestep that trap entirely.
Two implementation points. The argument of arccos can drift a hair outside ±1 through floating-point rounding on a degenerate triangle, so this calculator clamps it before inverting rather than returning NaN. And the triangle inequality is checked up front: if the longest side exceeds the sum of the other two, no triangle exists and the tool says so instead of reporting an angle.
Worked example: closing a survey traverse
From a station you tape 5.000 m to point A and 7.000 m to point B, and the instrument reads 60.000° between the two lines. How far apart are A and B, and what are the other angles?
- Set up. a = 5, b = 7, C = 60°, and c is the unknown distance A to B.
- Cosine. cos 60° = 0.500000 exactly.
- Correction term. 2ab·cos C = 2 × 5 × 7 × 0.5 = 35.000.
- Square of the third side. c² = 25 + 49 − 35 = 39.000.
- Third side. c = √39 = 6.244998 m. Note it is shorter than √(25+49) = 8.602 m, as it must be for an acute included angle.
- Angle A. cos A = (b² + c² − a²)/(2bc) = (49 + 39 − 25)/(2 × 7 × 6.244998) = 63/87.42997 = 0.720577, so A = 43.8985°.
- Angle B. 180 − 60 − 43.8985 = 76.1015°.
- Area. ½ab·sin C = ½ × 35 × 0.866025 = 15.1554 m².
Check with the law of sines: 5/sin 43.8985° = 5/0.693375 = 7.2110, 7/sin 76.1015° = 7/0.970725 = 7.2110, and 6.244998/sin 60° = 6.244998/0.866025 = 7.2110. All three ratios agree, which confirms the solution and incidentally gives the circumdiameter, 2R = 7.2110 m.
Now reverse it. Given only the three sides 5, 7 and 6.244998, cos C = (25 + 49 − 39)/(2 × 5 × 7) = 35/70 = 0.500000, so C = 60° — back where we started.
What the numbers tell you, and where precision goes
Read the sign of a² + b² − c² first. Positive means the angle opposite c is acute, negative means obtuse, zero means the triangle is right-angled. On measured data an exact zero never happens, so judge it against your measurement uncertainty rather than against the digits.
Precision behaves differently in the two directions. Finding a side from SAS is well conditioned: a small angle error produces a proportionally small change in c, and the sensitivity is smallest when C is near 0° or 180°. Finding an angle from SSS is worse conditioned near 0° and 180°, because the derivative of arccos blows up as its argument approaches ±1. On a nearly degenerate triangle the effect is dramatic: sides 5.00, 7.00 and 11.99 m give a largest angle of 175.26°, and adding just one centimetre to the long side to make it 12.00 m flattens the triangle to 180° — 4.7° of swing for 0.08% of length. The calculator warns when any angle exceeds 179°.
For that reason, solve the largest angle with the cosine rule when you can. It is the one most likely to be obtuse, and arccos handles obtuse angles unambiguously while arcsin does not. Once the largest angle is known, the remaining ones are safely acute and the sine rule finishes the triangle faster.
Finally, sanity-check the third side against its bounds. For any included angle, c must lie strictly between |a − b| and a + b. In the worked example that is between 2 and 12; a result outside those limits means the wrong angle was used, almost always one of the non-included ones.
Third side against included angle for a = 5, b = 7
| Included angle C | cos C | Third side c | Area | Type |
|---|---|---|---|---|
| 15° | 0.965926 | 2.5269 | 4.5293 | Acute at C |
| 30° | 0.866025 | 3.6576 | 8.7500 | Acute at C |
| 45° | 0.707107 | 4.9500 | 12.3744 | Acute at C |
| 60° | 0.500000 | 6.2450 | 15.1554 | Acute at C |
| 90° | 0.000000 | 8.6023 | 17.5000 | Right at C |
| 120° | −0.500000 | 10.4403 | 15.1554 | Obtuse at C |
| 150° | −0.866025 | 11.6027 | 8.7500 | Obtuse at C |
Two things to read here. The third side grows monotonically with the included angle, but the area does not — it peaks at C = 90° and is the same at 30° and 150°, because sin C is symmetric about 90°.
Where the cosine rule goes wrong in practice
- Using an angle that is not the included one. The formula needs the angle between a and b. Any other angle gives a result that looks reasonable and is simply wrong; the bounds check |a−b| < c < a+b will usually catch it.
- Dropping the sign on an obtuse cosine. cos 120° = −0.5, so −2ab·cos C becomes +ab. Treating it as a subtraction understates c badly.
- Forgetting the square root. The formula gives c². Reporting 39 instead of 6.245 is a surprisingly common slip.
- Solving a small angle first from SSS. Do the largest angle first: it is the only one that can be obtuse, and finishing with the sine rule then carries no ambiguity.
- Trusting angles on a near-degenerate triangle. When the longest side approaches the sum of the other two, arccos becomes extremely sensitive and small taping errors produce large angle errors.
- Mixing units between sides. All three lengths must share a unit. Angles are always in degrees here.
How this fits with the other triangle tools
Between them, the two rules cover every determinate triangle. Use the cosine rule for SAS and SSS; use the law of sines calculator for ASA, AAS and the ambiguous SSA case. AAA is not solvable at all — three angles fix the shape but not the size, giving a family of similar triangles.
When the triangle has a right angle, this law degenerates into simpler tools: the Pythagorean theorem calculator for the sides and the right triangle calculator for the angles, both of which avoid the arccos step entirely. If all you want is the area from three sides, Heron's formula in the triangle area calculator gets there in one expression without solving for any angle.
Historically, the result appears in Euclid's Elements as Propositions II.12 and II.13, stated geometrically as the excess or deficit of the square on a side in obtuse and acute triangles. The modern trigonometric form had to wait for the cosine function itself. It also generalises: the same relationship in vector notation, |a − b|² = |a|² + |b|² − 2a·b, is where the dot product's geometric meaning comes from, which is why the law of cosines underlies angle calculation in coordinate geometry and computer graphics. For angles between points given by coordinates, start with the distance formula calculator and feed the three lengths in here.
