next up previous contents
Next: The Overloaded Operator +: Up: The Definition File mycomplex.c: Previous: The Overloaded Operator /:

The Overloaded Operator / =:

Following our remarks on the * = implementation, we only need the following functions:

//====================================================//
//                                                    //
//                Overloaded /=                       //
//                                                    //
//====================================================//
MYCOMPLEX& MYCOMPLEX::operator/=(const MYCOMPLEX &w)
{
  if(modulus(w)<1.0e-14){
    cout << "Can't divide by 0!" << endl;
    return *this;
    }
  else{
    double c = modulus(w);
    c *=c;
    double r = (real*w.real + imaginary*w.imaginary)/c;
    double imag = (imaginary*w.real - real*w.imaginary)/c;
    this->real = r; this->imaginary = imag;
    return *this;
    }
}

MYCOMPLEX& operator/=(MYCOMPLEX& w,const int& i)
{
  if(i==0){
    cout << "Can't divide by 0!" << endl;
    return w;
    }
  else{
    w.real = w.real/(double)i;
    w.imaginary = w.imaginary/(double)i;
    return w;
    }
}
MYCOMPLEX& operator/=(MYCOMPLEX& w,const float& x)
{
  if(fabs(x)<1.0e-7){
    cout << "Can't divide by 0!" << endl;
    return w;
    }
  else{
    w.real = w.real/(double)x;
    w.imaginary = w.imaginary/(double)x;
    return w;
    }
}
MYCOMPLEX& operator/=(MYCOMPLEX& w,const double& x)
{
  if(fabs(x)<1.0e-14){
    cout << "Can't divide by 0!" << endl;
    return w;
    }
  else{
    w.real = w.real/x;
    w.imaginary = w.imaginary/x;
    return w;
    }
}



Jim Peterson
1999-04-22