C++ Complex::polar() function



The C++ std::complex::polar() function is a utility for creating a complex number from a polar coordinates. It takes two arguments: one is magnitude and another is an angle(in radians). It returns the complex object representing the complex number corresponding to the specified polar coordinates.

Syntax

Following is the syntax for std::complex::polar() function.

polar (const T& rho, const T& theta = 0);

Parameters

  • rho It indicates the magnitude(modulus) of the complex number.
  • theta It indicates the phase angle(angular component) of the complex number.
  • T It is a type of the components of the complex type.

Return Value

It returns the complex object equivalent to the polar format formed by rho and theta.

Exceptions

none

Example 1

In the following example, we are going to comsider the basic usage of the polar() function.

#include <iostream>
#include <complex>
int main() {
   std::complex < double > x = std::polar(1.2, M_PI / 4);
   std::cout << "Result: " << x << std::endl;
   return 0;
}

Output

Output of the above code is as follows −

Result: (0.848528,0.848528)

Example 2

Consider the following example, where we are going to create a complex number with 60degrees.

#include <iostream>
#include <complex>
int main() {
   std::complex < double > x = std::polar(2.1, M_PI / 3);
   std::cout << "Result : " << x << std::endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

Result : (1.05,1.81865)

Example 3

Let's look at the following example, where we are going to create a complx number using negative angle.

#include <iostream>
#include <complex>
int main() {
   std::complex < double > x = std::polar(1.5, -M_PI / 6);
   std::cout << "Result: " << x << std::endl;
   return 0;
}

Output

Following is the output of the above code −

Result: (1.29904,-0.75)
complex.htm