Diagonal of a rectangle
The diagonal of a rectangle API takes a width and a height and returns the exact diagonal length, applying the Pythagorean theorem to the right triangle formed by two adjacent sides.
Run — free
It is the measurement you need when sizing a screen, cutting a brace, checking whether an item fits through a frame corner to corner, or validating a layout — one call, one number, no spreadsheet and no manual square roots. Both dimensions must be positive finite numbers; anything else is rejected with a clear error and is never billed.
What the diagonal of a rectangle is
Every rectangle hides two identical right triangles: draw a line from one corner to the opposite corner and you have split the shape along its diagonal, making that line the hypotenuse of a triangle whose legs are the width and the height. The diagonal of a rectangle calculator applies the Pythagorean theorem — the square of the diagonal equals the square of the width plus the square of the height — and returns the square root of that sum. A rectangle three units wide and four units tall has a diagonal of exactly five; a square with a side of one has a diagonal of the square root of two, about 1.414214. The result is expressed in whatever unit you fed in: give it centimeters and you get centimeters, give it inches and you get inches. Because the computation is a single closed-form formula, the answer is exact, deterministic and instantaneous, with no approximation steps, no iterative solver and no rounding surprises beyond a stable six-decimal presentation.
How the computation and validation work
You send a JSON object with two fields, width and height, and receive an object echoing both dimensions alongside the computed diagonal. Validation is deliberately strict: each dimension must be a positive finite number. Zero, negative values, NaN, infinities, missing fields and values that are not numbers at all are all rejected with an invalid input error, and rejected calls are never billed. Numeric strings are accepted as a convenience, since web forms and query parameters transport everything as text. Internally the diagonal is computed with a hypotenuse primitive rather than the naive square-root-of-sum-of-squares expression, which keeps the result accurate even for very large dimensions where squaring first could overflow into infinity. The final figure is rounded to six decimal places so that automated consumers comparing outputs across machines and architectures see byte-identical results. There is no randomness, no clock, no network access and no hidden state: the same input always produces the same output, on the API and in the free browser version.
Where this measurement shows up in practice
Screen and display sizes are quoted as diagonals, so anyone building a catalog, a comparison tool or an electronics feed constantly converts between width-by-height and diagonal. Carpentry and construction reach for the diagonal whenever a frame must be squared: if both diagonals of a rectangular frame measure the same, the corners are true right angles. Shipping and packaging use the diagonal to decide whether a long object fits corner to corner inside a box or through a rectangular opening. In graphics and layout work, the diagonal normalizes distances and scales corner radii. This endpoint serves all of those cases from a single deterministic call that runs on our global edge, costs $0.002 per request and keeps nothing afterwards. The identical code powers the free calculator on this page, so what you test interactively is exactly what your integration will receive when you automate it at scale.
What you can do with it
Screen size tooling
Derive the marketed diagonal size of a display from its physical width and height, or verify a listing that quotes one against the other.
Squaring a frame in construction
Compare the expected diagonal of a rectangular frame against the measured one to confirm the corners are true right angles before fastening.
Fit checks for shipping and storage
Decide whether an item fits corner to corner inside a rectangular box, crate or opening by comparing its length against the diagonal.
FAQ
What does it cost?
$0.002 per request. It is also free to run in your browser on this page.
What formula does it use?
The Pythagorean theorem: the diagonal is the square root of the width squared plus the height squared, computed with an overflow-safe hypotenuse primitive.
What input is rejected?
Any width or height that is not a positive finite number: zero, negatives, NaN, infinity or missing fields. Rejected calls return an invalid input error and are not billed.
What unit is the result in?
The same unit as your input. Send width and height in centimeters and the diagonal comes back in centimeters.
Does it work for squares?
Yes. A square is a rectangle with equal width and height, so passing the same value twice returns the side times the square root of two.
How precise is the result?
The diagonal is rounded to six decimal places, which keeps outputs identical across machines and architectures.
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/diagonal-of-rectangle \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"width":3,"height":4}'const res = await fetch("https://api.kit.forhosting.com/math/diagonal-of-rectangle", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"width": 3,
"height": 4
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/math/diagonal-of-rectangle",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"width": 3,
"height": 4
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/math/diagonal-of-rectangle", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"width":3,"height":4}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"width":3,"height":4}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/math/diagonal-of-rectangle", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"width": 3,
"height": 4
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "math.diagonal_of_rectangle",
"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_mb | 1 |
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. |