Home Calculators Categories Category: Math, Statistics & Education

Law of Cosines Calculator

This law of cosines calculator solves a triangle from two sides and the angle between them (SAS) or from all three sides (SSS). Choose a task, enter your measurements, and get the missing side or angle, a labeled triangle diagram, and worked steps that show the cosine rule with your numbers substituted in.

Input
Load a worked example, then edit any measurement.
SAS requires the angle between the known sides. SSS requires all three sides.
Degrees and radians describe the same angle. Changing this selector converts the current angle.

Use the same unit for all sides. C is between sides a and b; side c is opposite C.

Side a is opposite angle A. Enter all lengths in one consistent unit.
units
Side b is opposite angle B; it meets side a at angle C.
units
Angle C sits between a and b. It must be greater than zero and less than a straight angle.
°
Visual
Triangle ABC: sides a, b, c are opposite angles A, B, C
Output
Side c
5 units

Opposite the included angle C

Worked steps

Results use 7 significant digits. Steps show rounded intermediate values; calculations retain full precision. Advanced shows the remaining angles, area and perimeter.

What the law of cosines calculator solves

The law of cosines, also called the cosine rule, relates the three sides of a triangle to one of its angles. It works for every Euclidean planar triangle (acute, right, or obtuse), which makes it the standard tool when the sine rule or the Pythagorean theorem cannot be applied directly.

The calculator supports two measurement sets:

Task You enter You get
SAS: find side c Sides aa and bb and the included angle CC The third side cc
SSS: find angle C Sides aa, bb, and cc The interior angle CC

SAS means "side, angle, side": the angle sits between the two known sides. SSS means all three sides are known. If you only know two sides and an angle that is not between them (SSA), or you know two angles and one side (ASA or AAS), this calculator does not apply. Those cases are usually handled with the law of sines.

The cosine law formula

Label the triangle so that each lowercase side is opposite the uppercase angle with the same letter: side aa is opposite angle AA, side bb is opposite BB, and side cc is opposite CC. Angle CC is therefore the angle between sides aa and bb. With that convention, the cosine law formula is:

c2=a2+b2−2abcos⁡Cc^2 = a^2 + b^2 - 2ab\cos C

The same rule holds for each side, by cycling the letters:

a2=b2+c2−2bccos⁡Aa^2 = b^2 + c^2 - 2bc\cos A
b2=a2+c2−2accos⁡Bb^2 = a^2 + c^2 - 2ac\cos B

Think of it as the Pythagorean theorem with a correction term. When C=90∘C = 90^\circ, cos⁡C=0\cos C = 0, the correction vanishes, and the rule becomes c2=a2+b2c^2 = a^2 + b^2. For other angles the term −2abcos⁡C-2ab\cos C adjusts the result:

  • If CC is acute, cos⁡C>0\cos C > 0, so cc is shorter than it would be in the right-angle case.
  • If CC is obtuse, cos⁡C<0\cos C < 0, so cc is longer.

This holds with aa and bb held fixed, and it is a quick way to sanity-check any answer.

Rearranging for an angle (SSS)

When all three sides are known, solve the formula for the cosine and take the inverse cosine:

C=arccos⁡(a2+b2−c22ab)C = \arccos\left(\frac{a^2 + b^2 - c^2}{2ab}\right)

The other two angles follow the same pattern:

A=arccos⁡(b2+c2−a22bc)A = \arccos\left(\frac{b^2 + c^2 - a^2}{2bc}\right)
B=arccos⁡(a2+c2−b22ac)B = \arccos\left(\frac{a^2 + c^2 - b^2}{2ac}\right)

Inverse cosine returns values from 0∘0^\circ to 180∘180^\circ, so it correctly yields obtuse interior angles. This is a real advantage over the sine rule, where arcsin⁡\arcsin cannot distinguish an acute angle from its obtuse supplement.

