C++ std::hypot() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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)
CallWhat 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

ParameterDescription
xRequired. The length of the first side. Negative values are allowed, because they are squared.
yRequired. The length of the second side. A third value z can be added in C++17.

Return Value

Return ValueDescription
HypotenuseThe value sqrt(x*x + y*y), never negative.
infIf either argument is infinity, even if the other is NaN.
NaNIf 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.

C++
#include <cmath>
#include <iostream>

int main() {
    std::cout << "hypot(3, 4)  = " << std::hypot(3.0, 4.0) << '\n';
    std::cout << "hypot(5, 12) = " << std::hypot(5.0, 12.0) << '\n';
    std::cout << "hypot(1, 1)  = " << std::hypot(1.0, 1.0) << '\n';
    std::cout << "hypot(0, 7)  = " << std::hypot(0.0, 7.0) << '\n';
    return 0;
}

How It Works

  1. 3² + 4² is 25, and its root is 5. The triangle with sides 3, 4, 5 is the best known.
  2. 5² + 12² is 169, so hypot(5, 12) gives 13.
  3. hypot(1, 1) is the diagonal of a unit square: 1.41421. With a side of 0, the answer is just the other side, 7.

Example 2 — Why hypot() Is Safer Than the Formula

With very big numbers, squaring first overflows. hypot() does not have that problem.

C++
#include <cmath>
#include <iostream>

int main() {
    double x = 1e200;
    double y = 1e200;

    double naive = std::sqrt(x * x + y * y);
    double safe = std::hypot(x, y);

    std::cout << "sqrt(x*x + y*y) = " << naive << '\n';
    std::cout << "hypot(x, y)     = " << safe << '\n';
    return 0;
}

How It Works

  1. 1e200 * 1e200 is 1e400, which is bigger than the largest double (about 1.8e308). It overflows to inf before the root is taken.
  2. Because the middle step is inf, the formula prints inf, which is wrong. The true answer is about 1.4e200.
  3. hypot() scales the values internally, so no step overflows. It prints 1.41421e+200, the correct answer.

Example 3 — Length of a 3D Vector (C++17)

Since C++17, hypot() accepts three numbers. This gives the length of a vector in 3D space. Compile with -std=c++17.

C++
#include <cmath>
#include <iostream>

int main() {
    double length1 = std::hypot(2.0, 3.0, 6.0);
    double length2 = std::hypot(1.0, 2.0, 2.0);

    std::cout << "Length of (2, 3, 6): " << length1 << '\n';
    std::cout << "Length of (1, 2, 2): " << length2 << '\n';
    std::cout << "Two sides, then z:   " << std::hypot(std::hypot(3.0, 4.0), 12.0) << '\n';
    return 0;
}

How It Works

  1. 2² + 3² + 6² is 4 + 9 + 36, which is 49. The root is 7.
  2. 1² + 2² + 2² is 9, so the length of (1, 2, 2) is 3.
  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.

Next: ilogb()

Keep going through the <cmath> functions. The binary exponent of a number, as an int.

ilogb() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful