From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-0.0 required=3.0 tests=BAYES_20 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 24 Jun 93 13:35:50 GMT From: agate!howland.reston.ans.net!darwin.sura.net!haven.umd.edu!news.umbc.edu! nobody@ucbvax.Berkeley.EDU (Mike Berman) Subject: Precedence Rules (was Re: Software vendors not using Ada but C) Message-ID: <20cajmINNqeb@umbc4.umbc.edu> List-Id: mfeldman@seas.gwu.edu (Michael Feldman) writes: | In article <1993Jun23.163346.29745@sei.cmu.edu> firth@sei.cmu.edu | (Robert Firth) writes: | >Good grief. And I thought Jean Ichbiah was paranoid when he refused to | >give "and" and "or" the same precedence rules in Ada that they had in the | >propositional calculus. Not verbatim, but approximately: "Robert, a | >programmer should never have to learn a new precedence rule as part of | >a language. The language should use only rules everyone already knows." | And how! Students have more important things to learn than big precedence | tables. IMHO the precedence rules are designed to make expression | evaluation well-defined and (reasonably) intuitive, in the absence of | parentheses. I teach precedence as a more-or-less advanced topic, more | to illustrate how the language designers thought out the precedence | than to make the students learn it. When talking precedence, of course my thoughts turn immediately to mod and rem (don't yours?). I have seen the best programmers/mathematicians/all-around-nice-guys brought down by the seemingly non-intuitive precedence rules surrounding negative arguments and the mod and rem operators. The expression -3 mod 2 is interpreted as -(3 mod 2), not (-3) mod 2, as most people expect (I can make that generalization - we instructors get to ask lots o' people). The syntactic reasoning is clear, but not obvious. mod and rem are grouped with the multiplying operators, which have higher precedence than unary minus. For * and / this makes no difference in the first operand, as (-3)*2 and -(3*2) produce the same result. When I read Mike and Roberts' comments, I thought "Oh, these rules must somehow mirror some reality with which I am not familiar". Or something like that. Checking the Rationale, I found the following (p. 92): The operations /, mod, and rem require explanation. There is no universal agreement on the semantics of these operations for negative operand values. Because different machines perform these operations differently, it is tempting not to define them for negative values. So what's the real story-behind-the-story? Is it just easier to set up the precedence rules this way, or is there some neat anecdote associated with mod and rem? Also, (I've always wanted to ask this), does anyone really use mod and rem with negative operands? -- Mike Berman University of Maryland, Baltimore County berman@umbc.edu The views represented in the above post are my own.