C++ Standard Library C++ STL Library

C++ <complex> - polar() Function



The C++ <complex> polar() function returns a complex number (in rectangular co-ordinates) corresponding to the complex number defined in polar co-ordinates with specified magnitude r and phase angle theta.

The function returns (real, imag), where real = r * cos(theta) and imag = r * sin(theta).

The behavior is undefined if r is negative or NaN, or if theta is infinite.

Syntax

template<class T> 
  complex<T> polar (const T& r, const T& theta = 0);

Parameters

r Specify magnitude of the complex number.
theta Specify phase angle of the complex number.

Return Value

Returns a complex number determined by r and theta.

Example:

The example below shows the usage of <complex> polar() function.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){

  double pi = 2 * acos(0.0);

  //constructing complex value
  //from polar form
  cout<<"polar(5.0, pi/6): "<<polar(5.0, pi/6)<<"\n";
  cout<<"polar(5.0, pi/3): "<<polar(5.0, pi/3)<<"\n"; 
  cout<<"polar(5.0, pi/2): "<<polar(5.0, pi/2)<<"\n";

  return 0;
}

The output of the above code will be:

polar(5.0, pi/6): (4.33013,2.5)
polar(5.0, pi/3): (2.5,4.33013)
polar(5.0, pi/2): (3.06162e-16,5)

❮ C++ <complex> Library