[J3] Evaluation of arguments to MERGE

Van Snyder van.snyder at sbcglobal.net
Wed Mar 27 21:34:25 UTC 2024


On Wed, 2024-03-27 at 17:32 +0000, Clune, Thomas L. (GSFC-6101) via J3
wrote:
> I have a colleague that is asking about the behavior of MERGE in the
> program below.    The intent of the statement is to avoid divide-by-
> zero when the FLAG argument is false.  
>  This appears to work as intended with most compilers, but under
> certain flags at least one compiler produces a floating invalid.
> 
>  
> 
> I had thought maybe there was special language in the standard about
> evaluation of the arguments to MERGE, but I did not find any.  
> Therefore, I believe a standard conforming
>  compiler is permitted to evaluate the TSOURCE argument even when
> FLAG evaluates to false.      If I’m wrong about this, please point
> me to where in the standard that explains this, otherwise I will
> relay this as a quality-of-implementation concern to my colleague.  

Subclause 15.5.3 Function Reference says "When it is invoked, all
actual argument expressions are evaluated, then the arguments are
associated, and then the function is executed."
When Fortran 90 was being developed, I advocated for what we finally
did, that is, conditional expressions. I was told we didn't need them
because of the existence of the MERGE intrinsic function. Back then, I
didn't appreciate the distinction between eager and lazy evaluation of
procedure arguments.  All Fortran procedure invocations, including
intrinsic ones, use eager evaluation, as explained in 15.5.3.
>  
> program mergetest
>   implicit none
>   real*8 :: a,b,c
>   call random_number(a)
>   b = 1d0
>   a = a - a
>   c = merge(b/a, 0d0, a > 0d0)
>   write(6,*) c
> end program mergetest
>  
> Thanks, 
>  
> 
> Tom
> 
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20240327/662ac7db/attachment.htm>


More information about the J3 mailing list