[J3] Evaluation of arguments to MERGE

Clune, Thomas L. (GSFC-6101) thomas.l.clune at nasa.gov
Wed Mar 27 17:32:21 UTC 2024


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.

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/8b1995a2/attachment.htm>


More information about the J3 mailing list