Creating and using complex numbers, Declaring complex numbers, Accessing complex number values – HP Integrity NonStop H-Series User Manual

Page 236

Advertising
background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Creating and Using Complex Numbers

In the following sections we will describe the operations used to create and manipulate complex
numbers.

Declaring Complex Numbers

The template argument is used to define the types associated with the real and imaginary fields.
This argument must be one of the floating point number data types available in the C++ language,
either float, double, or long double.

There are several constructors associated with the class. A constructor with no arguments initializes
both the real and imaginary fields to zero. A constructor with a single argument initializes the real
field to the given value, and the imaginary value to zero. A constructor with two arguments
initializes both real and imaginary fields. Finally, a copy constructor can be used to initialize a
complex number with values derived from another complex number.

complex<double> com_one; // value 0 + 0i
complex<double> com_two(3.14); // value 3.14 + 0i
complex<double> com_three(1.5, 3.14) // value 1.5 + 3.14i
complex<double> com_four(com_two); // value is also 3.14 + 0i

A complex number can be assigned the value of another complex number. Since the one-argument
constructor is also used for a conversion operator, a complex number can also be assigned the value
of a real number. The real field is changed to the right hand side, while the imaginary field is set to
zero.

com_one = com_three; // becomes 1.5 + 3.14i
com_three = 2.17; // becomes 2.17 + 0i

The function polar() can be used to construct a complex number with the given magnitude and
phase angle.

com_four = polar(5.6, 1.8);

The conjugate of a complex number is formed using the function conj(). If a complex number
represents x + iy, then the conjugate is the value x-iy.

complex<double> com_five = conj(com_four);

Advertising
This manual is related to the following products: