C++ 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
xandyparameters can be used in any order and with either sign (i.e.,hypot(±x, ±y),hypot(±y, ±x)). - If
xoryis ±0, the return value is similar tofabs()called with a non-zero argument. - If
xoryis ±∞, then +∞ is always returned (even if the other argument isNaN). - Otherwise,
NaNis returned ifxoryisNaN.
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.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours