Law of Cosines Calculator

Enter three sides and get all three angles, or enter two sides plus the angle between them and get the third side. Every step is shown with your own numbers, including how far your triangle sits from being right angled.

Put this calculator on your website for free

Copy one snippet and give your visitors a working Law of Cosines Calculator.

How the law of cosines works

Pythagoras is a wonderful theorem with one strict condition attached: the triangle has to contain a right angle. Tilt that corner even slightly and a2 = b2 + c2 stops being true. For centuries that left a real gap, because most triangles in the world are not right angled. Surveyors, navigators and astronomers all needed the general case.

The law of cosines fills it, and it does so in the most satisfying way possible: by keeping Pythagoras completely intact and bolting a correction term onto the end. That term measures exactly how much the triangle's opening angle has thrown the old formula off. Close the angle below 90 degrees and the far side comes in shorter than Pythagoras predicts. Open it past 90 and the far side stretches longer. At exactly 90 the correction is worth nothing at all and the familiar formula reappears untouched.

The formula

a2 = b2 + c2 − 2bc cos A

Here a, b and c are the three side lengths, and A is the angle sitting opposite side a, which is the same as the angle squeezed between sides b and c. Rearranged to hunt for an angle instead of a side, it becomes:

cos A = (b2 + c2 − a2) ÷ 2bc

That second form is the one that unlocks a triangle where you know all three sides and none of the angles. Run it once for each corner and the whole shape falls out.

Worked example

Two sides and the angle between them. A plot of land has two boundaries meeting at a corner: one runs 5 metres, the other runs 7 metres, and the corner between them opens to 60 degrees. How far apart are the far ends?

a2 = 52 + 72 − 2 × 5 × 7 × cos 60°
a2 = 25 + 49 − 70 × 0.5
a2 = 74 − 35 = 39
a = √39 = 6.24 metres

Now look at what the correction did. If that corner had been a right angle, the answer would have been √74, or 8.60 metres. Pulling the corner in from 90 degrees to 60 degrees pulled the far ends more than two metres closer together, and the entire difference is carried by that one 2bc cos A term.

The triangle's other numbers follow: the remaining angles come to 43.90 and 76.10 degrees, the perimeter is 18.24 metres, and the area is 15.16 square metres.

Pythagoras is hiding inside this formula

Set A to 90 degrees. The cosine of 90 degrees is exactly zero, so the term 2bc cos A becomes 2bc × 0, which is nothing at all. What survives is a2 = b2 + c2.

This is not a coincidence or a neat trick of notation. The law of cosines genuinely contains Pythagoras as one specific case out of infinitely many, which means you have not really been learning a second, competing formula. You have been learning the full version of the one you already knew. Every triangle you ever solved with Pythagoras, this law would have solved too.

The sign of that correction term is worth carrying around in your head, because it is a free sanity check on any answer:

If your answer falls on the wrong side of √(b2 + c2) for the angle you typed, something has gone astray. The calculator reports this comparison on every SAS answer so you can see it rather than take our word for it.

The one thing this law does that the law of sines cannot

Both laws will happily find you a missing angle, so it is fair to ask why anyone keeps two of them. The answer is obtuse angles.

Inverse sine has no way to distinguish 40 degrees from 140 degrees, because both have the same sine. Hand it a value and it always returns the acute option, leaving you to work out whether the obtuse one was the answer you actually wanted. That is the whole origin of the ambiguous case, where one set of measurements can describe two genuinely different triangles.

Inverse cosine has no such blind spot. Cosine is positive for acute angles and negative for obtuse ones, so the sign of your answer carries the information and the angle comes back unambiguously anywhere from 0 to 180 degrees. When you solve a triangle from its three sides, there is exactly one answer and this law finds it.

So the division of labour is clean. If the angle you know sits between the two sides you know, or if you know no angles at all, this is your law. Otherwise reach for the law of sines, which is easier arithmetic when it applies. If you would rather not choose, the triangle calculator takes any valid combination and picks the right law for you.

Frequently asked questions

What is the law of cosines?

For any triangle with sides a, b, c and angle A sitting opposite side a: a2 = b2 + c2 − 2bc cos A. It works on every triangle, not just right ones, which is what makes it the tool you reach for when Pythagoras does not apply.

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

Use the law of cosines when you know all three sides (SSS) or two sides and the angle between them (SAS). Use the law of sines when you know two angles and any side, or two sides and an angle that is not between them. The quick test: if the angle you know sits between the two sides you know, this is your law.

Is Pythagoras just a special case of the law of cosines?

Yes, and it is worth seeing why. Set A to 90 degrees and cos 90 is exactly 0, so the whole 2bc cos A term vanishes and a2 = b2 + c2 is left standing. Pythagoras is the law of cosines with the correction switched off.

Why can I not use the law of sines when I know three sides?

The law of sines relates a side to the angle opposite it, so you need at least one matched side and angle pair to start. With three sides and no angles there is no pair to begin from. The law of cosines is the only one of the two that can open an SSS triangle.

What does it mean if the calculator says no triangle exists?

Three lengths only close into a triangle if the two shorter ones add up to more than the longest. That is the triangle inequality. If they do not, the two short sides cannot reach each other, and no arrangement of them will ever meet.

How do I know which angle is opposite which side?

Angle A sits opposite side a, angle B opposite side b, and angle C opposite side c. Opposite means the angle is not touching that side at all: it looks across the triangle at it. Getting this pairing backwards is the single most common way the arithmetic goes wrong.

Can the law of cosines find an obtuse angle?

Yes, and this is where it beats the law of sines. Inverse cosine returns angles all the way from 0 to 180 degrees, so an obtuse answer comes out directly with no ambiguity. Inverse sine cannot tell 40 degrees from 140 degrees, which is why the law of sines has an ambiguous case and this one does not.

How do I find the area once I have solved the triangle?

With two sides and the angle between them, area = ½ bc sin A. With all three sides, Heron's formula uses the semi-perimeter s = (a + b + c) / 2 and gives area = √(s(s−a)(s−b)(s−c)). This calculator reports both the area and the perimeter with every answer.

Related calculators