Previous Page
Next Page


Calculates a hypotenuse by the Pythagorean formula

#include <math.h>
double hypot ( double x , double y  );
float hypotf ( float x , float y  );
long double hypotl ( long double x , long double y  );

The hypot( ) functions compute the square root of the sum of the squares of their arguments, while avoiding intermediate overflows. If the result exceeds the function's return type, a range error may occur.


double x, y, h;            // Three sides of a triangle

printf( "How many kilometers do you want to go westward? " );
scanf( "%lf", &x );

printf( "And how many southward? " );
scanf( "%lf", &y );

errno = 0;
h = hypot( x, y );

if ( errno )
  perror( _  _FILE_  _ );
  printf( "Then you'll be %4.2lf km from where you started.\n", h );

If the user answers the prompts with 3.33 and 4.44, the program prints this output:

Then you'll be 5.55 km from where you started.

See Also

sqrt( ), cbrt( ), csqrt( )

Previous Page
Next Page