questionnable nint() behavior

Dominique Dhumieres dominiq@lps.ens.fr
Thu Mar 23 15:50:00 GMT 2006


I have had a look at the F2003 draft and found:

7.1.8.2 Integrity of parentheses

The sections that follow state certain conditions under which a processor
may evaluate an expression that is different from the one specified by
applying the rules given in 7.1.1 and 7.2.  However, any expression in
parentheses shall be treated as a data entity.

NOTE 7.16

For example, in evaluating the expression A + (B ? C) where A, B, and C are
of numeric types, the difference of B and C shall be evaluated before the
addition operation is performed; the processor shall not evaluate the
mathematically equivalent expression (A + B) ? C.

7.1.8.3 Evaluation of numeric intrinsic operations

The rules given in 7.2.1 specify the interpretation of a numeric intrinsic
operation.  Once the interpretation has been established in accordance with
those rules, the processor may evaluate any mathematically equivalent
expression, provided that the integrity of parentheses is not violated.

...

NOTE 7.18
The following are examples of expressions with allowable alternative forms
that may be used by the processor in the evaluation of those expressions.
A, B, and C represent arbitrary real or complex operands; I and J represent
arbitrary integer operands; and X, Y, and Z represent arbitrary operands of
numeric type.
Expression          Allowable alternative form
X + Y               Y + X
X * Y               Y * X
-X + Y              Y - X
X + Y + Z           X + (Y + Z)
X - Y + Z           X - (Y - Z)
X * A / Z           X * (A / Z)
X * Y - X * Z       X * (Y - Z)
A / B / C           A / (B * C)
A / 5.0             0.2 * A

The following are examples of expressions with forbidden alternative forms
that shall not be used by a processor in the evaluation of those
expressions.
Expression          Forbidden alternative form
I / 2               0.5 * I
X * I / J           X * (I / J)
I / J / A           I / (J * A)
(X + Y) + Z         X + (Y + Z)
(X * Y) - (X * Z)   X * (Y - Z)
X * (Y - Z)         X * Y - X * Z

...

NOTE 7.19
For example, in the expression
A + (B - C)
the parenthesized expression (B ? C) shall be evaluated and then added to A.

...

NOTE 7.31

In interpreting a level-2-expr containing two or more binary operators 
+ or ?, each operand (addoperand) is combined from left to right.  
Similarly, the same left-to-right interpretation for a multoperand in
add-operand, as well as for other kinds of expressions, is a consequence of
the general form.  However, for interpreting a mult-operand expression when
two or more exponentiation operators ** combine level-1-expr operands, each
level-1-expr is combined from right to left.

(This remind me reading the European constitution!) Am I correct to understand
the above as "violating parentheses is FORBIDEN by the standard"?  while
bypassing the left to right evaluation is allowed, for instance computing
A+B+C as A+(B+C) instead of (A+B)+C (say because B+C has already been computed)?

Dominique



More information about the Fortran mailing list