cpp / latest / numeric / math / hypot.html /

std::hypot, std::hypotf, std::hypotl

Defined in header <cmath>
float       hypot ( float x, float y );
float       hypotf( float x, float y );
(1) (since C++11)
double      hypot ( double x, double y );
(2) (since C++11)
long double hypot ( long double x, long double y );
long double hypotl( long double x, long double y );
(3) (since C++11)
Promoted    hypot ( Arithmetic1 x, Arithmetic2 y );
(4) (since C++11)
float       hypot ( float x, float y, float z );
(5) (since C++17)
double      hypot ( double x, double y, double z );
(6) (since C++17)
long double hypot ( long double x, long double y, long double z );
(7) (since C++17)
Promoted    hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z );
(8) (since C++17)
1-3) Computes the square root of the sum of the squares of x and y, without undue overflow or underflow at intermediate stages of the computation.
4) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by (1-3). If any argument has integral type, it is cast to double. If any other argument is long double, then the return type is long double, otherwise it is double.
5-7) Computes the square root of the sum of the squares of x, y, and z, without undue overflow or underflow at intermediate stages of the computation.
8) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by (5-7). If any argument has integral type, it is cast to double. If any other argument is long double, then the return type is long double, otherwise it is double.

The value computed by the two-argument version of this function is the length of the hypotenuse of a right-angled triangle with sides of length x and y, or the distance of the point (x,y) from the origin (0,0), or the magnitude of a complex number x+iy.

The value computed by the three-argument version of this function is the distance of the point (x,y,z) from the origin (0,0,0).

Parameters

x, y, z - values of floating-point or integral types

Return value

1-4) If no errors occur, the hypotenuse of a right-angled triangle, \(\scriptsize{\sqrt{x^2+y^2} }\) x2
+y2
, is returned.
5-8) If no errors occur, the distance from origin in 3D space, \(\scriptsize{\sqrt{x^2+y^2+z^2} }\) x2
+y2
+z2
, is returned.

If a range error due to overflow occurs, +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL is returned.

If a range error due to underflow occurs, the correct result (after rounding) is returned.

Error handling

Errors are reported as specified in math_errhandling.

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

  • hypot(x, y), hypot(y, x), and hypot(x, -y) are equivalent
  • if one of the arguments is ±0, hypot(x,y) is equivalent to fabs called with the non-zero argument
  • if one of the arguments is ±∞, hypot(x,y) returns +∞ even if the other argument is NaN
  • otherwise, if any of the arguments is NaN, NaN is returned

Notes

Implementations usually guarantee precision of less than 1 ulp (units in the last place): GNU, BSD.

std::hypot(x, y) is equivalent to std::abs(std::complex<double>(x,y)).

POSIX specifies that underflow may only occur when both arguments are subnormal and the correct result is also subnormal (this forbids naive implementations).

Distance between two points (x1,y1,z1) and (x2,y2,z2) on 3D space can be calculated using 3-argument overload of std::hypot as std::hypot(x2-x1, y2-y1, z2-z1).

Feature testing macro: __cpp_lib_hypot.

(since C++17)

Example

#include <iostream>
#include <cmath>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cstring>
 
// #pragma STDC FENV_ACCESS ON
int main()
{
    // typical usage
    std::cout << "(1,1) cartesian is (" << std::hypot(1,1)
              << ',' << std::atan2(1,1) << ") polar\n";
    struct Point3D { float x, y, z; } a{3.14,2.71,9.87}, b{1.14,5.71,3.87};
    // C++17 has 3-argumnet hypot overload:
    std::cout << "distance(a,b) = " << std::hypot(a.x-b.x,a.y-b.y,a.z-b.z) << '\n';
    // special values
    std::cout << "hypot(NAN,INFINITY) = " << std::hypot(NAN,INFINITY) << '\n';
    // error handling
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "hypot(DBL_MAX,DBL_MAX) = " << std::hypot(DBL_MAX,DBL_MAX) << '\n';
    if (errno == ERANGE)
        std::cout << "    errno = ERANGE " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

Output:

(1,1) cartesian is (1.41421,0.785398) polar
distance(a,b) = 7
hypot(NAN,INFINITY) = inf
hypot(DBL_MAX,DBL_MAX) = inf
    errno = ERANGE Numerical result out of range
    FE_OVERFLOW raised

See also

(C++11)(C++11)
raises a number to the given power (\(\small{x^y}\)xy)
(function)
(C++11)(C++11)
computes square root (\(\small{\sqrt{x} }\)x)
(function)
(C++11)(C++11)(C++11)
computes cubic root (\(\small{\sqrt[3]{x} }\)3x)
(function)
returns the magnitude of a complex number
(function template)
C documentation for hypot

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/numeric/math/hypot