hypot()
Anonymous contributor
Published Dec 5, 2022
Contribute to Docs
The hypot()
function returns the value of the longest side of a right-angled triangle, or the hypotenuse.
Syntax
hypot(x, y)
The hypotenuse is the sum of the squares of the two shorter sides of a right-angled triangle. The x
and y
parameters are the floating-point lengths of the other two sides.
For C++ implementations that support floating-point math:
- The
x
andy
parameters can be used in any order and with either sign (i.e.,hypot(±x, ±y)
,hypot(±y, ±x)
). - If
x
ory
is ±0, the return value is similar tofabs()
called with a non-zero argument. - If
x
ory
is ±∞, then +∞ is always returned (even if the other argument isNaN
). - Otherwise,
NaN
is returned ifx
ory
isNaN
.
Example
The following example uses the hypot()
function to find the hypotenuse of a right triangle with x
and y
values:
#include <cmath>#include <iostream>using namespace std;int main() {double x = 9, y = 10, result;result = hypot(x, y);cout << result << endl;return 0;}
This results in the following output:
13.4536
Codebyte Example
The following example is runnable and returns the square root of the sum of squares of two long double
values:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.