C programming hypot , hypotf and hypotl math.h


In C programming the <math.h> hypot,hypotf and hypotl function compute the hypotenuse of a right angle triangle.The declaration of the functions are given below.

1 float hypot(float x , float y);
2 double hypot(double x , double y);
3 long double hypot(long double x , long double y);
4 float hypotf(float x , float y);
5 long double hypotl(long double x, long double y);

Parameters:
x -A floating point value.

y -The second floating point value

Return type
floating point -The square root of the sum of square of x and y.

All the functions are the same,they compute the hypotenuse.The 1st,2nd and the 3rd function have the same name i.e.’hypot’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

Some points to note:

i) hypot(x , y) is same as √x2 + y2.

ii) A range error can occur if the resultant value overflow and hence returns ∞.

iii) If the passed values are very small underflow occur and the return value is 0.

iv) If one of the arguments is ∞ the returned value is ∞,even if the other argument is NAN.

v) If one of the arguments is NAN and the other argument is some valid floating point value-not ∞- the returned value is NAN.

Code example

double dx=2 ,dy=4 , x1=2e-200 , x2=2 ;

float fx=2 , fy=4 ;

long double ldx=2 , ldy=4 ;

printf(“%lf”, hypot( dx , dy ) ) ; //calls the 2nd version
printf(“\n%f”, hypot( fx , fy ) ) ; //calls the 1st version
printf(“\n%Lf”, hypot( ldx ,ldy ) ) ;//calls the 3rd version

printf(“\n%lf”, hypot( x1 , x2 ) ) ;
printf(“\n%lf”, hypot( INFINITY , 2 ) ) ;

Output in Visual Studio,

4.472136
4.472136
4.472136
2.000000
inf

In the 4th output the value of x1 is very small so it is taken as 0,hence the output is 2.And for the 5th output since INFINITY(infinity) is passed as it’s argument we get the output as ‘inf'(infinity).






hypotf

The ‘hypotf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘hypotf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=0.32 , y=0.45 ;

printf(“%f”, hypot( x , y ) ); //Calls the 1st version
printf(“\n%f”, hypotf( 0.32 , 0.45 ) ); Calls the 4th version

Output in Visual Studio,

0.552177
0.552177


hypotl

The ‘hypotl‘ function is same as the 1st function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘hypotl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=0.32 , y=0.45 ;

printf(“%Lf”, hypot( x ) ); //Calls the 3rd version
printf(“\n%Lf”, hypotl( 0.32 , 0.45 ) ); Calls the 5th version

Output,

0.552178
0.552178


*Side Note

The hypot function facts.

  i) hypot(x , y) , hypot(y , x), and hypot(x , −y) are equivalent.

  ii) hypot(x , ±0) is equivalent to fabs(x).

LinK : C programming fabs,fabsf and fabsl