XL Fortran for AIX 8.1

Language Reference


Operators and Expressions

This section presents the expression levels in the order of evaluation precedence, from least to most.

General

The general form of an expression (general_expr) is:



>>-+---------------------------------+--expr-------------------><
   '-general_expr--defined_binary_op-'
 
 

defined_binary_op
is a defined binary operator. See Extended Intrinsic and Defined Operations.

expr
is one of the kinds of expressions defined below.

There are four kinds of intrinsic expressions: arithmetic, character, relational, and logical.

Arithmetic

An arithmetic expression (arith_expr), when evaluated, produces a numeric value. The form of arith_expr is:



>>-+-------------------------+--arith_term---------------------><
   '-+------------+--+- + -+-'
     '-arith_expr-'  '- - -'
 
 

The form of arith_term is:



>>-+---------------------+--arith_factor-----------------------><
   '-arith_term--+- / -+-'
                 '- * -'
 
 

The form of arith_factor is:



>>-arith_primary--+--------------------+-----------------------><
                  '- ** --arith_factor-'
 
 

An arith_primary is a primary of arithmetic type.

The following table shows the available arithmetic operators and the precedence each takes within an arithmetic expression.


Arithmetic Operator Representation Precedence
** Exponentiation First
* Multiplication Second
/ Division Second
+ Addition or identity Third
- Subtraction or negation Third

XL Fortran evaluates the terms from left to right when evaluating an arithmetic expression containing two or more addition or subtraction operators. For example, 2+3+4 is evaluated as (2+3)+4, although a processor can interpret the expression in another way if it is mathematically equivalent and respects any parentheses.

The factors are evaluated from left to right when evaluating a term containing two or more multiplication or division operators. For example, 2*3*4 is evaluated as (2*3)*4.

The primaries are combined from right to left when evaluating a factor containing two or more exponentiation operators. For example, 2**3**4 is evaluated as 2**(3**4). (Again, mathematical equivalents are allowed.)

The precedence of the operators determines the order of evaluation when XL Fortran is evaluating an arithmetic expression containing two or more operators having different precedence. For example, in the expression -A**3, the exponentiation operator (**) has precedence over the negation operator (-). Therefore, the operands of the exponentiation operator are combined to form an expression that is used as the operand of the negation operator. Thus, -A**3 is evaluated as -(A**3).

Note that expressions containing two consecutive arithmetic operators, such as A**-B or A*-B, are not allowed. You can use expressions such as A**(-B) and A*(-B).

If an expression specifies the division of an integer by an integer, the result is rounded to an integer closer to zero. For example, (-7)/3 has the value -2.

+-------------------------------IBM Extension--------------------------------+

For details of exception conditions that can arise during evaluation of floating-point expressions, see "Detecting and Trapping Floating-Point Exceptions" in the User's Guide .

+----------------------------End of IBM Extension----------------------------+

Examples of Arithmetic Expressions


Arithmetic Expression Fully Parenthesized Equivalent
-b**2/2.0 -((b**2)/2.0)
i**j**2 i**(j**2)
a/b**2 - c (a/(b**2)) - c

Data Type of an Arithmetic Expression

Because the identity and negation operators operate on a single operand, the type of the resulting value is the same as the type of the operand.

The following table indicates the resulting type when an arithmetic operator acts on a pair of operands.

Notation: T(param), where T is the data type (I: integer, R: real, X: complex) and param is the kind type parameter.

Table 3. Result Types for Binary Arithmetic Operators


second operand
first operand
I(1) I(2) I(4) I(8) R(4) R(8) R(16) X(4) X(8) X(16)
I(1) I(1) I(2) I(4) I(8) R(4) R(8) R(16) X(4) X(8) X(16)
I(2) I(2) I(2) I(4) I(8) R(4) R(8) R(16) X(4) X(8) X(16)
I(4) I(4) I(4) I(4) I(8) R(4) R(8) R(16) X(4) X(8) X(16)
I(8) I(8) I(8) I(8) I(8) R(4) R(8) R(16) X(4) X(8) X(16)
R(4) R(4) R(4) R(4) R(4) R(4) R(8) R(16) X(4) X(8) X(16)
R(8) R(8) R(8) R(8) R(8) R(8) R(8) R(16) X(8) X(8) X(16)
R(16) R(16) R(16) R(16) R(16) R(16) R(16) R(16) X(16) X(16) X(16)
X(4) X(4) X(4) X(4) X(4) X(4) X(8) X(16) X(4) X(8) X(16)
X(8) X(8) X(8) X(8) X(8) X(8) X(8) X(16) X(8) X(8) X(16)
X(16) X(16) X(16) X(16) X(16) X(16) X(16) X(16) X(16) X(16) X(16)

+-------------------------------IBM Extension--------------------------------+

Notes:

  1. XL Fortran implements REAL(4) operations using REAL(8) internal precision. See "Detecting and Trapping Floating-Point Exceptions" in the User's Guide for details on modifying this implementation. REAL(16) values must only be used in rounding to the nearest mode. The rounding mode can only be changed at the beginning and end of a subprogram. It cannot be changed across a subprogram call, and if it is changed within a subprogram, it must be restored before control is returned to the calling routine.
  2. XL Fortran implements integer operations using INTEGER(4) arithmetic, or INTEGER(8) arithmetic if data items are 8 bytes in length. If the intermediate result is used in a context requiring INTEGER(1) or INTEGER(2) data type, it is converted as required.
         INTEGER(2) I2_1, I2_2, I2_RESULT
         INTEGER(4) I4
         I2_1 = 32767             ! Maximum I(2)
         I2_2 = 32767             ! Maximum I(2)
         I4 = I2_1 + I2_2
         PRINT *, "I4=", I4       ! Prints "I4=65534"
     
         I2_RESULT = I2_1 + I2_2  ! Assignment to I(2) variable
         I4 = I2_RESULT           ! and then assigned to an I(4)
         PRINT *, "I4=", I4       ! Prints "I4=-2"
         END
    

+----------------------------End of IBM Extension----------------------------+

Character

A character expression, when evaluated, produces a result of type character. The form of char_expr is:



>>-+---------------+--char_primary-----------------------------><
   '-char_expr--//-'
 
 

char_primary is a primary of type character. All character primaries in the expression must have the same kind type parameter, which is also the kind type parameter of the result.

The only character operator is //, representing concatenation.

In a character expression containing one or more concatenation operators, the primaries are joined to form one string whose length is equal to the sum of the lengths of the individual primaries. For example, 'AB'//'CD'//'EF' evaluates to 'ABCDEF', a string 6 characters in length.

Parentheses have no effect on the value of a character expression.

A character expression can involve concatenation of an operand whose length was declared with an asterisk in parentheses (indicating inherited length), if the inherited-length character string is used to declare:

Example of a Character Expression

   CHARACTER(7)  FIRSTNAME,LASTNAME
   FIRSTNAME='Martha'
   LASTNAME='Edwards'
   PRINT *, LASTNAME//', '//FIRSTNAME       ! Output:'Edwards, Martha'
   END

Relational

A relational expression (rel_expr), when evaluated, produces a result of type logical, and can appear wherever a logical expression can appear. It can be an arithmetic relational expression or a character relational expression.

Arithmetic Relational Expressions

An arithmetic relational expression compares the values of two arithmetic expressions. Its form is:



>>-arith_expr1--relational_operator--arith_expr2---------------><
 
 

arith_expr1  and  arith_expr2
are each an arithmetic expression. Complex expressions can only be specified if relational_operator is .EQ., .NE., <>, ==, or /=.

relational_operator
is any of:

Relational Operator Representing
.LT. or < Less than
.LE. or <= Less than or equal to
.EQ. or == Equal to
.NE. or  *<> or /= Not equal to
.GT. or > Greater than
.GE. or >= Greater than or equal to
Note:
* XL Fortran relational operator.

An arithmetic relational expression is interpreted as having the logical value .true. if the values of the operands satisfy the relation specified by the operator. If the operands do not satisfy the specified relation, the expression has the logical value .false..

If the types or kind type parameters of the expressions differ, their values are converted to the type and kind type parameter of the expression (arith_expr1 + arith_expr2) before evaluation.

Example of an Arithmetic Relational Expression
      IF (NODAYS .GT. 365) YEARTYPE = 'leapyear'

Character Relational Expressions

A character relational expression compares the values of two character expressions. Its form is:



>>-char_expr1--relational_operator--char_expr2-----------------><
 
 

char_expr1  and  char_expr2
are each character expressions

relational_operator
is any of the relational operators described in "Arithmetic Relational Expressions".

For all relational operators, the collating sequence is used to interpret a character relational expression. The character expression whose value is lower in the collating sequence is less than the other expression. The character expressions are evaluated one character at a time from left to right. You can also use the intrinsic functions (LGE, LLT, and LLT) to compare character strings in the order specified by the ASCII collating sequence. For all relational operators, if the operands are of unequal length, the shorter is extended on the right with blanks. If both char_expr1 and char_expr2 are of zero length, they are evaluated as equal.

+-------------------------------IBM Extension--------------------------------+

Even if char_expr1 and char_expr2 are multibyte characters (MBCS) in XL Fortran, the ASCII collating sequence is still used.

+----------------------------End of IBM Extension----------------------------+

Example of a Character Relational Expression
    IF (CHARIN .GT. '0' .AND. CHARIN .LE. '9') CHAR_TYPE = 'digit'

Logical

A logical expression (logical_expr), when evaluated, produces a result of type logical. The form of a logical expression is:



>>-+------------------------------+--logical_disjunct----------><
   '-logical_expr--+-.EQV.------+-'
                   +-.NEQV.-----+
                   |       (1)  |
                   '-.XOR.------'
 
 


