(j3.2006) Fw: a question on MERGE and expression evaluation
Thu Nov 18 15:32:09 EST 2010
This question gets asked repeatedly on the j3 email list. The most
recent reply I found was from Malcolm, in reply to Bob. I'll paste that
in below. However, I do think that an implementation that DOES evaluate
all of the arguments first is still standard conforming, so the behavior
would not be the same in all cases. In the particular example you
showed, assuming that traps on divide-by-zero are not set, the code
should be fine. It amounts to merge(infinity, 0, a>0) which would
evaluate to 0.
Robert Corbett wrote:
> > The assertion was made on comp.lang.fortran that the intrinic
> > function MERGE must evaluate all of its arguments.
Come on Robert, you know better than that. Some compilers just
evaluated a mathematically-equivalent expression instead, as they are
explicitly allowed to. Or took advantage of the following explicit licence:
"It is not necessary for a processor to evaluate all of the operands
of an expression, *OR TO EVALUATE ENTIRELY EACH OPERAND*, if the value
of the expression can be determined otherwise."
It has been said that "otherwise" includes "by divine revelation", but
even that's not required here.
The program isn't even standard-conforming, since if a function defines
a variable, and the result of the function wasn't necessary for the
evaluation of the expression, that variable becomes undefined. So it is
printing an undefined variable.
"If a statement contains a function reference in a part of an
expression that need not be evaluated, all entities that would have
become defined in the execution of that reference become undefined..."
> > Are the three compilers that got the first result not
> > standard conforming in this regard?
You are just lucky you didn't start WW3!
-- .....................Malcolm Cohen, Nihon NAG, Tokyo.
On 11/18/10 2:08 PM, Jim Xia wrote:
> ----- Forwarded by Jim Xia/Toronto/IBM on 11/18/2010 03:08 PM -----
> From: Jim Xia/Toronto/IBM
> To: j3-bounces at j3-fortran.org
> Date: 11/18/2010 02:46 PM
> Subject: a question on MERGE and expression evaluation
> We have a piece of code that a user claims should result in a
> divide-by-zero exception.
> real a(10), y(10)
> a = 0.0
> y = merge(1.0/a, 0.0, a>0.0)
> print *, y
> None of the compilers I have tried (XLF, NAG and ifort) result in
> exception. The standard is also conflicting in saying whether or not
> 1.0/a should be evaluated at the function invocation time.
> Here are some quoted text from F08
> 12.5.3 seems to say 1.0/a should be evaluated at the function call.
> 13.2.3 seems to suggest 1.0/a need not to be evaluated since evaluation
> with undefined value can result in side-effects.
> Although I'm taking the 13.2.3 as a strong argument, F03 indeed spells
> out the intention of omitting evaluation of actual argument in Annex C
> (C 9.5)
> However, that section is deleted in F08.
> So the question is: should 1.0/a be evaluated upon the function call to
> Jim Xia
> XL Fortran Compiler Test
> IBM Toronto Lab at 8200 Warden Ave, Markham, On, L6G 1C7
> Phone (905) 413-3444 Tie-line 313-3444
> email: jimxia at ca.ibm.com
> D2/YF7/8200 /MKM
Bill Long longb at cray.com
Fortran Technical Support & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9142
Cray Inc./Cray Plaza, Suite 210/380 Jackson St./St. Paul, MN 55101
More information about the J3