Law of Cosines Calculator
Results
Formulas Used in Law of Cosines Calculator
In-Depth Tutorial: Law of Cosines Calculator
The Law of Cosines is the second of the two universal triangle-solving tools — partnered with the Law of Sines. Where the Law of Sines works for AAS, ASA, and SSA cases (you have a matched side-angle pair), the Law of Cosines works for the cases where you don't: SSS (three sides) and SAS (two sides + included angle). It also reduces to the Pythagorean theorem when the included angle is 90° — making it the Pythagorean theorem's natural generalization to all triangles. This tutorial walks through the statement, the proof, when to use it vs the Law of Sines, and worked examples for the SSS and SAS cases.
The Law of Cosines stated
For any triangle with sides a, b, c and the angle C opposite side c:
c² = a² + b² − 2ab · cos(C)
By symmetry, the same relation holds for the other two angles by relabeling:
- a² = b² + c² − 2bc · cos(A)
- b² = a² + c² − 2ac · cos(B)
- c² = a² + b² − 2ab · cos(C)
For finding an angle from three sides, rearrange to solve for cos(C):
cos(C) = (a² + b² − c²) / (2ab)
The Pythagorean theorem connection
When C = 90°, cos(C) = 0, and the formula reduces to:
c² = a² + b² − 2ab · 0 = a² + b²
That's the Pythagorean theorem. So the Law of Cosines is a strict generalization — it works for ANY triangle, with the −2ab·cos(C) term being a "correction" that vanishes when the triangle is right.
The sign of the correction also tells you about the triangle:
- cos(C) > 0 (C is acute, < 90°): the correction term is positive, so c² < a² + b² (c is shorter than the Pythagorean would predict). The triangle is acute at C.
- cos(C) = 0 (C is exactly 90°): correction vanishes. Right triangle at C.
- cos(C) < 0 (C is obtuse, > 90°): correction is negative, so c² > a² + b² (c is longer than Pythagorean would predict). Obtuse at C.
This is the converse Pythagorean test in disguise.
Proof from coordinates
Place a triangle on the coordinate plane: put vertex A at the origin, side AB along the positive x-axis with length c, and let vertex B = (c, 0). Place C somewhere above the x-axis.
From angle A and side b (length from A to C), the coordinates of C are:
C = (b · cos(A), b · sin(A))
The third side a goes from B = (c, 0) to C = (b·cos(A), b·sin(A)). Apply the distance formula:
a² = (b·cos(A) − c)² + (b·sin(A))²
= b²cos²(A) − 2bc·cos(A) + c² + b²sin²(A)
= b²(cos²(A) + sin²(A)) − 2bc·cos(A) + c²
= b² + c² − 2bc·cos(A)
The middle line used the Pythagorean identity cos² + sin² = 1. The result is the Law of Cosines.
When to use Law of Cosines vs Law of Sines
| You know | Use |
|---|---|
| SSS (3 sides) | Law of Cosines (to find any angle) |
| SAS (2 sides + included angle) | Law of Cosines (to find the third side) |
| ASA (2 angles + included side) | Law of Sines (after computing third angle) |
| AAS (2 angles + non-included side) | Law of Sines (after computing third angle) |
| SSA (2 sides + non-included angle) | Law of Sines — beware ambiguous case |
Mnemonic: Use Law of Cosines when no matched side-angle pair is yet known. Then if needed, switch to Law of Sines once you have one.
Worked example — SSS
Triangle with sides a = 5, b = 7, c = 9. Find all three angles.
Start with C (the angle opposite the longest side, often safest):
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 from c² > a² + b²).
Next find A:
cos(A) = (7² + 9² − 5²) / (2 · 7 · 9) = (49 + 81 − 25) / 126 = 105/126 ≈ 0.8333
A = arccos(0.8333) ≈ 33.56°.
Third angle: B = 180° − 95.74° − 33.56° = 50.70°. (Verified by Law of Cosines on B, but the sum-to-180° check is faster.)
Worked example — SAS
Triangle with a = 8, b = 10, and included angle C = 60°. Find side c.
c² = 8² + 10² − 2(8)(10)cos(60°) = 64 + 100 − 160(0.5) = 164 − 80 = 84
c = √84 ≈ 9.17.
Then to find the other angles, switch to Law of Sines:
sin(A) / 8 = sin(60°) / 9.17
sin(A) = 8 · sin(60°) / 9.17 ≈ 8 · 0.866 / 9.17 ≈ 0.755
A = arcsin(0.755) ≈ 49.11°.
B = 180° − 60° − 49.11° = 70.89°.
Why Law of Cosines doesn't have an "ambiguous case"
For SSS, the three sides uniquely determine the triangle (up to congruence). The Law of Cosines computes cos(C) directly, and arccos returns a unique angle in (0°, 180°). No ambiguity.
For SAS, the angle is given, so the third side is uniquely determined. Again no ambiguity.
Contrast SSA (handled by Law of Sines): arcsin returns either of two supplementary angles, and you need to manually choose which is valid. The Law of Cosines avoids this by working with arccos, which is single-valued in the relevant range.
The vector-form generalization
The Law of Cosines is also the geometric statement of the dot product. For two vectors u and v with angle θ between them:
u · v = |u| · |v| · cos(θ)
Expand and rearrange: if u and v are two sides of a triangle meeting at angle θ, the third side w = v − u satisfies |w|² = |v|² + |u|² − 2|u||v|cos(θ) — exactly the Law of Cosines.
This is why the Law of Cosines extends naturally to higher-dimensional geometry: it is the dot-product formula in disguise.
Common mistakes
- Sign error on the −2ab·cos(C) term. Some students write +2ab·cos(C). The formula has a MINUS sign in front of the 2ab·cos(C) term — confirmed by the Pythagorean reduction (when C = 90°, cos(C) = 0 and the term vanishes; if the sign were +, the formula would not reduce correctly).
- Using a sin/cos table only for acute angles. The Law of Cosines applies to any triangle, including obtuse ones. cos of an obtuse angle is negative; arccos of a negative value returns an angle in (90°, 180°). The formula handles this automatically.
- Mixing up which side is c. The formula c² = a² + b² − 2ab·cos(C) requires that C is the angle OPPOSITE c, and a/b are the two sides adjacent to C. Get this matching wrong and the formula gives nonsense.
- Forgetting to take the square root for c. The formula gives c², not c. Apply √ at the end.
Frequently Asked Questions – Law of Cosines Calculator
Use it for SSS (3 known sides → find angles) and SAS (2 sides + included angle → find the third side). It handles cases where the Law of Sines is ambiguous.
c² = a² + b² − 2ab·cos(C). To find an angle: cos(C) = (a² + b² − c²) / (2ab).
When C = 90°, cos(C) = 0 and the formula reduces to c² = a² + b² — the classic Pythagorean theorem.
Yes. "Cosine rule" is the usual name in the UK, Australia and India; "law of cosines" is the US name. Both mean c² = a² + b² − 2ab·cos C.
Rearrange the formula: cos C = (a² + b² − c²) / (2ab), then take the inverse cosine. For sides 5, 7 and 8, the angle opposite 7 is cos⁻¹((25 + 64 − 49) / 80) = cos⁻¹(0.5) = 60°.
With two sides and the included angle (SAS) the third side is fixed. If the angle is not between the two known sides (SSA), there can be two different triangles — use the law of sines calculator, which reports both.
Yes — free and unlimited.
The Law of Cosines is also the triangle inequality and the type test
The formula c² = a² + b² − 2ab·cos(C) isn't only a tool for solving triangles. It encodes two of the most important existence facts about triangles, both of which fall out by inspecting the cosine term.
Triangle type from three sides alone. Once you have a, b, c, the sign of a² + b² − c² tells you what kind of triangle you have without computing any angle. If a² + b² > c², the triangle is acute at C. If a² + b² = c², the triangle is right-angled at C (the Pythagorean theorem case). If a² + b² < c², the triangle is obtuse at C. Apply the same test to the other two vertices to fully classify the triangle in three quick arithmetic checks — much faster than computing all three angles via arccos.
The triangle inequality is a corollary. The cosine of any real angle satisfies −1 ≤ cos(C) ≤ 1. Substituting both extremes into the Law of Cosines:
If cos(C) = 1 (C approaches 0°): c² = a² + b² − 2ab = (a − b)², so c = |a − b|.
If cos(C) = −1 (C approaches 180°): c² = a² + b² + 2ab = (a + b)², so c = a + b.
A "real" triangle has C strictly between 0° and 180°, so |a − b| < c < a + b. That's the triangle inequality, and it's a direct consequence of the cosine of any real angle living in [−1, 1]. The calculator returns an error for inputs violating these bounds because no triangle with those side lengths exists.
Where this law shows up in working engineering
The Law of Cosines is the workhorse of any system that measures distances from known reference points and back-solves for an unknown location.
GPS trilateration. A GPS receiver doesn't measure angles to satellites — it measures the time radio signals take to arrive. Multiplying time by the speed of light gives the receiver's distance to each satellite. From at least four satellites at known positions, the receiver computes its own position by solving a system of equations whose individual two-satellite cases are Law-of-Cosines statements: the receiver, one satellite, and another satellite form a triangle whose three sides are the inter-satellite distance (known from orbital data) and the two satellite-to-receiver distances (measured). Solving gives the angle the receiver subtends, which combined across multiple pairs fixes its 3D position.
Surveyor's resection. If you're at an unknown point and can sight three landmarks whose positions are known (e.g., three church spires on a topographic map), you can compute your own coordinates by measuring the bearings to each landmark and applying the Law of Cosines to the triangles formed. This is how 19th-century surveyors located themselves before GPS and how modern surveyors still verify GPS results in places where satellite reception is poor (urban canyons, dense forest, mining tunnels).
Crystallography. X-ray diffraction patterns from crystals encode the lattice spacings, but the formulas that convert observed diffraction angles to unit-cell parameters reduce to the Law of Cosines applied to vectors in the reciprocal lattice. The 14 Bravais lattice types are classified largely by what the cosine relations between the three lattice vectors look like — equality, perpendicularity, or general parallelepiped.
Practical questions about applying the formula
Why is the Law of Cosines numerically better-behaved than Law of Sines for SSS problems?
Because arccos is single-valued on its natural domain [−1, 1], returning an angle in [0°, 180°] uniquely. Arcsin has the same return range only after you decide whether the angle is acute or obtuse — both produce the same sine. For SSS, arccos directly gives the correct angle in one step; arcsin would require a sign-check afterward. The arithmetic precision is also more forgiving: the operation (a² + b² − c²)/(2ab) stays well-conditioned unless the angle is extremely close to 0° or 180° (a "degenerate" near-straight triangle), in which case both a² + b² and c² are nearly equal and their difference loses precision. For typical inputs this is invisible.
What happens if I enter three sides that can't form a triangle?
The expression (a² + b² − c²)/(2ab) falls outside [−1, 1], so arccos returns NaN (not a number) and the calculator returns an error. Geometrically, this means the longest side is longer than the sum of the other two (or equal to it), which would require the triangle to be degenerate — flattened into a line segment with no enclosed area. Always run the triangle inequality check first as a sanity test: each side must be strictly less than the sum of the other two.
Is there a version of the Law of Cosines for higher-dimensional shapes?
Yes. The polarization identity for inner-product spaces — 2(u · v) = |u + v|² − |u|² − |v|² — is the Law of Cosines in vector form. It works in any number of dimensions and in any inner-product space (function spaces, Hilbert spaces). For an n-dimensional simplex (the higher-dim analog of a triangle), there's a generalized Law of Cosines that relates the squared lengths of edges and the squared volumes of faces. These come up in computational geometry and theoretical physics rather than everyday geometry, but the planar Law of Cosines is their direct ancestor.
The formula has Pythagoras as a special case — what's the deeper relationship?
Both are statements about the dot product, but at different specificity. The Pythagorean theorem says |u + v|² = |u|² + |v|² exactly when u · v = 0 (perpendicular vectors). The Law of Cosines drops the perpendicularity requirement and adds back the −2(u · v) = −2|u||v|cos(θ) term to account for non-orthogonality. So Pythagoras is the Law of Cosines restricted to right triangles, and the Law of Cosines is Pythagoras plus a correction for the angle. Once you internalize the dot product, both formulas are the same statement.