Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!apple!amdahl!key!sjc From: sjc@key.COM (Steve Correll) Newsgroups: comp.lang.fortran Subject: Re: Side effects in boolean expressions Message-ID: <2041@key.COM> Date: 14 Aug 90 23:32:58 GMT References: <2022@key.COM> <59110@lanl.gov> Organization: Key Computer Labs, Fremont, CA Lines: 40 In article <59110@lanl.gov>, jlg@lanl.gov (Jim Giles) writes: > From article <2022@key.COM>, by sjc@key.COM (Steve Correll): > > In article <58237@lanl.gov>, jlg@lanl.gov (Jim Giles) writes: > >> > X .GT. Y .OR. L(Z) > > [...] > > C is predictable. The Fortran standard says that the processor _need_ _not_ > > execute the function L if X exceeds Y, and that therefore the program must > > assume Z is undefined. The C standard for "(x > y) || l(&z)" says that the > > processor _must_ _not_ execute l if x exceeds y... > > C is predictable only if you know in advance whether X was greater than > Y. If I knew that, I wouldn't have tested it. This is what I meant > when I said that neither language is defined in such a way as to be > able to tell a priori (from the first) whether the function would be > evaluated or not... > It also means that I can't > apply the mathematical properties of the 'or' operator to correctness > proofs of the program. Perhaps we simply define "predictable" and "a priori" differently, but consider the following. Even though you don't know the value of x in advance, the C code is legal and its behavior predictable; the Fortran is neither: if ((x < 0.0) || (sqrt(x) < 10.0)) ... IF ((X .LT. 0.) .OR. (SQRT(X) .LT. 10.0)) ... If x is negative, a legal C processor will not call sqrt, but a legal Fortran processor may or may not--in fact, it could vary with the phase of the moon! By refusing to define the order of evaluation, Fortran gives the processor more freedom (e.g. to optimize). When proving correctness of a C program, notice that: if (p0 || p1) s0; is equivalent to: if (p0) s0; else if (p1) s0; -- ...{sun,pyramid}!pacbell!key!sjc Steve Correll