C++ Standard Library C++ STL Library

C++ <complex> - sqrt() Function



The C++ <complex> sqrt() function returns square root of a complex number z. It is a function on complex plane, and has one branch cut along the negative real axis.

Syntax

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

Parameters

z Specify the complex number.

Return Value

Returns square root of z.

Example:

The example below shows the usage of sqrt() function.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<double> z1 (2, 2);
  complex<double> z2 (2, 0);
  complex<double> z3 (0, 2);

  //calculate the exponential of given value
  cout<<"sqrt(z1): "<<sqrt(z1)<<"\n";
  cout<<"sqrt(z2): "<<sqrt(z2)<<"\n";
  cout<<"sqrt(z3): "<<sqrt(z3)<<"\n";

  return 0;
}

The output of the above code will be:

sqrt(z1): (1.55377,0.643594)
sqrt(z2): (1.41421,0)
sqrt(z3): (1,1)

❮ C++ <complex> Library