Skip to content

Core\Math::hypot

Core\Math::hypot returns the length of the longest side of a right triangle. You give it the lengths of the two other sides. This is also the straight-line distance between two points on a flat map: give it the difference in x and the difference in y.

The result is the square root of $a * $a + $b * $b. You could write that with Core\Math::sqrt, but it fails for very big numbers. $a * $a can be too big for a float and become INFINITY, even when the length itself fits. Core\Math::hypot gives the correct length there too.

The sign of each side does not change the result, and the result is never negative.

The examples below show two triangles, then the difference from the sqrt form with a very big number, then a program that finds the nearest store on a map.

Parameters

  • $afloatThe length of one side. Its sign does not change the result.
  • $bfloatThe length of the other side. Its sign does not change the result.

Return value

Returns float. The length. It is never negative. When $a or $b is INFINITY or -INFINITY, the result is INFINITY, even when the other one is NaN.

Examples

Two triangles

<?nvs
// Shows how to get the longest side of a right triangle from the other two sides.
// The sides 3 and 4 give a longest side of 5. This prints "5".
echo Core\Math::hypot(3.0, 4.0), "\n";
// The sides 5 and 12 give 13. This prints "13".
echo Core\Math::hypot(5.0, 12.0), "\n";
// A negative side gives the same result. This prints "13".
echo Core\Math::hypot(-5.0, 12.0), "\n";
Output
5
13
13

Compared with sqrt

<?nvs
// Shows why `Core\Math::hypot` is safer than the `sqrt` form for very big numbers.
float $side = 1.0e200;
// `$side * $side` is too big for a `float`, so it is `INFINITY`.
// The square root of `INFINITY` is `INFINITY`. This prints "INF".
echo Core\Math::sqrt($side * $side + $side * $side), "\n";
// `Core\Math::hypot` gives the correct length. This prints "1.4142135623731E+200".
echo Core\Math::hypot($side, $side), "\n";
// For small numbers, both forms give the same result. This prints "5" twice.
echo Core\Math::sqrt(3.0 * 3.0 + 4.0 * 4.0), "\n";
echo Core\Math::hypot(3.0, 4.0), "\n";
Output
INF
1.4142135623731E+200
5
5

The nearest store

<?nvs
// Shows how a delivery program finds the store nearest to a customer on a flat map.
class Map
{
// Returns the straight-line distance between two points, in kilometres.
public static function distance(float $x1, float $y1, float $x2, float $y2): float
{
return Core\Math::hypot($x2 - $x1, $y2 - $y1);
}
}
// The customer lives at x = 2 and y = 3.
float $north = Map::distance(2.0, 3.0, 5.0, 7.0);
float $station = Map::distance(2.0, 3.0, 1.0, 1.0);
// This prints "North store: 5 km" and "Station store: 2.2 km".
echo 'North store: ', $north, " km\n";
echo 'Station store: ', Core\Math::round($station, { precision: 1 }), " km\n";
// This prints "Deliver from: Station store".
echo 'Deliver from: ', $station < $north ? 'Station store' : 'North store', "\n";
Output
North store: 5 km
Station store: 2.2 km
Deliver from: Station store