Arithmetic operations, Comparing complex values, Stream input and output – HP Integrity NonStop H-Series User Manual
Page 237: Norm and absolute value, Accessing complex number values
Accessing Complex Number Values
The member functions real() and imag() return the real and imaginary fields of a complex number,
respectively. These functions can also be invoked as ordinary functions with complex number
arguments.
// the following should be the same
cout << com_one.real() << "+" << com_one.imag() << "i" << endl;
cout << real(com_one) << "+" << imag(com_one) << "i" << endl;
Functions and Member Functions
Arithmetic Operations
The arithmetic operators +, -, *, and / can be used to perform addition, subtraction, multiplication
and division of complex numbers. All four work either with two complex numbers, or with a
complex number and a real value. Assignment operators are also defined for all four.
cout << com_one + com_two << endl;
cout << com_one - 3.14 << endl;
cout << 2.75 * com_two << endl;
com_one += com_three / 2.0;
The unary operators + and - can also be applied to complex numbers.
Comparing Complex Values
Two complex numbers can be compared for equality or inequality, using the operators == and !=.
Two values are equal if their corresponding fields are equal. Complex numbers are not
well-ordered, and thus cannot be compared using any other relational operator.
Stream Input and Output
Complex numbers can be written to an output stream, or read from an input stream, using the
normal stream I/O conventions. A value is written in parentheses, either as (u) or (u,v), depending
upon whether or not the imaginary value is zero. A value is read as a set of parentheses surrounding
two numeric values.
Norm and Absolute Value
The function norm() returns the norm of the complex number. This is the sum of the squares of the
real and imaginary parts. The function abs() returns the absolute value, which is the square root of
the norm. Note that both are ordinary functions that take the complex value as an argument, not
member functions.
cout << norm(com_two) << endl;
cout << abs(com_two) << endl;
The directed phase angle of a complex number is yielded by the function arg().