Trigonometry
The Law of Cosines Explained with Examples
By MathSolver Pro Editorial Team · Published
The law of cosines extends the Pythagorean theorem to every triangle. It finds the third side when you know two sides and the angle between them, and it finds any angle when you know all three sides.
The formula
Label the triangle in the usual way: angles , , , with sides , , opposite them. The law of cosines has three versions, one for each side, and they all follow the same pattern:
Read it as: the square of one side equals the sum of the squares of the other two, minus a correction term. The angle in the correction term is the one opposite the side on the left, which is also the angle between the two sides on the right. When , and the correction vanishes, leaving . So the Pythagorean theorem is a special case.
Solving for the cosine gives the form used to find angles:
A short proof with coordinates
Place vertex at the origin and side along the positive x-axis, so vertex is at . Vertex is at distance from the origin in the direction of angle , so its coordinates are .
Side is the distance from to . By the distance formula, . Expanding gives , and since , this is . The proof works whether is acute or obtuse, because the coordinates handle the signs automatically.
The same identity appears in vector form as , which is why the dot product can be used to find the angle between vectors.
Finding a side: two sides and the included angle
When you know two sides and the angle between them (SAS), the law of sines cannot start, because no side is paired with its opposite angle. The law of cosines gives the third side directly.
Example 1: , ,
- Substitute: .
- , so and .
- Find the angle opposite the longest given side, , with the law of cosines again: , giving .
- The last angle: .
Example 2: two hikers
- Two hikers leave a camp along straight paths that are apart. One walks 5 km and the other 8 km. How far apart are they?
- .
- km exactly, because .
Finding angles: three sides
With all three sides known (SSS), use the cosine form for each angle. Start with the largest angle, the one opposite the longest side. If it is obtuse, its cosine is negative, and the inverse cosine handles that correctly because it returns angles from to . Once the largest angle is known, the other two must be acute, so even the law of sines could finish without any ambiguity.
Example 3: , ,
- Largest angle : , so , an obtuse angle.
- Angle : , so .
- Angle : . Computing it directly, , gives the same value.
If the three lengths cannot form a triangle, the formula tells you: the cosine comes out outside . For sides 3, 4 and 8, the angle opposite 8 has , which is impossible, matching the triangle inequality .
Acute, right or obtuse without a calculator
The sign of matches the sign of . With the longest side, that gives a quick classification:
- : the largest angle is acute, so the triangle is acute.
- : the triangle is right angled.
- : the triangle is obtuse.
For sides 5, 7 and 10, , so the triangle is obtuse, as Example 3 confirmed. For 6, 8 and 9, , so it is acute.
Area from three sides: Heron's formula
Combining the law of cosines with the area formula leads to Heron's formula, which needs only the side lengths. Let be the semi-perimeter. Then:
For sides 5, 7 and 10, and the area is . As a check, gives the same 16.25.
Choosing between the laws
Use the law of cosines for SAS and SSS, the two cases with no side and opposite angle pair. Use the law of sines when you have a side with its opposite angle. In mixed problems, the law of cosines often comes first and the law of sines finishes. The solving triangles guide sets out every case in one place.
One practical advantage of the law of cosines is that it never produces an ambiguous answer for an angle, because each cosine between and 1 corresponds to exactly one angle between and . The law of sines, by contrast, cannot tell an acute angle from its obtuse supplement. That is why many textbooks recommend finding the largest angle with cosines whenever an obtuse angle is possible.
Common mistakes
- Computing first and then multiplying by . The multiplication must happen before the subtraction.
- Using an angle that is not between the two given sides.
- Forgetting to take the square root at the end when finding a side.
- Mixing degrees and radians on the calculator.
- Rounding early and then using the rounded value to find the angles.
The law of cosines calculator solves SAS, SSS and other cases with each substitution shown. To review the exact values of cosine at special angles, see the unit circle calculator.
Where the law of cosines is used
Any situation with two known distances and the angle between them is a law of cosines problem. Navigators use it to find how far a ship is from port after two legs of a journey on different headings. Engineers use it to find the length of a brace or cable that joins the ends of two beams meeting at a known angle. In robotics, the angle each joint of a two link arm must take to reach a point is found by solving the law of cosines for an angle, because the lengths of the two links and the distance to the target are all known.
The same formula measures how similar two lists of numbers are in data science. Treating each list as a vector, the cosine of the angle between them, computed from the dot product, is called cosine similarity, and it is used in search engines and recommendation systems. The geometry is identical to the triangle version; only the number of dimensions changes.
Practice problems with answers
- Sides 4 and 6 meet at . Find the third side. Answer: , since .
- Find the largest angle of the triangle with sides 7, 8 and 13. Answer: , so the angle is exactly .
- Is the triangle with sides 9, 12 and 15 acute, right or obtuse? Answer: , so it is a right triangle.
- Find the area of the triangle with sides 13, 14 and 15. Answer: , area .
Frequently asked questions
Is the law of cosines the same as the Pythagorean theorem?
It is a generalisation. For a right angle, , so the extra term disappears and the law becomes . For other angles, the term corrects for how far the angle is from a right angle.
What happens to the formula when the angle is obtuse?
The cosine of an obtuse angle is negative, so becomes positive and the opposite side is longer than the Pythagorean value. That matches intuition: opening the angle wider pushes the ends further apart.
Which angle should I find first in an SSS problem?
Find the angle opposite the longest side first. It is the only one that could be obtuse, and the inverse cosine identifies it correctly. The remaining two angles are then guaranteed to be acute.
Can the law of cosines detect impossible triangles?
Yes. If three lengths break the triangle inequality, the cosine formula gives a value below or above 1, which no angle has. Sides 3, 4 and 8 give , for example.
Do I need degrees or radians for the law of cosines?
Either works, as long as your calculator is set to the same unit as the angle you type in. Most triangle problems give angles in degrees, so check that the calculator is in degree mode before evaluating .