function
<cmath> <ctgmath>

hypot

     double hypot  (double x     , double y);
      float hypotf (float x      , float y);
long double hypotl (long double x, long double y);
     double hypot (double x     , double y);
      float hypot (float x      , float y);
long double hypot (long double x, long double y);
     double hypot (Type1 x      , Type2 y);       // additional overloads
Compute hypotenuse
Returns the hypotenuse of a right-angled triangle whose legs are x and y.

The function returns what would be the square root of the sum of the squares of x and y (as per the Pythagorean theorem), but without incurring in undue overflow or underflow of intermediate values.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

Parameters

x, y
Floating point values corresponding to the legs of a right-angled triangle for which the hypotenuse is computed.

Return Value

The square root of (x2+y2).
If the magnitude of the result is too large to be represented by a value of the return type, the function may return HUGE_VAL (or HUGE_VALF or HUGE_VALL) with the proper sign (in which case, an overflow range error occurs):

If an overflow range error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: FE_OVERFLOW is raised.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
/* hypot example */
#include <stdio.h>      /* printf */
#include <math.h>       /* hypot */

int main ()
{
  double leg_x, leg_y, result;
  leg_x = 3;
  leg_y = 4;
  result = hypot (leg_x, leg_y);
  printf ("%f, %f and %f form a right-angled triangle.\n",leg_x,leg_y,result);
  return 0;
}


Output:

3.000000, 4.000000 and 5.000000 form a right-angled triangle.

See also