# C++ cmath hypot , hypotf and hypotl function

The C++ <cmath> 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.

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

/*Passing floating point value*/
cout<< “hypot(3 , 4)=” << hypot(3 , 4) << endl;

/*Overflow case*/
cout<< “hypot(23e234 , 3e345)=” << hypot(23e234 , 3e345) << endl;

/*Underflow case*/
cout<< “hypot(3e-334 , 1e-330)=” << hypot(3e-334 , 1e-330) << endl;

/*Passing INFINITY*/
cout<< “hypot(INFINITIY , NAN)=” << hypot(INFINITIY , NAN) << endl;

Output

hypot(3 , 4)= 5
hypot(23e234 , 3e345)= inf
hypot(3e-334 , 1e-330)= 0
hypot(INFINITIY , NAN)= inf

#### hypotf and hypotl

*Note

i)The 4th version hypotf is same as the 1st version float hypot(float , float) .The ‘f‘ character appended to ‘hypotf’ stands for ‘float’ which signify the argument type and return type of the function.

ii)The 5th version hypotl is same as the 3rd version long double hypot(long double , long double).The ‘l‘ character in ‘hypotl’ stands for ‘long double’ which signify the argument typeand return type of the function.

Code example

#include <typeinfo>

/*hypotf */
cout<< “*hypotf\n”;
float f=2 , f1=4;
cout<< typeid( hypotf(2 , 4) ).name() << endl /*identify type of hypotf retunred value */
<< typeid( hypot(f , f1) ).name() << endl; /*identify type of hypot(float) returned value */

/*hypotl*/
cout<< “*hypotl\n”;
long double ld=2 , ld1=4;
cout<< typeid( hypotl(2 , 4) ).name() << endl /*indentify type of hypotl returned value */
<< typeid( hypot(ld , ld1) ).name() << endl; /*identify type of hypot(long double) returned value*/

Output

*hypotf
f
f
*hypotl
e (means ‘long double’)
e (means ‘long double’)

*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).