DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Complex arithmetic in C++

Complex variables and data initialization

A program using complex arithmetic will contain declarations of complex variables. For example:

   complex zz = complex(3,-5);
will declare zz to be complex and initialize it with a pair of values. The first value of the pair is taken as the real part of the Cartesian representation of a complex number and the second as the imaginary part. The constructor will convert the arguments to the proper type (double). Such initializations may be written more compactly. For example:
   complex zz(3,-5);
   complex c_name(-3.9,7);
   complex rpr(SQRT_2,root3);

A complex variable can be initialized to a real value by using the constructor with only one argument. For example:

   complex ra = complex(1);
will set up ra as a complex variable initialized to (1,0). Alternatively the initialization to a real value can also be written without explicit use of the constructor:
   complex rb = 123;
The integer value will be converted to the equivalent complex value exactly as if the constructor complex(123) had been used explicitly. However, no conversion of a complex into a double is defined, so
   double dd = complex(1,0);
is illegal and will cause a compile time error.

If there is no initialization in the declaration of a complex variable, then the variable is initialized to (0,0). For example:

   complex orig;
is equivalent to the declaration:
   complex orig = complex(0,0);
Naturally a complex variable can also be initialized by a complex expression. For example:
   complex cx(-0.5000000e+02,0.8660254e+02);
   complex cy = cx+log(cx);
It is also possible to declare arrays of complex numbers. For example:
   complex carray[30];
sets up an array of 30 complex numbers, all initialized to (0,0). Using the above declarations:
   complex carr[] = { cx, cy, carray[2], complex(1.1,2.2) };
sets up a complex array carr[ ] of four complex elements and initializes it with the members of the list. However, a struct style initialization cannot be used. For example:
   complex cwrong[] = {1.5, 3.3, 4.2, 4};
is illegal, because it makes unwarranted assumptions about the representation of complex numbers.
Next topic: input and output
Previous topic: Complex arithmetic in C++

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005