Distance between two points

The straight-line distance between two points, with the midpoint and the slope. It uses the scaled form rather than squaring first, because squaring first returns Infinity for points far apart and zero for points close together. Nothing you type here leaves your browser — it is worked out on your own device, so there is nothing to send and nothing to log.

Why not just square it and take the root?

The distance between two points is taught as √dx² + dy², and the squaring is the problem. A double holds numbers up to about 1.8×10308, so dx² overflows once dx passes roughly 1.3×10154 — and underflows to nothing once dx drops below about 10−162.

(0, 0) to (10200, 10200) the naive formula returns Infinity
(0, 0) to (10−200, 10−200) it returns 0 — two different points reported as the same place

Neither answer is merely imprecise; both are meaningless, and neither raises an error. Math.hypot scales the operands before squaring them and returns the right answer in both cases, so that is what this page uses. The same function gives the slant height of a cone and the hypotenuse of a right triangle, for the same reason.

As with the area above, the example everyone checks with — a 3-4-5 pair — gives 5 either way.

Where to go next