Arithmetic

An arithmetic expression (arith_expr), when evaluated, produces a numeric value. The form of arith_expr is:
Read syntax diagramSkip visual syntax diagram arith_expr +  -  arith_term
The form of arith_term is:

Read syntax diagramSkip visual syntax diagramarith_term /  * arith_factor
The form of arith_factor is:

Read syntax diagramSkip visual syntax diagramarith_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

IBM® Open 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 IBM Open 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 begins For details of exception conditions that can arise during evaluation of floating-point expressions, see Detecting and trapping floating-point exceptions. IBM extension ends