Notes:


  1. XL Fortran logical operator


The form of a logical_disjunct is:



>>-+------------------------+--logical_term--------------------><
   '-logical_disjunct--.OR.-'
 
 

The form of a logical_term is:



>>-+---------------------+--logical_factor---------------------><
   '-logical_term--.AND.-'
 
 

The form of a logical_factor is:



>>-+--------------------------------+--------------------------><
   '-+-------+--+-logical_primary-+-'
     '-.NOT.-'  '-rel_expr--------'
 
 

logical_primary is a primary of type logical.

rel_expr is a relational expression.

The logical operators are:


Logical Operator Representing Precedence
.NOT. Logical negation First (highest)
.AND. Logical conjunction Second
.OR. Logical inclusive disjunction Third
.XOR. (See Note *.) Logical exclusive disjunction Fourth (lowest) (See Note *.)
.EQV. Logical equivalence Fourth (lowest)
.NEQV. Logical nonequivalence Fourth (lowest)
Note:
* XL Fortran logical operator.

+-------------------------------IBM Extension--------------------------------+

The .XOR. operator is treated as an intrinsic operator only when the -qxlf77=intxor compiler option is specified. (See "-qxlf77 Option" in the User's Guide for details.) Otherwise, it is treated as a defined operator. If it is treated as an intrinsic operator, it can also be extended by a generic interface.

+----------------------------End of IBM Extension----------------------------+

The precedence of the operators determines the order of evaluation when a logical expression containing two or more operators having different precedences is evaluated. For example, evaluation of the expression A.OR.B.AND.C is the same as evaluation of the expression A.OR.(B.AND.C).

Value of a Logical Expression

Given that x1 and x2 represent logical values, use the following tables to determine the values of logical expressions:

x1 .NOT. x1
True False
False True


x1 x2 .AND. .OR. .XOR. .EQV. .NEQV.
False False False False False True False
False True False True True False True
True False False True True False True
True True True True False True False

Sometimes a logical expression does not need to be completely evaluated to determine its value. Consider the following logical expression (assume that LFCT is a function of type logical):

A .LT. B .OR. LFCT(Z)

If A is less than B, the evaluation of the function reference is not required to determine that this expression is true.

XL Fortran evaluates a logical expression to a LOGICAL(n) or INTEGER(n) result, where n is the kind type parameter. The value of n depends on the kind parameter of each operand.

By default, for the unary logical operator .NOT., n will be the same as the kind type parameter of the operand. For example, if the operand is LOGICAL(2), the result will also be LOGICAL(2).

The following table shows the resultant type for unary operations:

OPERAND RESULT of Unary Operation
* BYTE INTEGER(1) *
LOGICAL(1) LOGICAL(1)
LOGICAL(2) LOGICAL(2)
LOGICAL(4) LOGICAL(4)
LOGICAL(8) LOGICAL(8)
* Typeless Default integer *
Note:
* Resultant types for unitary operations in XL Fortran

If the operands are of the same length, n will be that length.

+-------------------------------IBM Extension--------------------------------+

For binary logical operations with operands that have different kind type parameters, the kind type parameter of the expression is the same as the larger length of the two operands. For example, if one operand is LOGICAL(4) and the other LOGICAL(2), the result will be LOGICAL(4).

+----------------------------End of IBM Extension----------------------------+

The following table shows the resultant type for binary operations:

Table 4. Result Types for Binary Logical Expressions


second operand
first
operand
*BYTE LOGICAL(1) LOGICAL(2) LOGICAL(4) LOGICAL(8) *Typeless
*BYTE *INTEGER(1) *LOGICAL(1) *LOGICAL(2) *LOGICAL(4) *LOGICAL(8) *INTEGER(1)
LOGICAL(1) LOGICAL(1) LOGICAL(1) LOGICAL(2) LOGICAL(4) LOGICAL(8) LOGICAL(1)
LOGICAL(2) LOGICAL(2) LOGICAL(2) LOGICAL(2) LOGICAL(4) LOGICAL(8) LOGICAL(2)
LOGICAL(4) LOGICAL(4) LOGICAL(4) LOGICAL(4) LOGICAL(4) LOGICAL(8) LOGICAL(4)
LOGICAL(8) LOGICAL(8) LOGICAL(8) LOGICAL(8) LOGICAL(8) LOGICAL(8) LOGICAL(8)
*Typeless *INTEGER(1) *LOGICAL(1) *LOGICAL(2) *LOGICAL(4) *LOGICAL(8) *Default Integer
Note:
* Resultant types for binary logical expressions in XL Fortran

If the expression result is to be treated as a default integer but the value cannot be represented within the value range for a default integer, the constant is promoted to a representable kind.

Primary

The form of a primary expression is:



>>-+------------------+--primary-------------------------------><
   '-defined_unary_op-'
 
 

defined_unary_op
is a defined unary operator. See Extended Intrinsic and Defined Operations.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]