Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
Functions | ||||||||||||||||
Basic operations | ||||||||||||||||
| ||||||||||||||||
Exponential functions | ||||||||||||||||
Power functions | ||||||||||||||||
| ||||||||||||||||
Trigonometric and hyperbolic functions | ||||||||||||||||
Error and gamma functions | ||||||||||||||||
Nearest integer floating point operations | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
Floating point manipulation functions | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
Classification and comparison | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
Types | |||||||||||||||||||||||||||||||||||||||||
Macro constants | |||||||||||||||||||||||||||||||||||||||||
|
|
Defined in header <cmath> | ||
(1) | ||
float hypot(float x,float y); double hypot(double x,double y); | (since C++11) (until C++23) | |
/*floating-point-type*/ hypot(/*floating-point-type*/ x, | (since C++23) (constexpr since C++26) | |
float hypotf(float x,float y); | (2) | (since C++11) (constexpr since C++26) |
longdouble hypotl(longdouble x,longdouble y); | (3) | (since C++11) (constexpr since C++26) |
(4) | ||
float hypot(float x,float y,float z); double hypot(double x,double y,double z); | (since C++17) (until C++23) | |
/*floating-point-type*/ hypot(/*floating-point-type*/ x, | (since C++23) (constexpr since C++26) | |
Defined in header <cmath> | ||
template<class Arithmetic1, Arithmetic2> /*common-floating-point-type*/ | (A) | (since C++11) (constexpr since C++26) |
template<class Arithmetic1, Arithmetic2, Arithmetic3> /*common-floating-point-type*/ | (B) | (since C++17) |
std::hypot
for all cv-unqualified floating-point types as the type of the parametersx andy.(since C++23)std::hypot
for all cv-unqualified floating-point types as the type of the parametersx,y andz.(since C++23)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 lengthx andy, or the distance of the point(x,y)
from the origin(0,0)
, or the magnitude of a complex numberx+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)
.
Contents |
x, y, z | - | floating-point or integer values |
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.
Errors are reported as specified inmath_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
Implementations usually guarantee precision of less than 1ulp (Unit in the Last Place — Unit of Least Precision):GNU,BSD.
std::hypot(x, y) is equivalent tostd::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 | (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 argumentnum1, second argumentnum2 and the optional third argumentnum3:
| (until C++23) |
Ifnum1,num2 andnum3 have arithmetic types, then
where/*common-floating-point-type*/ is the floating-point type with the greatestfloating-point conversion rank and greatestfloating-point conversion subrank among the types ofnum1,num2 andnum3, arguments of integer type are considered to have the same floating-point conversion rank asdouble. If no such floating-point type with the greatest rank and subrank exists, thenoverload resolution does not result in a usable candidate from the overloads provided. | (since C++23) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_hypot | 201603L | (C++17) | 3-argument overload ofstd::hypot (4,B) |
#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 usagestd::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 valuesstd::cout<<"hypot(NAN,INFINITY) = "<< std::hypot(NAN,INFINITY)<<'\n'; // error handlingerrno=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) polardistance(a,b) = 7hypot(NAN,INFINITY) = infhypot(DBL_MAX,DBL_MAX) = inf errno = ERANGE Numerical result out of range FE_OVERFLOW raised
(C++11)(C++11) | raises a number to the given power (\(\small{x^y}\)xy) (function)[edit] |
(C++11)(C++11) | computes square root (\(\small{\sqrt{x}}\)√x) (function)[edit] |
(C++11)(C++11)(C++11) | computes cube root (\(\small{\sqrt[3]{x}}\)3√x) (function)[edit] |
returns the magnitude of a complex number (function template)[edit] | |
C documentation forhypot |