Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Parentheses in Numeric Expressions

You can use parentheses to force a particular order of evaluation. When part of an expression is enclosed in parentheses, that part is evaluated first. The resulting value is used in the evaluation of the remainder of the expression.

NOTE:

The Intel® Fortran compiler does not honor parentheses in floating-point expressions when optimization is enabled (default). The -fprotect-parens (Linux) and /Qprotect-parens (Windows) compiler options force the compiler to honor parentheses.

In the following examples, the numbers below the operators indicate a possible order of evaluation. Alternative evaluation orders are possible in the first three examples because they contain operators of equal precedence that are not enclosed in parentheses. In these cases, the compiler is free to evaluate operators of equal precedence in any order, as long as the result is the same as the result gained by the algebraic left-to-right order of evaluation.

     4 + 3 * 2 - 6/2   = 7
       ^   ^   ^  ^
       2   1   4  3
     (4 + 3) * 2 - 6/2   = 11
        ^    ^   ^  ^
        1    2   4  3
     (4 + 3 * 2 - 6)/2   = 2
        ^   ^   ^   ^
        2   1   3   4
     ((4 + 3) * 2 - 6)/2   = 4
         ^    ^   ^   ^
         1    2   3   4

Expressions within parentheses are evaluated according to the normal order of precedence. In expressions containing nested parentheses, the innermost parentheses are evaluated first.

Nonessential parentheses do not affect expression evaluation, as shown in the following example:

     4 + (3 * 2) - (6/2)

However, using parentheses to specify the evaluation order is often important in high-accuracy numerical computations. In such computations, evaluation orders that are algebraically equivalent may not be computationally equivalent when processed by a computer (because of the way intermediate results are rounded off).

Parentheses can be used in argument lists to force a given argument to be treated as an expression, rather than as the address of a memory item.