Law of Cosines Calculator
Solve triangles using the law of cosines. Find a missing side from two sides and an included angle, or find angles from three sides. Shows working.
Solve triangles using the law of cosines: c² = a² + b² - 2ab cos(C). Find a missing side from two sides and the included angle (SAS), or find all three angles from three known sides (SSS). Step-by-step working with area and perimeter included.
About Law of Cosines Calculator
The Law of Cosines Formula
The law of cosines relates the sides and angles of any triangle:
c² = a² + b² - 2ab cos(C)
There are three forms, one for each side:
| To Find | Formula |
|---|---|
| Side c | c² = a² + b² - 2ab cos(C) |
| Side b | b² = a² + c² - 2ac cos(B) |
| Side a | a² = b² + c² - 2bc cos(A) |
To find an angle when all three sides are known, rearrange:
cos(C) = (a² + b² - c²) / (2ab)
Worked Example: SAS (Find Missing Side)
Given: a = 7, b = 10, C = 55°
- c² = 7² + 10² - 2(7)(10)cos(55°)
- c² = 49 + 100 - 140 × 0.5736
- c² = 149 - 80.30 = 68.70
- c = √68.70 = 8.289
Now find the remaining angles using the rearranged formula:
- cos(A) = (b² + c² - a²) / (2bc) = (100 + 68.70 - 49) / (2 × 10 × 8.289) = 119.70/165.78 = 0.7222
- A = arccos(0.7222) = 43.73°
- B = 180° - 55° - 43.73° = 81.27°
Area: (1/2) × 7 × 10 × sin(55°) = 35 × 0.8192 = 28.67 square units
Worked Example: SSS (Find All Angles)
Given: a = 5, b = 7, c = 9
- First check triangle inequality: 5+7=12 > 9, 5+9=14 > 7, 7+9=16 > 5 ✓
- Find largest angle first (opposite longest side c):
- cos(C) = (5² + 7² - 9²) / (2 × 5 × 7) = (25 + 49 - 81) / 70 = -7/70 = -0.1
- C = arccos(-0.1) = 95.74° (obtuse - as expected for the longest side)
- cos(A) = (7² + 9² - 5²) / (2 × 7 × 9) = (49 + 81 - 25) / 126 = 105/126 = 0.8333
- A = arccos(0.8333) = 33.56°
- B = 180° - 95.74° - 33.56° = 50.70°
Verify: 33.56° + 50.70° + 95.74° = 180.00° ✓
Connection to the Pythagorean Theorem
When angle C = 90°, cos(90°) = 0, so the law of cosines becomes:
c² = a² + b² - 2ab(0) = a² + b²
This is exactly the Pythagorean theorem. The law of cosines is the general version that works for all triangles, not just right triangles.
| Angle C | cos(C) | Effect on c² | Triangle Type |
|---|---|---|---|
| Less than 90° | Positive | c² < a² + b² | Acute |
| Exactly 90° | 0 | c² = a² + b² | Right |
| Greater than 90° | Negative | c² > a² + b² | Obtuse |
When to Use Law of Cosines vs Law of Sines
| Given Information | Best Method | Why |
|---|---|---|
| SAS (two sides + included angle) | Law of Cosines | Directly finds the third side |
| SSS (three sides) | Law of Cosines | Directly finds any angle |
| AAS (two angles + any side) | Law of Sines | Simpler calculation |
| ASA (two angles + included side) | Law of Sines | Find third angle first, then use ratios |
| SSA (two sides + non-included angle) | Law of Sines | But watch for ambiguous case |
Triangle Area from Three Sides (Heron's Formula)
When you know all three sides, you can find the area without computing any angles:
s = (a + b + c) / 2 (semi-perimeter)
Area = √(s(s-a)(s-b)(s-c))
Example: a = 5, b = 7, c = 9
- s = (5 + 7 + 9) / 2 = 10.5
- Area = √(10.5 × 5.5 × 3.5 × 1.5) = √303.1875 = 17.41 square units
The Triangle Inequality
Before solving, the calculator checks that a valid triangle can exist. The triangle inequality states that the sum of any two sides must be greater than the third side:
- a + b > c
- a + c > b
- b + c > a
If any of these fail, no triangle exists and the calculator shows an error.
For SSA problems with possible ambiguous solutions, the law of sines calculator checks for multiple triangles. For right triangle problems specifically, the Pythagorean theorem calculator provides a simpler interface.
A Brief History of the Law of Cosines
The law of cosines predates trigonometry as a distinct subject. Euclid's Elements (c. 300 BCE), Book II Propositions 12 and 13, proves the result geometrically for obtuse and acute triangles without ever naming an angle - it talks about rectangles built on sides. The modern algebraic form with the cosine function was first published by the Persian astronomer Jamshīd al-Kāshī in his 1427 treatise Miftāḥ al-Ḥisāb (Key of Arithmetic), which is why French textbooks still call it théorème d'al-Kashi. The result reached European trigonometry through François Viète's 1579 Canon mathematicus, and the cosine-based form became standard after Leonhard Euler's 1748 Introductio in analysin infinitorum popularised modern trigonometric notation.
How Does the Law of Cosines Relate to the Dot Product?
The law of cosines is really the dot product in disguise. If sides a and b of a triangle are the vectors A and B meeting at angle C, then the third side is the vector C = B - A. Taking the squared magnitude:
|C|² = (B - A)·(B - A) = |A|² + |B|² - 2(A·B)
Since A·B = |A||B|cos(C), this is exactly the law of cosines. That link is why physics and engineering fields that work in vector spaces - orbital mechanics, crystallography, computer graphics - use the law of cosines constantly even when triangles are never drawn. In computer graphics specifically, the rearranged form cos(θ) = (A·B) / (|A||B|) is the standard way to compute the angle between two surface normals for lighting calculations.
Real-World Applications
The law of cosines is the backbone of any field that needs distances between points when only angles and partial distances are known.
| Field | Typical Use | What's Measured |
|---|---|---|
| Land surveying | Compute distance across an obstacle (river, building) | Two known distances from a baseline plus the included angle |
| GPS / trilateration | Calculate angles in satellite-receiver triangles | Signal travel times convert to distances; angles follow from SSS |
| Astronomy | Angular separation between stars on the celestial sphere | Spherical law of cosines extends the planar version |
| Navigation | Great-circle distance between two points on Earth | Uses the spherical form with latitude and longitude as angles |
| Robotics | Inverse kinematics for two-jointed arms | Joint angles from desired end-effector position |
| Structural engineering | Force resolution on truss members meeting at non-right angles | Net force magnitude from two force vectors and included angle |
The spherical version, cos(c) = cos(a)cos(b) + sin(a)sin(b)cos(C), is still used for great-circle distances although the numerically stable haversine formula has largely replaced it for short distances where floating-point precision matters.
Numerical Accuracy and Common Pitfalls
Three subtle issues trip up students and sometimes production code when implementing the law of cosines:
- Floating-point domain errors on arccos. When sides are close to a degenerate triangle (e.g. a + b barely greater than c), the computed
cos(C)can land just outside [-1, 1] due to rounding. CallingMath.acos(1.0000001)returns NaN. This tool clamps the input to [-1, 1] before the arccos call, which is the standard defensive pattern. - Loss of significance for small angles. When C is very small,
cos(C)is close to 1, soa² + b² - 2ab cos(C)subtracts two nearly equal numbers. The result loses significant digits - the classic catastrophic cancellation. For angles under about 1 degree, an alternative form based on the haversine identity gives more stable results. - Ambiguous angle naming. The formula
c² = a² + b² - 2ab cos(C)only works if C is the angle OPPOSITE side c and BETWEEN sides a and b. Students who label sides and angles inconsistently get wrong answers. The convention used here (lowercase letter = side, uppercase letter = opposite angle) comes from Euler and is universal in modern textbooks.
What Is the Largest Angle of a Triangle?
The largest angle is always opposite the longest side, and the smallest angle is opposite the shortest side. This is a direct corollary of the law of cosines: if a ≥ b ≥ c, then the formula cos(A) = (b² + c² - a²) / (2bc) produces the smallest (or most negative) cosine, which corresponds to the largest arccos value. Finding the largest angle first is best practice when solving SSS triangles, because if it turns out to be obtuse, the remaining two angles are guaranteed acute and the law of sines can finish the job without ambiguity.
Common Mistakes Students Make
- Using the wrong formula pairing. Applying the law of sines to SAS or SSS cases - it works for SAS finding a side only if you already know a ratio, which you don't.
- Forgetting to convert degrees to radians. Most programming languages (including JavaScript's Math.cos) expect radians. This tool handles the conversion internally. On scientific calculators, check whether DEG or RAD mode is selected before starting.
- Dropping the minus sign in the rearranged form. The correct rearrangement is
cos(C) = (a² + b² - c²) / (2ab), not(a² + b² + c²). The minus is what lets an obtuse angle give a negative cosine. - Using the law of cosines when the Pythagorean theorem is enough. If one angle is already 90°, just use c² = a² + b². The triangle calculator auto-detects the easiest method based on what you know.
For students studying other triangle relationships, the trigonometric functions calculator computes sin, cos, and tan for any angle and can verify intermediate steps.
All calculations run in your browser. No data is sent to any server.
Sources
Frequently Asked Questions
What is the law of cosines?
The law of cosines states c squared = a squared + b squared minus 2ab times cos(C), where C is the angle between sides a and b. It generalises the Pythagorean theorem to non-right triangles.
When should I use the law of cosines instead of the law of sines?
Use the law of cosines when you have SSS (three sides) or SAS (two sides and the included angle). The law of sines is better for AAS, ASA, or SSA cases.
How is this related to the Pythagorean theorem?
When angle C is exactly 90 degrees, cos(90) equals 0, so the formula simplifies to c squared = a squared + b squared, which is the Pythagorean theorem. The calculator flags this special case.
Can I find all three angles from three sides?
Yes. Use the rearranged formula cos(C) = (a squared + b squared - c squared) / (2ab) to find each angle. Enter all three sides in the Find Missing Angle mode.
What if my triangle does not satisfy the triangle inequality?
If any one side is longer than the sum of the other two, no valid triangle exists. The calculator validates this and will not return a result for invalid side combinations.
Related Tools
Link to this tool
Copy this HTML to link to this tool from your website or blog.
<a href="https://toolboxkit.io/tools/law-of-cosines-calculator/" title="Law of Cosines Calculator - Free Online Tool">Try Law of Cosines Calculator on ToolboxKit.io</a>