The Standard Engine.
MathematicsFinanceHealthPhysicsEngineeringBrowse all

Mathematics · Geometry & Trigonometry · Trigonometry

Law of Cosines Calculator

Calculates the unknown side or angle of any triangle using the Law of Cosines, given two sides and the included angle (SAS) or all three sides (SSS).

Calculator

Advertisement

Formula

In the standard form: c is the side opposite angle C; a and b are the other two sides of the triangle; C is the included angle between sides a and b. To find an unknown angle given all three sides, rearrange to: \cos(C) = \frac{a^2 + b^2 - c^2}{2ab}. The same formula applies cyclically for sides a and b by substituting the appropriate opposite angles A and B.

Source: Euclid's Elements generalized form; standard reference in Larson & Hostetler, 'Precalculus', 8th Edition, Cengage Learning.

How it works

The Law of Cosines states that for any triangle with sides a, b, and c and opposite angles A, B, and C respectively: c² = a² + b² − 2ab·cos(C). This relationship holds for any triangle regardless of whether its angles are acute, right, or obtuse. When angle C equals 90°, the term 2ab·cos(90°) vanishes to zero, and the formula reduces exactly to the familiar Pythagorean theorem c² = a² + b², confirming its status as a true generalisation.

The formula can be used in two classic configurations. In the SAS (Side-Angle-Side) case, you know two sides and the included angle between them, and you solve for the opposite side. In the SSS (Side-Side-Side) case, you know all three sides and rearrange the formula to isolate cos(C), then take the inverse cosine to recover the angle: C = arccos((a² + b² − c²) / (2ab)). The same cyclic substitution applies to find angles A and B. All angle inputs and outputs use degrees in this calculator, converted internally to radians for computation.

Applications of the Law of Cosines span navigation (computing distances between GPS waypoints), structural engineering (analysing force triangles), computer graphics (ray-triangle intersection), and classical astronomy. Any professional working with triangular geometries where a right angle cannot be assumed will rely on this rule regularly.

Worked example

Example 1 — SAS (finding side c): A surveyor marks two boundary lines of length a = 7 m and b = 10 m meeting at an angle of C = 60°. To find the distance c between the far endpoints:
c² = 7² + 10² − 2(7)(10)·cos(60°)
c² = 49 + 100 − 140 × 0.5
c² = 149 − 70 = 79
c = √79 ≈ 8.8882 m

Example 2 — SSS (finding angle C): A triangle has sides a = 5, b = 8, and c = 7. To find the angle C opposite side c:
cos(C) = (5² + 8² − 7²) / (2 × 5 × 8)
cos(C) = (25 + 64 − 49) / 80
cos(C) = 40 / 80 = 0.5
C = arccos(0.5) = 60.0000°

Limitations & notes

The Law of Cosines requires that the triangle inequality be satisfied: the sum of any two sides must exceed the third (a + b > c, etc.). If you enter side lengths that violate this rule — for instance a = 1, b = 2, c = 10 — the argument to arccos will fall outside [−1, 1], returning NaN. Similarly, all sides must be strictly positive; zero or negative inputs are geometrically meaningless. For the SAS case, the angle C must be strictly between 0° and 180° exclusive. The formula does not distinguish between unique and ambiguous triangle cases the way the Law of Sines does for the SSA configuration — however, the Law of Cosines itself always produces a unique solution when properly applied to SAS or SSS data. Floating-point rounding may introduce tiny errors for very large or very small side values; results are reliable to at least 10 significant figures for typical engineering inputs.

Frequently asked questions

What is the difference between the Law of Cosines and the Law of Sines?

The Law of Sines relates sides to opposite angles via the ratio a/sin(A) = b/sin(B) = c/sin(C) and works best for AAS, ASA, and non-ambiguous SSA cases. The Law of Cosines directly relates three sides and one angle, making it the correct choice for SAS and SSS problems. For obtuse or right triangles where the ambiguous SSA case could arise, the Law of Cosines is generally safer and always yields a unique answer.

Does the Law of Cosines work for right triangles?

Yes — and it reduces perfectly to the Pythagorean theorem when the included angle is 90°. Because cos(90°) = 0, the term 2ab·cos(C) disappears, leaving c² = a² + b². You can therefore think of the Pythagorean theorem as a special case of the Law of Cosines, valid only when one angle is exactly 90°.

What happens if I get a NaN or invalid result?

NaN typically means the input values are geometrically impossible. For SSS mode, check that every pair of sides satisfies the triangle inequality (the sum of any two sides must be greater than the third). For SAS mode, verify that angle C is strictly between 0° and 180°, and that both sides are positive real numbers. A result greater than 180° or a negative cosine argument greater than 1 in absolute value signals invalid inputs.

Can I use this calculator to find all three angles of a triangle?

Yes. Enter all three sides in SSS mode to find angle C (opposite side c). Then swap the roles of the sides — use a, c as your 'a, b' and b as 'c' — to find angle B. The third angle A follows from A = 180° − B − C. Alternatively, apply the formula cyclically: cos(A) = (b² + c² − a²)/(2bc) and cos(B) = (a² + c² − b²)/(2ac).

How accurate is the Law of Cosines for very flat or very obtuse triangles?

For nearly degenerate triangles — where one angle approaches 0° or 180°, or sides are very close in length — floating-point cancellation errors can reduce precision. In practice, double-precision arithmetic (as used in JavaScript) provides roughly 15–16 significant digits, so errors only become noticeable in extreme cases. Numerical analysts sometimes recommend the Haversine variant or Kahan summation for geodetic calculations where sub-millimetre precision matters over thousands of kilometres.

Last updated: 2025-01-15 · Formula verified against primary sources.