hypot(), hypotf(), hypotl() in C++ (original) (raw)
The **hypot() function in C++ returns the square root of sum of square of arguments passed. It finds the hypotenuse, _hypotenuse is the longest side of a right angled triangle. It is calculated by the formula :
**h = sqrt(x 2 +y 2)
where x and y are the other two sides of the triangle. 
**Syntax:
**double hypot(double x, double y);
**float hypot(float x, float y);
**long double hypot(long double x, long double y);
**Time Complexity: O(1)
**Space Complexity: O(1)
**Examples:
Input : x=3, y=4
Output :5
Input :x=9, y=10
Output :13.4536
**Explanation
**Header File : cmath
**Parameters : The hypot() takes either 2 or 3 parameters of integral or floating-point type.
**Returns :
1. The hypotenuse of a right-angled triangle if two arguments are passed.
_2. Distance from the origin to the (x, y, x) if three arguments are passed
**Exceptions or Errors
1. hypot(x, y), hypot(y, x), and hypot(x, -y) are equivalent.
2. If one of the arguments is 0, hypot(x, y) is equivalent to **fabs called with the non-zero argument
3. If one of the arguments is _infinite or undefined, hypot(x, y) returns undefined.
**Example Application : Finding the hypotenuse of a right angle triangle given 2 of its other sides.
CPP `
// CPP program to illustrate // hypot() function #include #include using namespace std; // Driver Program int main() { double x = 9, y = 10, res; res = hypot(x, y);
// hypot() returns double in this case
cout << res << endl;
long double a, b, result;
a = 4.525252;
b = 5.767676;
// hypot() returns long double in this case
result = hypot(a, b);
cout << result;
return 0;}
`
Output:
13.4536
7.33103
**hypotf() function
hypotf() function is same as the hypot function.The only difference is that 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.
**Syntax
**float hypotf(float x);
**Time Complexity: O(1)
**Space Complexity: O(1)
C++ program implementation of hypotf() Here, variables are assigned float type otherwise _type mismatch error occurs.
CPP `
// CPP program to illustrate // hypotf() function #include #include using namespace std; // Driver Program int main() { float x = 9.3425, y = 10.0987, res;
// hypotf() takes float values and returns float
res = hypotf(x, y);
cout << res << endl;
return 0;}
`
Output:
13.7574
**hypotl() function
hypotl() function is same as the hypot function.The only difference is that 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.
**Syntax
**long double hypotl(long double x);
**Time Complexity: O(1)
**Space Complexity: O(1)
C++ program implementation of hypotl() Here, variables are assigned long double type otherwise _type mismatch error occurs.
CPP `
// CPP program to illustrate // hypotl() function #include #include using namespace std; // Driver Program int main() { long double x = 9.3425453435, y = 10.0987456456, res;
// hypotl() takes long double values and returns long double
res = hypotl(x, y);
cout << res << endl;
return 0;}
`