Complex

A complex type specifier must include one of the following keywords:

See COMPLEX and DOUBLE COMPLEX (IBM extension) for details on declaring entities of type complex.

IBM extension begins The following table shows the corresponding values for the kind type parameter and the length specification when the complex type specifier has the COMPLEX keyword:

Kind Type Parameter i
COMPLEX(i)

Length Specification j
COMPLEX*j

4
8
16

8
16
32

In XL Fortran, the kind type parameter specifies the precision of each part of the complex entity, while the length specification specifies the length of the whole complex entity. IBM extension ends

The kind of a complex constant is determined by the kind of the constants in the real and imaginary parts.

The precision of DOUBLE COMPLEX values is twice that of default complex values.

Scalar values of type complex can be formed using complex constructors. The form of a complex constructor is:

Read syntax diagramSkip visual syntax diagram
>>-(----expression----,----expression----)---------------------><

A complex literal constant is a complex constructor where each expression is a pair of constant expressions. Variables and expressions can be used in each part of the complex constructor as an XL Fortran extension.

In Fortran 95 you are only allowed to use a single signed integer, or real literal constant in each part of the complex constructor. In Fortran 2003, you can also use a named constant.

Fortran 2008 begins
In Fortran 2008, you can use complex part designators to access the real or imaginary part of complex entities directly. The type of a complex part designator is real and its kind and shape are those of the designator that appears to the left of the complex part selector. A complex part selector is either %RE or %IM. %RE selects the real part of a complex entity and %IM selects the imaginary part of a complex entity. Here is the syntax for complex part designators where designator has to be of type complex:
Read syntax diagramSkip visual syntax diagram
>>-designator--%--+-IM-+---------------------------------------><
                  '-RE-'   

Complex part designators follow the rules for real data types. In addition, you can use complex part designators as variables in assignment statements; if x is of type complex, x%IM=0.0 sets the imaginary part of x to zero.

Fortran 2008 ends

If both parts of the literal constant are of type real, the kind type parameter of the literal constant is the kind parameter of the part with the greater precision, and the kind type parameter of the part with lower precision is converted to that of the other part.

If both parts are of type integer, they are each converted to type default real. If one part is of type integer and the other is of type real, the integer is converted to type real with the precision of the real part.

See COMPLEX and DOUBLE COMPLEX (IBM extension) for details on declaring entities of type complex.

Each part of a complex number has the same internal representation as a real number with the same kind type parameter.

Examples of complex constants
(3_2,-1.86)  ! Integer constant 3 is converted to default real
             ! for constant 3.0.
(45Q6,6D45)  ! The imaginary part is converted to extended
             ! precision 6.Q45.
(1+1,2+2)    ! Use of constant expressions. Both parts are
             ! converted to default real. 
Examples of complex part designators
COMPLEX :: x, y, z
print *, x%RE ! Prints the same value as REAL(x)
print *, y%IM ! Prints the same value as AIMAG(y)
z%IM = 0.0    ! Sets the imaginary part of z to zero