Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!unmvax!brainerd From: brainerd@unmvax.unm.edu (Walt Brainerd) Newsgroups: comp.lang.fortran Subject: Re: operator precedence on FORTRAN 77 Summary: It's illegal Keywords: unary minus, exponenation Message-ID: <320@unmvax.unm.edu> Date: 2 Sep 89 17:40:30 GMT References: <3744@uwovax.uwo.ca> <8980@saturn.ucsc.edu> Distribution: na Organization: University of New Mexico at Albuquerque Lines: 43 In article <8980@saturn.ucsc.edu>, sla@helios (Steve Allen) writes: > Which takes precedence, a unary minus, or an exponentiation? This question The standard (p 6-2, l. 45): "the exponentiation operator has precedence over the negation operator", but I don't think this is the relevant fact. > recently came up when one of our users ran a code which should have had more > parentheses. The example is below. My own impression is that the > Sun interpretation is the only one that makes sense, but I don't know > what the FORTRAN-77 standard requires. Is there a correct interpretation? > No, see below. > double precision func1, func2, func3, func4 > func1 = 10.D0** -(3.D0)/2.D0 > func2 = 10.D0**(-(3.D0)/2.D0) > func3 = 10.D0** -3.D0 /2.D0 > func4 = 10.D0** - 3.D0 /2.D0 > write(*,*) func1, func2 > write(*,*) func3, func4 > end > The expressions in all but func2 are illegal in F77 and still will be illegal in the propsed F8x. Therefore, all of the implementations cited have treated this as an extension and are free to give any interpretation they want. The only relevant question is whether one or the other interpretation makes better sense, is more in conformance with mathematical usage, or is a more logical extension of the exiting rules of Fortran. func3 and func4 must be equivalent because blanks are insignificant in Fortran 77 source. func1 also must be equivalent because putting parens around a constant should have no effect. The two interpretations cited correspond to placing parens as follows (the D is also irrelevant). (10. ** -3.) / 2. 10. ** (-3. / 2.) The first seems more reasonable because ** has higher precedence than /, but one catch is that 10. ** -3 is still illegal, so must be interpreted as if it were 10. ** (-3.). I guess I would agree that the Sun interpretation (the one on the left) is what I would expect, but this is just OPINION. -- Walt Brainerd Unicomp, Inc. brainerd@unmvax.cs.unm.edu 2002 Quail Run Dr. NE Albuquerque, NM 87122 505/275-0800