How to use hypot() in C++
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 thehypotfunction.
Return value
- If two arguments are passed to the function, the return value equals .
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 . The return value is mathematically equal to the distance of a point from the origin in a
3Dgraph.
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
hypotfunction islong double, the return type islong double. If not, the return type isdouble. If both the arguments are of typefloat, the return type is alsofloat.
Examples
- The code below shows how the
hypotfunction 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 typelong 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 casecout << "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 casecout << "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;}
- The code below shows how the
hypotfunction 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 casecout << "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;}
Free Resources