🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #12276

Re: IEEE arithmetic (Goldberg paper)

#12276
From: John C Green Jr
Date: Thu, 27 Jun 1991 19:11
63 lines
3103 bytes
This discussion involves non `standard conforming' programs which get different
answers with different compilers or with different optimization options on the
same compiler.

My favorite program of this type in genuine upper case FORTRAN IV was
constructed by Stanley Rabinowitz in 1969. He got paid $50 by the magazine
"Software Age" which published it in their column "X-Tran's Adventures In
Troubletran".

      LOGICAL FOO
      I = 0
      IF ( I .EQ. 0 .OR. FOO ( I ) ) I = I + 2
      WRITE ( 6 , 99 ) I
      STOP
   99 FORMAT ( 1X , 1I1 )
      END

      LOGICAL FUNCTION FOO ( J )
      J = J + 1
      FOO = .FALSE.
      RETURN
      END

This was run on an IBM System/360 Model 50 under three of the world's most
popular compilers:

    Fortran G   Answer: 1
    Fortran H   Answer: 2
    Watfor      Answer: 3

Watfor checked to see if I was 0 then called the function.
Fortran H checked to see if I was 0 and then did not call the function.
Fortran G called the function and then checked to see if I was 0.

I found it amazing that such a small program can get three different answers
with no compiler diagnostics and no runtime diagnostics on three of the most
popular compilers on the same machine!

The paragraph in the Fortran '66 standard says approximately:

    If a subroutine subprogram or a function subprogram changes any of its
    arguments or any element in COMMON then that argument or element of COMMON
    cannot appear anywhere else in any statement that calls the subroutine or
    invokes the function.

In line 3 of the program the invocation of the function FOO changes I and I
appears no less than 3 other times in this statement.

The paragraph in the Fortran '77 standard section 6.6, `Evaluation of
Expressions' says in part:

    The execution of a function reference in a statement may not alter the
    value of any other entity within the statement in which the function
    reference appears. The execution of a function reference in a statement may
    not alter the value of any entity in common that affects the value of any
    other function reference in that statement. However, execution of a
    function reference in the expression `e' of a logical IF statement is
    permitted to affect entities in the statement `st' that is executed when
    the value of the expression `e' is true.

In line 3 of the program the invocation of the function FOO changes I and I
appears in the expression `e' of the logical IF statement. In Fortran '77 there
is no problem with the I = I + 2.
Technical Details
Message-ID: <4765@alliant.Alliant.COM>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!sdd.hp.com!think.com!snorkelwacker.mit.edu!linus!alliant!jgreen
References: <9106190124.AA27410@ucbvax.Berkeley.EDU> <1991Jun22.003029.16748@watson.ibm.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!sdd.hp.com!think.com!snorkelwacker.mit.edu!linus!alliant!jgreen
From: jgreen@Alliant.COM (John C Green Jr)
Newsgroups: comp.arch
Subject: Re: IEEE arithmetic (Goldberg paper)
Summary: An Old Anecdote
Message-ID: <4765@alliant.Alliant.COM>
Date: 27 Jun 91 19:11:47 GMT
References: <9106190124.AA27410@ucbvax.Berkeley.EDU> <1991Jun22.003029.16748@watson.ibm.com>
Organization: Alliant Computer Systems, Littleton, MA
Lines: 63