hypot
Calculates the length of the hypotenuse of a right triangle, or a point's distance from the origin.
hypot(float $x, float $y): float- Added in PHP 4.1.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | float | Length of the first side. |
$y | float | Length of the second side. |
Return value
floatThe calculated length of the hypotenuse, i.e. the square root of x squared plus y squared.
hypot() returns the same value as sqrt($x * $x + $y * $y), but it computes it the way the C library does: by scaling the inputs before squaring them. That difference matters more often than people expect.
When to reach for it
Use it whenever you need a Euclidean distance in two dimensions — the gap between two points on a canvas, the length of a drag gesture, the magnitude of a velocity vector, or the radius half of a polar coordinate. Subtract the coordinates and pass the two deltas.
Why not write the formula by hand
Squaring each side first creates an intermediate value twice as large in magnitude. With big inputs that intermediate overflows to INF, and with tiny inputs it underflows to 0.0, so you get a garbage answer from a calculation whose real result is perfectly representable as a float. hypot() sidesteps both, and is generally a touch more accurate in the middle of the range too.
The function handles exactly two dimensions. For a 3D distance, nest the calls: hypot(hypot($dx, $dy), $dz). Integer arguments are coerced to float, and the return value is always a float even when the answer is a whole number.
Example
When to use it
- Measuring how far a user dragged an element on a canvas, in pixels.
- Turning an (x, y) offset into a radius before calling atan2() for the angle.
- Computing the magnitude of a 2D velocity or acceleration vector in a physics step.
- Finding the nearest store to a coordinate pair on a projected (flat) map.
Frequently asked questions
What is the difference between hypot and sqrt in PHP?
hypot($x, $y) computes the same value as sqrt($x*$x + $y*$y) but scales the inputs first, so it does not overflow to INF for very large values or underflow to 0 for very small ones. Use hypot() whenever the inputs are not tightly bounded.
Can hypot calculate 3D distance?
Not directly — it takes exactly two arguments. Nest the calls instead: hypot(hypot($dx, $dy), $dz) gives the correct 3D distance and keeps the overflow protection.
How do I find the distance between two points in PHP?
Subtract the coordinates and pass the deltas: hypot($x2 - $x1, $y2 - $y1). That is the Euclidean distance in whatever unit the coordinates use.
More examples
Why hypot() beats the hand-written formula
The hand-written version squares first. 1e200 squared is 1e400, far past PHP_FLOAT_MAX, so it becomes INF and the square root stays INF. At the other end, 1e-200 squared underflows to 0, so sqrt() returns 0. hypot() scales the operands before squaring and returns the correct finite answer (roughly 1.414e200 and 1.414e-200) in both cases.
<?php
$big = 1.0e200;
var_dump(sqrt($big * $big + $big * $big));
var_dump(hypot($big, $big));
$small = 1.0e-200;
var_dump(sqrt($small * $small + $small * $small));
var_dump(hypot($small, $small));Cartesian to polar coordinates
hypot() supplies the radius and atan2() supplies the angle — together they convert an (x, y) pair to polar form. The three classic right triangles come out exactly: 3-4-5, 5-12-13 and a straight 7 unit drop at -90 degrees.
<?php
$points = [[3, 4], [-5, 12], [0, -7]];
foreach ($points as [$x, $y]) {
$r = hypot($x, $y);
$deg = rad2deg(atan2($y, $x));
printf("(%3d,%3d) r=%5.2f angle=%7.2f deg\n", $x, $y, $r, $deg);
}
Discussion