Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

cplxops Operators


This section describes the basic input/output, arithmetic, comparison, and assignment operators.


#include <complex.h>

class complex

{

public:

friend complex
friend complex
friend complex
friend complex
friend complex

friend int
friend int

void
void
void
void

operator+(complex, complex);
operator-(complex);
operator-(complex, complex);
operator*(complex, complex);
operator/(complex, complex);

operator==(complex, complex);
operator!=(complex, complex);

operator+=(complex);
operator-=(complex);
operator*=(complex);
operator/=(complex);

};

ostream&
istream&

operator<<(ostream&, complex);
operator>>(istream&, complex&);





The operators have their conventional precedences. In the following descriptions for operators, x, y, and z are variables of class complex.

Arithmetic operators:

z = x + y

Returns a complex which is the arithmetic sum of complex numbers x and y.

z = -x

Returns a complex which is the arithmetic negation of complex number x.

z = x - y

Returns a complex which is the arithmetic difference of complex numbers x and y.

z = x * y

Returns a complex which is the arithmetic product of complex numbers x and y.

z = x / y

Returns a complex which is the arithmetic quotient of complex numbers x and y.

Comparison operators:

x == y

Returns a non-zero integer if complex number x is equal to complex number y;
returns 0 otherwise.

x != y

Returns a non-zero integer if complex number x is not equal to complex number y;
returns 0 otherwise.

Assignment operators:

x += y

Complex number x is assigned the value of the arithmetic sum of itself and complexnumber y.

x -= y

Complex number x is assigned the value of the arithmetic difference of itself and complex number y.

x *= y

Complex number x is assigned the value of the arithmetic product of itself and complex number y.

x /= y

Complex number x is assigned the value of the arithmetic quotient of itself and complex number y.

Warning

The assignment operators do not produce a value that can be used in an expression.In other words, the following construction is syntactically invalid:

complex x, y, z;

x = (y += z);

The following lines, by contrast:

x = (y + z);

x = (y == z);

are valid.

Input/output operators:

Output and input of complex numbers may be performed using the << and >> operators, respectively.

Output format:

( real, imag)

Input format:

Input            corresponds to       complex number

-----------------------------------------------------
Zahl                   |              (Zahl, 0)
(Zahl)                 |              (Zahl, 0)
(Zahl1, Zahl2)         |              (Zahl1, Zahl2)


EXAMPLE

The following program defines the complex numbers c and d, divides d by c, and then prints the values of c and d:
#include <iostream.h>
#include <complex.h>
main()
{
  complex c,d;
  d = complex(10.0, 11.0);
  c = complex (2.0, 2.0);
  while (norm(c) < norm(d))
  {
    d /= c;
    cout << c << " " <<d << "\n";
  }
  return 0;
}

The result of executing the program is:

( 2, 2)  ( 5.25, 0.25)
( 2, 2)  ( 1.375, -1.25)
%  CCM0998 CPU time used: 0.0009 seconds

SEE ALSO


cplxcartpol, cplxerrcplxexpcplxtrig