Law of Cosines Calculator

The law of cosines solves the two triangle cases the law of sines cannot start from: two sides with the angle between them (SAS), and three sides with no angles at all (SSS). Enter either and this calculator returns the missing side or every angle, plus the area and perimeter, with each arithmetic step displayed. It checks the triangle inequality first, so three lengths that cannot close are reported rather than turned into an impossible angle. Angles are in degrees; lengths use any unit you like.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
What you knowSAS finds the third side; SSS finds all three angles.Two sides and the included angle (SAS)
Side aOne of the two sides bounding angle C.5
Side bThe other side bounding angle C, in the same unit.7
Side cThe side opposite angle C. Only used in SSS mode.6.244998
Included angle CThe angle between sides a and b, in degrees. It must be the enclosed angle, not either of the others.60 °

It returns

  • Side c (opposite angle C) — In SAS mode this is the side the law of cosines solves for.
  • Angle C
  • Angle A (opposite side a)
  • Angle B (opposite side b)
  • Area
  • Perimeter

The formula

c2=a2+b22abcosC
cosC=a2+b2c22ab

In plain text: c² = a² + b² − 2ab·cos C; cos C = (a² + b² − c²) / (2ab)

  • a, bThe two sides that enclose angle C (units)
  • cThe side opposite angle C (units)
  • CThe angle enclosed between sides a and b (degrees)
  • A, BThe angles opposite sides a and b (degrees)

Also called the cosine rule. When C = 90°, cos C = 0 and the expression collapses to the Pythagorean theorem, so Pythagoras is the right-angle case of this law.

Updated Category Triangle Solvers & Trigonometric Laws Verified against published test cases Reading time 9 min

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?

  1. Set up. a = 5, b = 7, C = 60°, and c is the unknown distance A to B.
  2. Cosine. cos 60° = 0.500000 exactly.
  3. Correction term. 2ab·cos C = 2 × 5 × 7 × 0.5 = 35.000.
  4. Square of the third side. c² = 25 + 49 − 35 = 39.000.
  5. 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.
  6. Angle A. cos A = (b² + c² − a²)/(2bc) = (49 + 39 − 25)/(2 × 7 × 6.244998) = 63/87.42997 = 0.720577, so A = 43.8985°.
  7. Angle B. 180 − 60 − 43.8985 = 76.1015°.
  8. 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

c = √(74 − 70·cos C). The limits are |a − b| = 2 at C → 0° and a + b = 12 at C → 180°.
Included angle Ccos CThird side cAreaType
15°0.9659262.52694.5293Acute at C
30°0.8660253.65768.7500Acute at C
45°0.7071074.950012.3744Acute at C
60°0.5000006.245015.1554Acute at C
90°0.0000008.602317.5000Right at C
120°−0.50000010.440315.1554Obtuse at C
150°−0.86602511.60278.7500Obtuse 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.

Frequently asked questions

When do I use the law of cosines instead of the law of sines?

Use the cosine rule when you have two sides and the angle between them (SAS) or all three sides (SSS). The sine rule cannot start from either, because it always needs a matched side-and-opposite-angle pair. Use the sine rule for ASA, AAS and SSA once you have one such pair.

How do I find an angle from three sides?

Rearrange to cos C = (a² + b² − c²)/(2ab) and take the inverse cosine. For sides 2, 3 and 4 with c = 4: cos C = (4 + 9 − 16)/12 = −0.25, so C = 104.478°. The negative cosine tells you the angle is obtuse before you even invert it.

Does the law of cosines have an ambiguous case?

No. Arccos returns a single value between 0° and 180°, which is the full range an interior angle can take, so SSS and SAS always give exactly one triangle. The ambiguity people remember belongs to the SSA case under the law of sines, where arcsin cannot distinguish an angle from its supplement.

Is the Pythagorean theorem a special case of this?

Yes. Set C = 90°, so cos C = 0, and c² = a² + b² − 0 is exactly Pythagoras. Working the other way, the sign of a² + b² − c² classifies the triangle: positive is acute at C, zero is right, negative is obtuse.

Why does my answer come out larger than a + b?

It cannot, so something is wrong with the inputs. For any valid triangle, |a − b| < c < a + b. The usual cause is using an angle that is not the one enclosed between a and b, or missing the minus sign on an obtuse cosine. Check the included angle first.

How accurate are angles found from measured sides?

Very accurate near 90° and progressively worse as the angle approaches 0° or 180°, because arccos steepens at the ends of its range. On a triangle with sides 5.00, 7.00 and 11.99 m the largest angle is 175.26°; one more millimetre on the long side moves it to 175.50°, and one more centimetre flattens it to 180°. Solve the largest angle first and treat near-degenerate triangles with suspicion.

Can I use it with the angle in radians?

The formula works in any angle measure as long as your cosine function expects the same one. This calculator takes degrees and converts internally. If you are working by hand on a calculator, confirm the mode — cos(60) in radian mode returns −0.9524 rather than 0.5, which is a large and silent error.

What is the area once the triangle is solved?

Use ½ab·sin C with the included angle, which is what this calculator reports. From three sides, Heron's formula gives the same value without solving for any angle. Both are exact; Heron is preferable when you never needed the angles in the first place.

References

  • Euclid's Elements, Book II, Propositions 12 and 13 — Public domain
  • CRC Standard Mathematical Tables and Formulae, 33rd ed. — CRC Press
  • Elementary Surveying: An Introduction to Geomatics, 15th ed. — Ghilani, Pearson