Thread Links Date Links
Thread Prev Thread Next Thread Index Date Prev Date Next Date Index

Re: On Arnold's challenge & Paul's Observation... - reordering



On Sat, Nov 29, 2008 at 1:46 PM, Michel Hack <hack@xxxxxxxxxxxxxx> wrote:
> On (Sat, 29 Nov 2008 13:40:31 -0600) Gabriel Dos Reis wrote:
>> On Fri, Nov 28, 2008 at 2:23 PM, Ian McIntosh <ianm@xxxxxxxxxx> wrote:
>>> No, the C and Fortran standards both say the order of a+b+c is unspecified.
>>
>> You are probably talking of the order of evaluation of operands (or
>> arguments).  That is entirely different from the association of operands.
>
> I believe Fortran uses operator precedence (other than via parenteses)
> strictly for parsing purposes, and NOT for evaluation order.
>
> See Van Snyder's post of Fri, 28 Nov 2008 14:40:27 -0800 where he quotes
> a paragraph that appears in both Fortran77 and the draft 2008 standard:
>
>    Once the interpretation of a numeric intrinsic operation is
>    established, the processor may evaluate any mathematically
>    equivalent expression, provided that the integrity of parentheses
>    is not violated.
>
> Rules may be different for C and C++.

Yes.
C and C++ do not just use the precedence for parsing only.

 For example,  in C and C++

     a + b + f() + g()

is syntactically and semantically equivalent to the the verbose form:

   ((a + b) + f()) + g()

And there is no ambiguity about that.  And in general, the compiler
is not allowed to rearrange the operands in either form -- it can do
so only if it can prove that the result is unchanged, under the
"as-if rule".

But the evaluation order is left unspecified.   What is meant is that
the compiler has the freedom to evaluates g() before evaluating
((a + b) + f()), or after.  But in either case, it must add the values of the
intermediary sub expressions as  written..
Similarly, the compiler is free to evaluate f() before (a+b), but it must
add the values in the order specified in the source code.

  -- Gaby