hypot, hypotf, hypotl

Header: <math.h>

1-3) Computes the square root of the sum of the squares of x and y, without undue overflow or underflow at intermediate stages of the computation.

# Declarations

float hypotf( float x, float y );

(since C99)

double hypot( double x, double y );

(since C99)

long double hypotl( long double x, long double y );

(since C99)

#define hypot( x, y )

(since C99)

# Parameters

# Return value

If no errors occur, the hypotenuse of a right-angled triangle, (\scriptsize{\sqrt{x^2+y^2} })√x2+y2, is returned.

# Notes

Implementations usually guarantee precision of less than 1 ulp (units in the last place): GNU, BSD.

hypot(x, y) is equivalent to cabs(x + I*y).

POSIX specifies that underflow may only occur when both arguments are subnormal and the correct result is also subnormal (this forbids naive implementations).

hypot(INFINITY, NAN) returns +∞, but sqrt(INFINITY * INFINITY + NAN * NAN) returns NaN.

# Example

#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    // typical usage
    printf("(1,1) cartesian is (%f,%f) polar\n", hypot(1,1), atan2(1, 1));
 
    // special values
    printf("hypot(NAN,INFINITY) = %f\n", hypot(NAN, INFINITY));
 
    // error handling
    errno = 0;
    feclearexcept(FE_ALL_EXCEPT);
    printf("hypot(DBL_MAX,DBL_MAX) = %f\n", hypot(DBL_MAX, DBL_MAX));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

# See also