Cray C/C++ Reference Manual | ||
---|---|---|
Prev Section | Chapter 3. Cray C and C++ Extensions | Next Section |
Note:: Complex data types are available in Cray Standard C only. Complex data types are supported in Cray C++ through the complex class library.
Cray Standard C provides float complex, double complex, and long double complex data types for use on all Cray systems. These data types are available only if the nonstandard header <complex.h> is included in your source. These data types are available even if you are compiling in strict conformance mode, as long as the <complex.h> header is included. Complex arithmetic can be performed in much the same way as with real data types (either integral or floating type). Complex variables can be declared and initialized. Most arithmetic operators can be used with one or two complex operands to yield a complex result. Many standard math functions have corresponding functions that take complex arguments and return complex values.
The complex data types are represented in memory with two contiguous parts, the real part and the imaginary part. The characteristics of the imaginary part agree with those of the corresponding real types. For example, the imaginary part of a float complex type has the same characteristics as a float.
An imaginary constant has the following form:
Ri |
R is either a floating-constant or an integer-constant; no space or other character can appear between R and i. If you are compiling in strict conformance mode (-h conform), imaginary constants are not available.
A complex variable is initialized by using an expression that may contain an imaginary constant. For example:
#include <complex.h> double complex z1 = 1.2 + 3.4i; double complex z2 = 5i; |
When the ++ operator is used to increment a complex variable, only the real part is incremented.
Printing a complex value requires the use of two %f specifications and any formatting needed for legibility must be specified as shown in the following example:
double complex z = 0.5 + 1.5i; printf("<.2f,%.2f>\n ", creal(z), cimag(z)); |
The output from the preceding example is as follows:
<0.50,1.50> |
A binary operator with one complex operand and one real operand causes the real operand to be promoted to a complex type before the operation is performed. When a real value is promoted to a complex value, the new complex value's real part gets the same value as if the promotion were to the corresponding floating type, and its imaginary part is zero. When a complex value is demoted to a real type, the value of its imaginary part is discarded, and its real part is demoted according to the demotion rules for the corresponding floating type.
A complex type is not valid as an operand for any operator that requires an integral data type. A complex type is not valid as an operand for any of the relational operators, but it is valid for the equality and inequality operators. It is valid as an operand for any other operator that allows floating data types.
Math functions that take complex arguments are declared in the complex.h header and described in the UNICOS System Libraries Reference Manual.
Adding complex types to the C language affects the usual arithmetic conversions as specified in the standard. See Section 3.8 for information on how complex types affect how the usual arithmetic conversions are performed on Cray machines.
Prev Section | Table of Contents | Title Page | Next Section |
// Comments | Up one level | Variable Length Arrays |