ForHosting KIT · Math

Gradient calculator

Give two points and get the slope of the line that joins them: from (1, 2) to (4, 8) the gradient is 2, so the line climbs two units for every unit across. Positive rises, negative falls, zero is flat. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
X1
1
Y1
2
X2
4
Y2
8
You get
Slope
2

You send x1, y1, x2 and y2, and you get back the gradient m = (y2 − y1) / (x2 − x1), the single number that says how steep the line is and in which direction it climbs. A positive result rises left to right, a negative one falls, and zero means the line is perfectly horizontal.

What the gradient of a line actually tells you

The gradient, also called the slope, is the ratio between how much a line rises and how far it runs: rise over run. Given two points (x1, y1) and (x2, y2), the rise is y2 − y1 and the run is x2 − x1, so the gradient is m = (y2 − y1) / (x2 − x1).

Its sign carries meaning: a positive gradient means the line climbs as x grows, a negative gradient means it descends, and a gradient of exactly zero means both points sit at the same height and the line is horizontal. Its magnitude carries meaning too: a gradient of 3 climbs three units for every unit of horizontal travel, while a gradient of 0.25 barely tilts.

How the result is computed and why vertical lines fail

The result is rounded to six decimal places so the output is byte-stable across machines — a raw floating-point quotient like 0.1 / 0.3 could print differently on another CPU, and stable output matters when you compare or cache results. There is exactly one case where the formula has no answer: when x1 equals x2. Then the run is zero, the line is vertical, and division is undefined.

All four coordinates must be finite numbers; missing or non-numeric fields are rejected the same way.

Where a gradient calculation fits in real work

Gradients appear anywhere two measurements define a direction. In civil engineering the slope between two surveyed points is a grade; in finance the slope between two price observations is a rate of change; in machine learning the slope between two loss values tells you whether training is improving; in physics the slope of a position-time line is a velocity. Nothing you send is stored: the four coordinates are read, the division is done, and the slope is returned.

Check a ramp or road grade

Feed two surveyed elevation points and verify the gradient stays inside the permitted maximum before the design moves forward.

Measure a rate of change between readings

Compute the slope between two sensor or price observations to quantify how fast a value is rising or falling.

Feed downstream geometry

Use the gradient as the input to line equations, inclination angles or parallel and perpendicular line checks in an automated pipeline.

What formula is used?

The classic two-point slope formula: m = (y2 − y1) / (x2 − x1).

Does the order of the points matter?

No. Swapping the points changes the sign of both the rise and the run, so the gradient is identical either way.

How precise is the result?

The slope is rounded to six decimal places so the output is stable and comparable across machines and languages.

Is anything I send stored?

No. The four coordinates are read, the division is computed, and only the slope is returned.

What happens if both points have the same x value?

The line is vertical, so the run is zero and the slope is undefined. Instead of showing infinity or a made-up large number, the calculator tells you the line is vertical so you can check your points.

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.

POSThttps://api.kit.forhosting.com/math/gradient

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.

curl -X POST https://api.kit.forhosting.com/math/gradient \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"x1":1,"y1":2,"x2":4,"y2":8}'
{
  "x1": 1,
  "y1": 2,
  "x2": 4,
  "y2": 8
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "math.gradient",
  "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.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

The gradient API computes the slope of the straight line that passes through two points, given only their x and y coordinates. When both points share the same x coordinate the line is vertical, its gradient is undefined, and the endpoint says so with a clear validation error instead of a silent infinity. It runs on our global edge in a single deterministic pass, with nothing stored, and the very same code runs free in your browser on this page.

What the gradient of a line actually tells you

Because the gradient depends only on the two endpoints, it does not matter which point you label first — swapping them changes the sign of both the rise and the run, and the quotient stays identical. This one number is the foundation of coordinate geometry: once you have it, the full equation of the line, its inclination angle, and its parallels and perpendiculars all follow directly.

How the result is computed and why vertical lines fail

The endpoint performs the classic two-point formula in a single deterministic pass: it subtracts the y coordinates to get the rise, subtracts the x coordinates to get the run, and divides. Rather than returning Infinity, NaN, or a made-up large number, the API rejects the input with an invalid_input error that explains the line is vertical. That is a deliberate design choice: an explicit error lets a calling pipeline stop and branch, while a silent infinity would flow downstream and corrupt averages, fits and further geometry.

Where a gradient calculation fits in real work

Automating the calculation through an API makes sense when the points are produced by software rather than typed by a person: a sensor pipeline that needs the tilt between two readings, a charting service that normalizes trend lines, or a validation job that checks whether a ramp design stays within a maximum gradient. Each request costs $0.002, and the same code runs free in your browser on this page, so you can verify a handful of points by hand and only pay when you automate the call at volume.

What does it cost?

$0.002 per request. It is also free to run in your browser on this page.

What happens when both points have the same x coordinate?

The line is vertical and its gradient is undefined, so the request is rejected with an invalid_input error instead of returning Infinity.

Read the full KIT documentation →