How to use the calculator

  1. Pick the task in "Solve for." Choose "SAS — find side c" if you know two sides and the included angle, or "SSS — find angle C" if you know all three sides. SAS is the default.
  2. Choose the angle unit. Degrees (°) is the default; Radians (rad) is also available. Switching units converts the current included-angle value, so you do not need to redo the conversion yourself. The relationships are radians == degrees × π/180\times\,\pi/180 and degrees == radians × 180/π\times\,180/\pi.
  3. Enter the sides. Type values or use the sliders. Side aa and side bb are always active. Side cc is active only in SSS. Every side must be a finite length from 10−610^{-6} to 10910^{9}.
  4. Enter the included angle CC (SAS only). It must be at least 10−610^{-6} degrees and at most 179.999999179.999999 degrees, or at least 10−610^{-6} radians and strictly below π\pi. A straight angle is not a triangle and is rejected.
  5. Read the result. Simple mode shows "Side c" for SAS or "Angle C" for SSS. Switch to Advanced to also see angles AA and BB, the area, and the perimeter.
  6. Open "Worked steps" to see the formula, your values substituted into it, and the result. Advanced mode adds detail for the remaining outputs.

Keep your units consistent

All sides must use the same length unit, whether centimeters, meters, feet, or anything else. The calculator does no length conversion. Lengths and perimeter are labeled "units" and area is labeled "units²," so read them in whichever unit you entered. Mixing, say, meters for aa and centimeters for bb will give a meaningless answer.

Check the opposite-side convention

The most common input mistake is matching a side to the wrong angle. Angle CC must be the angle between sides aa and bb, and side cc must be the side across from CC. If your diagram uses different letters, relabel it before entering values. The triangle diagram in the calculator labels vertices AA, BB, and CC and the opposite sides, and it marks CC as the given included angle in SAS or the solved angle in SSS, so you can compare it with your own sketch.

Example triangles

The "Example triangles" menu loads ready-made cases:

Menu item Loads
Custom triangle Manual entry
3–4–5 right triangle SAS, degrees, a=3a=3, b=4b=4, C=90∘C=90^\circ
Equilateral SSS SSS, degrees, a=b=c=2a=b=c=2
Obtuse SSS SSS, degrees, a=5a=5, b=7b=7, c=10c=10

Reset returns the calculator to Simple mode, degrees, and the default SAS values (a=3a=3, b=4b=4, C=90∘C=90^\circ).

Worked example 1: SAS, finding a side

Suppose a=3a = 3, b=4b = 4, and the included angle is C=90∘C = 90^\circ.

Step 1: write the formula.

c2=a2+b2−2abcos⁡Cc^2 = a^2 + b^2 - 2ab\cos C

Step 2: substitute.

c2=32+42−2(3)(4)cos⁡90∘c^2 = 3^2 + 4^2 - 2(3)(4)\cos 90^\circ

Step 3: evaluate. Since cos⁡90∘=0\cos 90^\circ = 0:

c2=9+16−0=25c^2 = 9 + 16 - 0 = 25

Step 4: take the positive square root.

c=25=5c = \sqrt{25} = 5

This is the classic 3–4–5 right triangle. In Advanced mode the calculator also reports A≈36.8699∘A \approx 36.8699^\circ, B≈53.1301∘B \approx 53.1301^\circ, area =6= 6 units², and perimeter =12= 12 units.

The area comes from the SAS area formula:

K=12absin⁡C=12(3)(4)(1)=6K = \tfrac{1}{2}ab\sin C = \tfrac{1}{2}(3)(4)(1) = 6

A note on numerical stability

The expression a2+b2−2abcos⁡Ca^2 + b^2 - 2ab\cos C can lose precision when aa and bb are nearly equal and CC is tiny, because two large, almost identical quantities get subtracted. To reduce this cancellation, the calculator internally computes the algebraically equivalent form:

c=(a−b)2+4absin⁡2(C2)c = \sqrt{(a-b)^2 + 4ab\sin^2\left(\frac{C}{2}\right)}

The answer is mathematically the same. The worked steps still display the standard cosine-rule form, which is the one to use by hand.

Worked example 2: SSS, finding an angle

Take the obtuse triangle with a=5a = 5, b=7b = 7, and c=10c = 10. The longest side is cc, so the angle opposite it, CC, should be the largest, and likely obtuse.

