std::sqrt(std::complex)

< cpp‎ | numeric‎ | complex

C++
 Language Standard library headers Concepts Utilities library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11)

Numerics library
 Common mathematical functions Floating-point environment Complex numbers Numeric arrays Pseudo-random number generation Compile-time rational arithmetic (C++11) Generic numeric operations iota (C++11) accumulate inner_product adjacent_difference partial_sum

std::complex
Member functions
Non-member functions
 real imag abs arg norm conj proj (C++11) polar operator""ioperator""ifoperator""il (C++14)(C++14)(C++14)
Exponential functions
Power functions
sqrt
Trigonometric functions
 asin (C++11) acos (C++11) atan (C++11)
Hyperbolic functions
 asinh (C++11) acosh (C++11) atanh (C++11)

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

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

Contents

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.)

If the argument is a negative real number, the result lies on the positive imaginary axis.

Error handling and special values

Errors are reported consistent with math_errhandling

If the implementation supports IEEE floating-point arithmetic,

• The function is continuous onto the branch cut taking into account the sign of imaginary part
• std::sqrt(std::conj(z)) == std::conj(std::sqrt(z))
• If z is (±0,+0), the result is (+0,+0)
• If z is (x,+∞), the result is (+∞,+∞) even if x is NaN
• If z is (x,NaN), the result is (NaN,NaN) (unless x is ±∞) and FE_INVALID may be raised
• If z is (-∞,y), the result is (+0,+∞) for finite positive y
• If z is (+∞,y), the result is (+∞,+0) for finite positive y
• If z is (-∞,NaN), the result is (NaN,∞) (sign of imaginary part unspecified)
• If z is (+∞,NaN), the result is (+∞,NaN)
• If z is (NaN,y), the result is (NaN,NaN) and FE_INVALID may be raised
• If z is (NaN,NaN), the result is (NaN,NaN)

Example

#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)