C++ Standard Library C++ STL Library

C++ <complex> - operator/= Function



The C++ complex::operator/= function supports compound assignment operator (divide AND assignment operator) of two complex numbers or a complex and a scalar.

Syntax

complex& operator/= (const T& val);
template<class X> complex& operator/= (const complex<X>& rhs);

Parameters

val Specify scalar value of matching type.
rhs Specify complex value of matching type.

Return Value

*this.

Example:

In the example below, the complex::operator/= function is used to perform divide and assignment operation on a given complex number.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<double> z1 (10, 20);
  complex<double> z2 (2, 3);
  double x = 0.5;
  
  //displaying z1
  cout<<"z1 : "<<z1<<"\n";
   
  //dividing z1 by z2
  z1 /= z2;

  //displaying z1
  cout<<"z1 : "<<z1<<"\n";  

  //dividing z1 by x
  z1 /= x;

  //displaying z1
  cout<<"z1 : "<<z1<<"\n";  

  return 0;
}

The output of the above code will be:

z1 : (10,20)
z1 : (6.15385,0.769231)
z1 : (12.3077,1.53846)

❮ C++ <complex> Library

5