Step 1: write the rearranged formula.

cos⁡C=a2+b2−c22ab\cos C = \frac{a^2 + b^2 - c^2}{2ab}

Step 2: substitute.

cos⁡C=52+72−1022(5)(7)=25+49−10070\cos C = \frac{5^2 + 7^2 - 10^2}{2(5)(7)} = \frac{25 + 49 - 100}{70}

Step 3: simplify.

cos⁡C=−2670=−1335\cos C = \frac{-26}{70} = -\frac{13}{35}

Step 4: apply inverse cosine.

C=arccos⁡(−1335)≈111.8037∘C = \arccos\left(-\frac{13}{35}\right) \approx 111.8037^\circ

The negative cosine is the signal of an obtuse angle. If the numerator a2+b2−c2a^2 + b^2 - c^2 is negative, the angle opposite cc is greater than 90∘90^\circ; if it is zero, the angle is exactly 90∘90^\circ; if positive, the angle is acute.

Other angles follow with the cyclic formulas. Internally the calculator uses equivalent angle formulas based on atan2, which are more stable for extreme shapes, while the displayed steps use the standard cosine-rule expressions.

Two more SSS checks

  • 3–4–5 in radians. For a=3a=3, b=4b=4, c=5c=5, we get cos⁡C=(9+16−25)/24=0\cos C = (9+16-25)/24 = 0, so C=90∘C = 90^\circ. In radian mode the calculator shows this as 1.570796 rad, which is π/2\pi/2.
  • Equilateral triangle. For a=b=c=2a=b=c=2, cos⁡C=(4+4−4)/8=12\cos C = (4+4-4)/8 = \tfrac12, so every angle is 60∘60^\circ. The perimeter is 66 and the area is 3≈1.732051\sqrt{3} \approx 1.732051.

Area and perimeter

In Advanced mode the calculator reports both.

Perimeter is simply the sum of the sides:

P=a+b+cP = a + b + c

Area depends on the task. For SAS:

K=12absin⁡CK = \tfrac{1}{2}ab\sin C

For SSS, the calculator uses a numerically stable rearrangement of Heron's formula:

K=s(s−a)(s−b)(s−c),s=a+b+c2K = \sqrt{s(s-a)(s-b)(s-c)}, \qquad s = \frac{a+b+c}{2}

Both approaches give the same mathematical area. For the equilateral example, s=3s = 3 and K=3⋅1⋅1⋅1=3K = \sqrt{3 \cdot 1 \cdot 1 \cdot 1} = \sqrt{3}.

When a triangle is invalid

Not every set of three lengths forms a triangle. For positive sides, each pair must sum to more than the third side. Equivalently:

∣a−b∣<c<a+b|a - b| < c < a + b

If equality holds, the three points lie on a straight line and the triangle is degenerate, with zero area. For example, a=2a=2, b=3b=3, c=5c=5 gives 2+3=52 + 3 = 5, so the sides are collinear and the calculator rejects them.

The calculator also applies a stability check. Sort the sides from longest to shortest as x≥y≥zx \ge y \ge z. The triangle is rejected when:

y+z−xx≤10−12\frac{y + z - x}{x} \le 10^{-12}

This excludes some nearly collinear triangles that would technically have a positive area in exact arithmetic but cannot be solved reliably in floating-point arithmetic.

When any active input is invalid, the calculator:

  • shows an error on the offending field,
  • replaces visible results with an em dash (—),
  • clears the worked steps,
  • replaces the diagram with a request for valid measurements, and
  • disables Copy and Print.

Very thin triangles

A valid triangle whose smallest angle is strictly below 1∘1^\circ gets the note "A very thin triangle." This is a sensitivity warning, not an error. Tiny rounding differences in the measured sides can shift a very small angle by a large relative amount. For instance, with a=b=1a = b = 1 and c=0.01c = 0.01, the calculator returns C≈0.5729602∘C \approx 0.5729602^\circ and shows the advisory. If your inputs come from physical measurements, treat results for such triangles with extra caution.

Reading the output

  • Precision. Results display up to seven significant digits. Nonzero values with magnitude below 10−410^{-4}, or at least 10910^{9}, switch to scientific notation. Intermediate steps are rounded only for display; the calculator carries unrounded values forward, so the final answer is not degraded by rounding in the steps.
  • Angle units. Angle outputs follow the unit you selected.
  • Sanity checks. The longest side is always opposite the largest angle. The three interior angles sum to 180∘180^\circ (or π\pi radians). An acute included angle in SAS yields a shorter opposite side than the right-angle case, and an obtuse one yields a longer side.
  • Copy and Print. Copy saves a summary of the active inputs and visible results. Export/Print opens your browser's print dialog, where you can choose "Save as PDF."
  • Diagram. The scaled triangle updates as you change measurements. Zoom and reset view affect only the drawing, never the calculation.

When to use the cosine rule instead of other methods

Situation Best tool
Right triangle, two sides known Pythagorean theorem (a special case of the cosine rule)
Two sides and the included angle (SAS) Law of cosines
Three sides (SSS) Law of cosines
Two angles and a side (ASA, AAS) Law of sines (outside this calculator)
Two sides and a non-included angle (SSA) Law of sines, with care for the ambiguous case (outside this calculator)

A common workflow is to use the cosine rule first to find one side or angle, then finish the remaining angles with either the cosine rule again or the sine rule.

Frequently asked questions

What is the law of cosines?

It is a formula that connects the three sides of a triangle with one angle: c2=a2+b2−2abcos⁡Cc^2 = a^2 + b^2 - 2ab\cos C. It generalizes the Pythagorean theorem to triangles that do not have a right angle.

Is the law of cosines the same as the cosine rule?

Yes. "Law of cosines," "cosine law," and "cosine rule" all name the same relationship. A cos rule calculator and a law of cosine solver both do what this tool does.

Can I use this as a law of cosines SSS calculator?

Yes. Select "SSS — find angle C," enter all three sides, and the calculator returns angle CC. Advanced mode adds angles AA and BB, plus the area and perimeter.

Does the calculator show steps?

Yes. "Worked steps" is available in both Simple and Advanced modes. It shows the applicable cosine rule, your values substituted into it, and the result. Advanced mode adds detail for the other outputs.

Can the calculator handle radians?

Yes. Set the angle unit to Radians (rad). Angle inputs and angle outputs then use radians, and switching units converts the current included-angle value automatically.

Why does it say my triangle is invalid?

Either the sides fail the triangle inequality (∣a−b∣<c<a+b|a-b| < c < a+b), the sides are collinear or so nearly collinear that they fall inside the calculator's stability check, or a value is outside the accepted range. Check the field error for details.

What is the difference between the law of cosines and the law of sines?

The law of cosines uses SAS or SSS information. The law of sines is typically used with ASA, AAS, or SSA information. For SSS, the cosine rule is the right choice because it returns obtuse angles unambiguously.

Can the law of cosines find an obtuse angle?

Yes. A negative value of (a2+b2−c2)/(2ab)(a^2 + b^2 - c^2)/(2ab) means the angle opposite cc is obtuse, and inverse cosine returns it directly. In the 55–77–1010 example, C≈111.8037∘C \approx 111.8037^\circ.

Does the calculator work for triangles on a sphere?

No. It solves Euclidean planar triangles only.

Calculation Methodology Documented

Read the formula definitions, supported input ranges, and limitations before using this estimate.

Review Methodology
Debjeet Bhowmick
Author Debjeet Bhowmick AWS Certified Solutions Architect • Cloud & DevOps Consultant

AWS Certified Solutions Architect Professional and Senior Cloud & DevOps Consultant with over 14 years of hands-on expertise architecting resilient cloud infrastructure across AWS, GCP, and Azure. Specializes in infrastructure as code, continuous delivery automation, and telemetry across Terraform, Ansible, Docker, Kubernetes, ELK, Python, and SQL. Creator of OrangeCalc, delivering client-side high-precision computational tools.