Related Tags

c++

# How to use hypot() in C++ Sadia Zubair

The hypot function computes the square root of the sum of the square of arguments passed.

To use the hypot function, the cmath header file needs to be included in the program:

#include <cmath>


## Parameters

The hypot  function can either take two or three parameters.

The option of passing three parameters is provided since C++17. If you have a C++ version below 17, you will not pass three parameters in the hypot function.

## Return value

• If two arguments are passed to the function, the return value equals $\sqrt{x^{2}+y^{2}}$.

The return value is mathematically equal to the hypotenuse of a right-angled triangle.

• If three arguments are passed to the function, the return value equals $\sqrt{x^{2}+y^{2}+z^{2}}$. The return value is mathematically equal to the distance of a point from the origin in a 3D graph.

The hypot function can have the return value of three types:

• float
• double
• long double

These types depend on the type of arguments the programmer gives to the functions.

If any argument passed to the hypot function is long double, the return type is long double. If not, the return type is double. If both the arguments are of type float, the return type is also float.

Figure 1 : the possible return types when two arguments are passed
Figure 2 : the possible return types when three arguments are passed

## Examples

1. The code below shows how the hypot function functions when two arguments are passed:
• In line 13, both the arguments passed to the function are of the same type (i.e., double), which results in the returned value being the same (i.e., double) as that of the argument types.
• In line 17, the first argument passed to the function is type long double, which results in the returned value also being of type long double.
#include <iostream>
#include <cmath>
#include <typeinfo>

using namespace std;

int main()
{
double x = 2.1;
double y = 3.1;

// The function returns double in this case
cout << "hypot(x, y) = " << hypot(x, y) << endl;
// the type of returned value is output d which means returned value is of type double.
cout << "The type of returned value is " << typeid(hypot(x, y)).name() << endl;

double x1 = 2.1;
long double y1 = 3.1;

// The functions returns long double in this case
cout << "hypot(x, y) = " << hypot(x1, y1) << endl;
// the type of returned value is output e which means returned value is of type long double.
cout << "The type of returned value is " << typeid(hypot(x1, y1)).name() << endl;

return 0;
}
1. The code below shows how the hypot function functions when three arguments are passed:
• In line 12, all the arguments passed to the function are of the same type (i.e., double), which results in the returned value being the same (i.e., double) as that of the argument types.

This program will only compile if the compiler supports C++17.

#include <iostream>
#include <cmath>
#include <typeinfo>

using namespace std;

int main()
{
double x = 2.1;
double y = 13.1;
double z = 23.3;

// The function returns double in this case
cout << "hypot(x, y, z) = " << hypot(x, y, z) << endl;
// the type of returned value is output d which means returned value is of type double.
cout << "The type of returned value is " << typeid(hypot(x, y, z)).name() << endl;
return 0;
}

RELATED TAGS

c++

CONTRIBUTOR Sadia Zubair 