The complex class defines a set of mathematical operators with the same precedence as the corresponding real operators. With these operators, you can code expressions on complex numbers. They are:
- operator + (addition)
- operator * (multiplication)
- operator - (negation)
- operator - (subtraction)
- operator / (division)
- operator += (assignment)
- operator -= (assignment)
- operator *= (assignment)
- operator /= (assignment)
- operator == (equality)
- operator != (inequality)
Assignment operators do not produce an lvalue. The complex mathematical assignment operators (+=, -=, *=, /=) do not produce a value that can be used in an expression.
The equality and inequality operators test for an exact equality between the real parts of two numbers, and between their complex parts. Because both components are double values, two numbers may be "equal" within a certain tolerance, but unequal as far as these operators are concerned. If you want an equality or inequality operator that can test for an absolute difference within a certain tolerance between the two pairs of corresponding components, you should define your own equality functions rather than use the equality and inequality operators of the complex class.
The complex mathematical assignment operators (+=, -=, *=, /=) do not produce a value that can be used in an expression. The following code, for example, produces a compile-time error:
complex x, y, z; //valid declaration
x - (y += z ) // invalid assignment causes
// a compile-time error