C++ hypot()

The hypot() function in C++ returns the square root of sum of square of arguments passed.

hypot() prototype

double hypot(double x, double y);
float hypot(float x, float y);
long double hypot(long double x, long double y);
Promoted pow(Type1 x, Type2 y);

double hypot(double x, double y, double x); // (since C++17)
float hypot(float x, float y, float z); // (since C++17)
long double hypot(long double x, long double y, long double z); // (since C++17)
Promoted pow(Type1 x, Type2 y, Type2 y); // (since C++17)

Since C++11, if any argument passed to hypot() is long double, the return type Promoted is long double. If not, the return type Promoted is double.


h = √(x2+y2

in mathematics is equivalent to

h = hypot(x, y);

in C++ Programming.


If three arguments are passed:

h = √(x2+y2+z2))

in mathematics is equivalent to

h = hypot(x, y);

in C++ Programming.


This function is defined in <cmath> header file.


hypot() Parameters

The hytpot() takes either 2 or 3 parameters of integral or floating-point type.


hypot() Return Value

The hypot() returns:

  • the hypotenuse of a right-angled triangle if two arguments are passed, i. e. √(x2+y2).
  • distance from the origin to to the (x, y, x) if three arguments are passed, i.e, √(x2+y2+z2).

Example 1: How hypot() works in C++?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double x = 2.1, y = 3.1, result;
	result = hypot(x, y);
	cout << "hypot(x, y) = " << result << endl;
	
	long double yLD, resultLD;
	x = 3.52;
	yLD = 5.232342323;
	
	// hypot() returns long double in this case
	resultLD = hypot(x, yLD);
	cout << "hypot(x, yLD) = " << resultLD;
	
	return 0;
}

When you run the program, the output will be:

hypot(x, y) = 3.74433
hypot(x, yLD) = 6.30617 

Example 2: hypot() with Three Arguments

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double x = 2.1, y = 3.1, z = 23.3, result;
	result = hypot(x, y, z);
	cout << "hypot(x, y, z) = " << result << endl;
		
	return 0;
}

Note: This program will only run in new compilers that supports C++17.