Law of cosines calculator
Give two sides and the angle between them to get the third side, or all three sides to get an angle: sides of 5 and 7 meeting at 60° leave a third side of 6.244998. Lengths that cannot form a triangle are refused. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- A
- 5
- B
- 7
- Angle c
- 60
- Calculation mode
- sas
- C
- 6.244998
Send two sides and the included angle and it returns the third side; send all three sides and it returns the angle opposite one of them. Every result comes from a single deterministic evaluation of c² = a² + b² − 2ab·cos C (or its inverse), with no approximation tables, no iteration and no guesswork.
Two sides and the angle between them
The classic use of the law of cosines is the SAS case: you know two sides of a triangle and the angle between them, and you need the side opposite that angle. The formula c² = a² + b² − 2ab·cos C generalizes the Pythagorean theorem — when C is exactly 90 degrees the cosine term vanishes and you get c² = a² + b² back. The response is the side c, rounded to six decimals so results are stable across machines.
This is the calculation behind countless real measurements: the distance across a lake from two measured shore points, the third side of a roof truss, the diagonal of a parallelogram of forces. Because the included angle must be strictly inside the open interval, degenerate answers — a collapsed or flattened triangle — are refused up front rather than returned as misleading numbers.
Three sides: recovering the angle
The second mode runs the formula backwards. Before computing anything it verifies the triangle inequality: the longest side must be strictly shorter than the sum of the other two. A clamp on the cosine value protects the arccosine from floating-point dust on nearly degenerate input, so a valid-but-skinny triangle still yields a clean answer.
What you can do with it
Measure an unreachable distance
Two measured sides and the angle between them give the distance across a river, a lot line or a warehouse diagonal that cannot be taped directly.
Find a joint angle from member lengths
With three sides known from a parts list, recover the exact angle a bracket, truss or robotic arm must be cut or driven to.
FAQ
Which two problems does it solve?
SAS: two sides and the included angle give the third side. SSS: three sides give the angle opposite side c.
Can I send both the angle and the third side?
No. Sending both is ambiguous about what you want computed, so it is rejected.
Is the result deterministic?
Yes. It is one closed-form evaluation with fixed six-decimal rounding — the same input always returns the same output, with no randomness or iteration.
What happens if the three sides cannot form a triangle?
The calculation is refused with a clear message. The longest side must be strictly shorter than the other two added together, so lengths such as 2, 3 and 9 cannot make any triangle.
How is the law of cosines related to Pythagoras?
It is the general version. The third side squared equals the other two squared and added, minus twice their product times the cosine of the angle between them; at exactly 90° that last term vanishes and Pythagoras remains.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/math/law-of-cosines \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"a":5,"b":7,"angle_c":60}'const res = await fetch("https://api.kit.forhosting.com/math/law-of-cosines", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"a": 5,
"b": 7,
"angle_c": 60
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/math/law-of-cosines",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"a": 5,
"b": 7,
"angle_c": 60
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/math/law-of-cosines", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"a":5,"b":7,"angle_c":60}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"a":5,"b":7,"angle_c":60}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/math/law-of-cosines", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"a": 5,
"b": 7,
"angle_c": 60
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "math.law_of_cosines",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
max_side | 1000000000000 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
The law of cosines API solves the two problems the Pythagorean theorem cannot touch: triangles with no right angle. Inputs that cannot form a real triangle — a zero side, a 180-degree angle, three lengths that fail the triangle inequality — are rejected with a clear invalid_input error and are never charged. The same code runs free in your browser on this page, so you can check a result by hand before you ever automate the call.
Two sides and the included angle: finding the third side
To use this mode, send fields a, b and angle_c, with the angle in degrees strictly between 0 and 180.
Three sides: recovering the angle
Given all three sides a, b and c, the endpoint rearranges the law of cosines to cos C = (a² + b² − c²) / (2ab) and returns the angle C opposite side c, in degrees. Three lengths like 2, 3 and 9 cannot form any triangle, and the call fails with an invalid_input error saying exactly that instead of producing NaN. This mode is what surveyors use when distances are cheap to measure but angles are not, what a CNC programmer uses to find the angle of a joint from its member lengths, and what students meet as the SSS case in trigonometry courses.
Errors, determinism and what it costs
The contract is deliberately strict about input. The computation itself is constant-time arithmetic plus a single cosine or arccosine, fully deterministic: the same input always returns byte-identical output, which is why the published examples can be checked exactly. Sides must be positive finite numbers up to 10¹²; the angle must lie strictly between 0 and 180 degrees; and you must send either angle_c or c, never both and never neither. Every violation raises invalid_input with a message naming the offending field, and invalid calls are never billed.
Integration use cases
Check triangle data in a pipeline
Reject impossible geometry early: sides that violate the triangle inequality fail with invalid_input before they reach downstream calculations.
Technical questions
What does it cost?
$0.002 per request, flat — there is no per-unit charge. It is also free to run in your browser on this page.
Are angles in degrees or radians?
Degrees, always. angle_c goes in as degrees and SSS mode returns the angle in degrees.
What happens with sides like 2, 3 and 9?
The call fails with an invalid_input error explaining that the triangle inequality is violated. Impossible triangles are never silently answered.