SysTutorials

Linux Manual Pages › Section 3

stdsqrtstdcomplex(3)

std::sqrt(std::complex) - std::sqrt(std::complex)

Synopsis

Defined in header<complex>
    template <class T>
    complex<T> sqrt(const complex<T> &z);

Computes the square root of the complex number z with a branch cut along the negative real axis.

Parameters

z - complex number to take the square root of

Return value

If no errors occur, returns the square root of z, in the range of the right half-plane, including the imaginary axis ([0; +∞) along the real axis and (−∞; +∞) along the imaginary axis.) Error handling and special values Errors are reported consistent with math_errhandling If the implementation supports IEEE floating-point arithmetic,

Notes

The semantics of this function are intended to be consistent with the C function csqrt. Defect reports 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

Example

// Run this code

#include <iostream>
#include <complex>
int main()
{
std::cout << "Square root of -4 is "
<< std::sqrt(std::complex<double>(-4, 0)) << ‘\n’
<< "Square root of (-4,-0), the other side of the cut, is "
<< std::sqrt(std::complex<double>(-4, -0.0)) << ‘\n’;
}

Output:

  Square root of -4 is (0,2)   Square root of (-4,-0), the other side of the cut, is (0,-2)

See also

                    complex power, one or both arguments may be a complex number pow(std::complex) (function template) sqrt sqrtf computes square root ( sqrtl √                     x)                     (function) (C++11) (C++11)                     applies the function std::sqrt to each element of valarray sqrt(std::valarray) (function template)