Open In App

sqrt() function for complex number in C++

Improve
Improve
Like Article
Like
Save
Share
Report

The complex version of sqrt() function is defined in the complex header file. This function is used to calculate the square root of the complex number z with a branch cut along the negative real axis.

Syntax:  

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

Parameters: This method takes a mandatory parameter z which represents the complex number whose square root is to be calculated.

Return Value: This function returns the square root of the complex number z.

Below program illustrate the sqrt() function for complex number in C++:

cpp




// C++ program to demonstrate
// example of sqrt() function.
 
#include <complex>
#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    cout << "Square root of -9 is ";
    cout << sqrt(complex<double>(-9.0, 0.0)) << endl;
 
    cout << "Square root of (-9, -0) is ";
    cout << sqrt(complex<double>(-9.0, -0.0)) << endl;
 
    return 0;
}


Output

Square root of -9 is (0,3)
Square root of (-9, -0) is (0,-3)

Time Complexity: O(√(a2+b2)) where a and b are the real and imaginary part of complex number.
Auxiliary Space: O(1)


Last Updated : 17 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads