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 | |||||||||||||||||||||||||||||||
|
Defined in header <complex> | ||
template<class T> std::complex<T> sqrt(conststd::complex<T>& z); | ||
Computes the square root of the complex numberz with a branch cut along the negative real axis.
Contents |
z | - | complex number to take the square root of |
If no errors occur, returns the square root ofz, in the range of the right half-plane, including the imaginary axis ([0; +∞) along the real axis and(−∞; +∞) along the imaginary axis).
Errors are reported consistent withmath_errhandling.
If the implementation supports IEEE floating-point arithmetic,
(±0,+0)
, the result is(+0,+0)
(x,+∞)
, the result is(+∞,+∞)
even if x is NaN(x,NaN)
, the result is(NaN,NaN)
(unless x is ±∞) andFE_INVALID may be raised(-∞,y)
, the result is(+0,+∞)
for finite positive y(+∞,y)
, the result is(+∞,+0)
for finite positive y(-∞,NaN)
, the result is(NaN,∞)
(sign of imaginary part unspecified)(+∞,NaN)
, the result is(+∞,NaN)
(NaN,y)
, the result is(NaN,NaN)
andFE_INVALID may be raised(NaN,NaN)
, the result is(NaN,NaN)
The semantics of this function are intended to be consistent with the C functioncsqrt.
#include <complex>#include <iostream> int main(){std::cout<<"Square root of -4 is "<<std::sqrt(std::complex<double>(-4.0,0.0))<<'\n'<<"Square root of (-4,-0) is "<<std::sqrt(std::complex<double>(-4.0,-0.0))<<" (the other side of the cut)\n";}
Output:
Square root of -4 is (0,2)Square root of (-4,-0) is (0,-2) (the other side of the cut)
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2597 | C++98 | specification mishandles signed zero imaginary parts | erroneous requirement removed |
complex power, one or both arguments may be a complex number (function template)[edit] | |
(C++11)(C++11) | computes square root (\(\small{\sqrt{x}}\)√x) (function)[edit] |
applies the functionstd::sqrt to each element of valarray (function template)[edit] | |
C documentation forcsqrt |