C++ Standard Library C++ STL Library

C++ <complex> - imag() Function



The C++ <complex> imag() function returns the imaginary part of a complex number. The additional overloads is added in C++11 to provide the arguments of any fundamental arithmetic type. In this case, the function assumes the value has a zero imaginary component, and thus simply returns z converted to the proper type.

Syntax

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

//additional overloads
double imag (ArithmeticType z);                       

Parameters

z Specify the complex number.

Return Value

Returns the imaginary part of the complex number.

Example:

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

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<int> z (5, 10);

  //display the imaginary part of 
  //the complex number
  cout<<"Imaginary part of z: "<<
      imag(z)<<"\n";

  return 0;
}

The output of the above code will be:

Imaginary part of z: 10

❮ C++ <complex> Library