In C++, std::hypot(x, y) returns the length of the hypotenuse. That is the long side of a right triangle whose other two sides are x and y. It is the same as sqrt(x * x + y * y).
The advantage over writing the formula yourself is safety. If x * x is too big for a double, the formula overflows to inf, while hypot() still gives the right answer. In C++17 there is also a version with three arguments, for lengths in 3D.
std::hypot() is declared in <cmath> and is part of the group Roots and powers. These functions raise numbers to powers or take roots. Watch the domain: a negative number has no real square root, so the answer is NaN.
Syntax
Include <cmath> and call std::hypot. Start with these lines.
C++
double a = std::hypot(3.0, 4.0); // 5
float b = std::hypot(5.0f, 12.0f); // 13.0f
double c = std::hypot(2.0, 3.0, 6.0); // 7 (C++17)
Call
What it does
std::hypot(3.0, 4.0)
Two sides of a right triangle. Returns 5.
std::hypot(5.0f, 12.0f)
The float overload. Returns 13.0f.
std::hypot(2.0, 3.0, 6.0)
C++17. Length of a 3D vector. Returns 7.
Write std::hypot so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float hypot(float x, float y);
double hypot(double x, double y);
long double hypot(long double x, long double y);
// C++17: three arguments
double hypot(double x, double y, double z);
Parameters
Parameter
Description
x
Required. The length of the first side. Negative values are allowed, because they are squared.
y
Required. The length of the second side. A third value z can be added in C++17.
Return Value
Return Value
Description
Hypotenuse
The value sqrt(x*x + y*y), never negative.
inf
If either argument is infinity, even if the other is NaN.
NaN
If an argument is NaN and neither is infinity.
C++ Examples
Example 1 — Hypotenuse of Right Triangles
Find the long side for four triangles. The first two are well-known whole-number triangles.
Length of (2, 3, 6): 7
Length of (1, 2, 2): 3
Two sides, then z: 13
How It Works
2² + 3² + 6² is 4 + 9 + 36, which is 49. The root is 7.
1² + 2² + 2² is 9, so the length of (1, 2, 2) is 3.
The third line uses an older trick that works in any standard: hypot(3, 4) is 5, and hypot(5, 12) is 13.
Frequently Asked Questions
std::hypot(x, y) returns the square root of x squared plus y squared. It is the length of the long side (hypotenuse) of a right triangle with sides x and y.
The value sqrt(x*x + y*y) as float, double, or long double. If x or y is infinity, the result is inf, even if the other is NaN.
It is safer. hypot() avoids overflow and underflow in the middle of the calculation, and it is usually accurate to the last digit. For small, normal numbers both give the same result.
Yes, since C++17. std::hypot(x, y, z) gives the length of a vector in 3D. In older standards, call hypot twice: std::hypot(std::hypot(x, y), z).
Include <cmath> and write std::hypot(). That always works and picks the C++ overloads. The global name ::hypot often works too, but the standard does not promise it, and it may only take a double.