C++ Complex::arg() function



The C++ std::complex::arg() function is used to return the phase angle of a complex number, returning it as a value in radians. It represents the angle between the positive real axis and the line formed by the origin and the complex number in the complex plane.

The result is expressed in radians and ranges from -22/7(-pi) to +22/7(+pi).

Syntax

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

arg (const complex& x);	
double arg (ArithmeticType x);

Parameters

  • x − It indicates the complex value.

Return Value

It returns the phase angle of the complex number x.

Exceptions

none

Example 1

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

#include 
#include 
int main() {
   std::complex < double > x(1.0, 1.2);
   std::cout << "Result : " << std::arg(x) << " radians" << std::endl;
   return 0;
}

Output

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

Result : 0.876058 radians

Example 2

Consider the following example, where we are going to take argument of negative complex number.

#include 
#include 
int main() {
   std::complex < double > a(-2.1, -1.2);
   std::cout << "Result : " << std::arg(a) << " radians" << std::endl;
   return 0;
}

Output

Following is the output of the above code −

Result : -2.62245 radians

Example 3

Let's look at the following example, where we are going to consider the argument of real complex part.

#include 
#include 
int main() {
   std::complex < double > a(3.0, 0.0);
   std::cout << "Result : " << std::arg(a) << " radians" << std::endl;
   return 0;
}

Output

Output of the above code is as follows −

Result : 0 radians
complex.htm
Advertisements