Namespaces
Variants
Views
Actions

hypot

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
hypot
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
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)

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. This is the length of the hypotenuse of a right-angled triangle with sides of length x and y, or the distance of the point (x,y) from the origin (0,0), or the magnitude of a complex number x+iy.

Contents

[edit] Parameters

x - floating point value
y - floating point value

[edit] Return value

The hypotenuse of a right-angled triangle, x2
+y2
.

[edit] Exceptions

If the result overflows, a range error may occur and FE_OVERFLOW may be raised.

If the result is subnormal, an underflow error may occur and FE_UNDERFLOW may be raised.

[edit] Notes

Typical implementation strategy is to calculate an equivalent of u1+(
v
u
)2
where u is max(x,y) and v is min(x,y).

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf("hypot(3.0,4.0)      = %.1f\n", hypot(3.0,4.0));
    printf("hypot(INFINITY,4.0) = %.1f\n", hypot(INFINITY,4.0));
    printf("hypot(INFINITY,NAN) = %.1f\n", hypot(INFINITY,NAN));
    printf("hypot(NAN,4.0)      = %.1f\n", hypot(NAN,4.0));
    printf("\n");
 
    /* range error                         */
    /* "inexact" floating-point exception  */
    /* "overflow" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("hypot(DBL_MAX,DBL_MAX) = %g\n", hypot(DBL_MAX,DBL_MAX));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

hypot(3.0,4.0)      = 5.0
hypot(INFINITY,4.0) = inf
hypot(INFINITY,NAN) = inf
hypot(NAN,4.0)      = nan
 
hypot(DBL_MAX,DBL_MAX) = inf
Numerical result out of range
current exceptions raised:  FE_INEXACT FE_OVERFLOW

[edit] See also

computes square root (x)
(function) [edit]
raises a number to the given power (xy)
(function) [edit]