std::hypot,...std::hypotl(3)
Hey bestie π welcome to the std::hypot,...std::hypotl(3) page β thatβs the std::hypot, std::hypotf, std::hypotl entry from the C++ Standard Library manual. Weβre doing hypotenuse math, but make it cute. Every technical detail is still here, no cap. β¨
πΈ NAME πΈ
std::hypot, std::hypotf, std::hypotl β and the original name line is literally std::hypot,std::hypotf,std::hypotl - std::hypot,std::hypotf,std::hypotl. Thatβs the whole vibe. π
π¦ Synopsis π¦
Defined in header <cmath>:
float hypot ( float x, float y );
double hypot ( double x, double y );
long double hypot ( long double x, long double y );
/* floating-point-type */
hypot ( /* floating-point-type */ x,
/* floating-point-type */ y );
float hypotf( float x, float y );
long double hypotl( long double x, long double y );
float hypot ( float x, float y, float z );
double hypot ( double x, double y, double z );
long double hypot ( long double x, long double y, long double z );
/* floating-point-type */
hypot ( /* floating-point-type */ x,
/* floating-point-type */ y,
/* floating-point-type */ z );
Labels youβll see in the house: (1), (2), (3), (4), (A), (B). π
Additional overloads π
Defined in header <cmath>:
template< class Arithmetic1, Arithmetic2 >
/* common-floating-point-type */
hypot ( Arithmetic1 x, Arithmetic2 y );
template< class Arithmetic1, Arithmetic2, Arithmetic3 >
/* common-floating-point-type */
hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z );
Version tea, bestie:
double hypot ( double x, double y );β(since C++11),(until C++23)./* floating-point-type */ hypot ( /* floating-point-type */ x, /* floating-point-type */ y );β(since C++23),(constexpr since C++26).float hypotf( float x, float y );β(since C++11), label(2),(constexpr since C++26).long double hypotl( long double x, long double y );β(since C++11), label(3),(constexpr since C++26).double hypot ( double x, double y, double z );β(since C++17),(until C++23)./* floating-point-type */ hypot ( /* floating-point-type */ x, /* floating-point-type */ y, /* floating-point-type */ z );β(since C++23),(constexpr since C++26), label(4).- Additional overload
(A)β(since C++11),(constexpr since C++26). - Additional overload
(B)β(since C++17),(constexpr since C++26).
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. The library provides overloads of std::hypot for all cv-unqualified floating-point types as the type of the parameters x and y. (since C++23)
4) 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. The library provides overloads of std::hypot for all cv-unqualified floating-point types as the type of the parameters x, y and z. (since C++23)
A,B) Additional overloads are provided for all other combinations of arithmetic types.
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β floating-point or integer values.
π₯ Return value π₯
1-3,A) If no errors occur, the hypotenuse of a right-angled triangle, \(\scriptsize{\sqrt{x^2+y^2} }\) (the page also renders it as β x2 +y2), is returned.
4,B) If no errors occur, the distance from origin in 3D space, \(\scriptsize{\sqrt{x^2+y^2+z^2} }\) (the page also renders it as β 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):
std::hypot(x, y),std::hypot(y, x), andstd::hypot(x, -y)are equivalent.- if one of the arguments is
Β±0,std::hypot(x, y)is equivalent tostd::fabscalled with the non-zero argument. - if one of the arguments is
Β±β,std::hypot(x, y)returns+βeven if the other argument isNaN. - otherwise, if any of the arguments is
NaN,NaNis returned.
π§ Notes π§
Implementations usually guarantee precision of less than 1 ulp (Unit in the Last Place β Unit of Least Precision): 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). (since C++17)
The additional overloads are not required to be provided exactly as (A,B). They only need to be sufficient to ensure that for their first argument num1, second argument num2 and the optional third argument num3:
- If
num1,num2ornum3has typelong double, thenstd::hypot(num1, num2)has the same effect asstd::hypot(static_cast<long double>(num1), static_cast<long double>(num2)), andstd::hypot(num1, num2, num3)has the same effect asstd::hypot(static_cast<long double>(num1), static_cast<long double>(num2), static_cast<long double>(num3)).
- Otherwise, if
num1,num2and/ornum3has typedoubleor an integer type, then(until C++23)std::hypot(num1, num2)has the same effect asstd::hypot(static_cast<double>(num1), static_cast<double>(num2)), andstd::hypot(num1, num2, num3)has the same effect asstd::hypot(static_cast<double>(num1), static_cast<double>(num2), static_cast<double>(num3)).
- Otherwise, if
num1,num2ornum3has typefloat, thenstd::hypot(num1, num2)has the same effect asstd::hypot(static_cast<float>(num1), static_cast<float>(num2)), andstd::hypot(num1, num2, num3)has the same effect asstd::hypot(static_cast<float>(num1), static_cast<float>(num2), static_cast<float>(num3)).
(since C++23)Ifnum1,num2andnum3have arithmetic types, thenstd::hypot(num1, num2)has the same effect asstd::hypot(static_cast</* common-floating-point-type */>(num1), static_cast</* common-floating-point-type */>(num2)), andstd::hypot(num1, num2, num3)has the same effect asstd::hypot(static_cast</* common-floating-point-type */>(num1), static_cast</* common-floating-point-type */>(num2), static_cast</* common-floating-point-type */>(num3)).
where /* common-floating-point-type */ is the floating-point type with the greatest floating-point conversion rank and greatest floating-point conversion subrank among the types of num1, num2 and num3, arguments of integer type are considered to have the same floating-point conversion rank as double.
If no such floating-point type with the greatest rank and subrank exists, then overload resolution does not result in a usable candidate from the overloads provided.
Feature-test macro vibe check:
__cpp_lib_hypotβ value201603L, std(C++17), feature: 3-argument overload ofstd::hypot.
π Example π
// Run this code
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
struct Point3D { float x, y, z; };
int main()
{
// typical usage
std::cout << "(1,1) cartesian is (" << std::hypot(1,1)
<< ',' << std::atan2(1,1) << ") polar\n";
Point3D a{3.14, 2.71, 9.87}, b{1.14, 5.71, 3.87};
// C++17 has 3-argument 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 β
pow,powf(C++11),powl(C++11)β raises a number to the given power (\(\small{x^y}\), i.e.x^y) (function).sqrt,sqrtf(C++11),sqrtl(C++11)β computes square root (\(\small{\sqrt{x}}\), i.e.βx) (function).cbrt,cbrtf(C++11),cbrtl(C++11)β computes cube root (\(\small{\sqrt[3]{x}}\), i.e.3βx) (function).abs(std::complex)β returns the magnitude of a complex number (function template).- C documentation for
hypot.
Thatβs the whole page, bestie π«Ά source/version: http://cppreference.com, 2024.06.10; footer page: std::hypot,...std::hypotl(3). Byeee πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms