dma@bach.BERKELEY.EDU (D. Mark Abrahams) (10/29/86)
In an algebraic expression syntax, what do people think should be the relative precedence of unary minus and exponentiation? In other words, should -2^2 be: 4? (That is, unary minus has higher precedence) -4? (That is, exponentiation has higher precedence) Sure, you can always use parentheses to override the default. But what should unadorned -2^2 equal? In practice there seems to be no general agreement. A quick survey shows that the following give precedence to unary minus (-2^2 = 4): bc (the interactive UNIX algebraic interpreter) GLIM (release 3.12) S Whereas the following give precedence to exponentiation (-2^2 = -4): Basic (at least, our local Basic). BMDP (uses ** not ^) Fortran77 (uses ** not ^ of course) SAS SPSS-X Vaxima In favor of precedence to unary minus: 1) The notion that, in general, unary operators should have precedence over binary operators. 2) The notion that -2 should mean -2 regardless of context (i.e., the `-' sign in -2 is not an operator but part of the number itself); and for consistency the unary minus in -x should have extremely high precedence. 3) It seems that precedence to unary minus leads to a more natural precedence ordering for certain other (non-arithmetic) operators. In favor of precedence to exponentiation: 1) Standard mathematical notation; (or so I'm told by the mathematical types around here); hence, legions of mathematical people expect -2^2 = -4. 2) For even integers p (including the perhaps-most-frequent case, p=2), setting -x^p to (-x)^p renders the `-' a no-op (you could have typed x^p to begin with) whereas setting it to -(x^p) gives you something useful. There must be other pros and cons as well. I'd like to know what other people think about this. If your favorite statistical (or mathematical) interpreter (or language) isn't listed above, what does it do? I'd also like a good sample of simple preferences for one or the other (if mailing preferences only, send directly to me). Thanks... Mark Abrahams dma@bach.berkeley.edu Dept of Statistics, UC Berkeley dma@ucbbach.bitnet ucbvax!bach!dma