From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3b4bed4f74b8ac49 X-Google-Attributes: gid103376,public From: "Norman H. Cohen" Subject: Re: GNAT messages and the not operator (pitfall alert!) Date: 1996/11/18 Message-ID: <32908796.294D@watson.ibm.com>#1/1 X-Deja-AN: 197231327 references: <32762A30.D2D@watson.ibm.com> <1996Oct29.145959.1@corning.com> <327771EE.1237@watson.ibm.com> <55p8b6$l29$1@goanna.cs.rmit.edu.au> content-type: text/plain; charset=us-ascii organization: IBM Thomas J. Watson Research Center mime-version: 1.0 reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada x-mailer: Mozilla 3.0 (Win95; I) Date: 1996-11-18T00:00:00+00:00 List-Id: Richard A. O'Keefe wrote: > "Norman H. Cohen" writes: > > >(As you noted, "-5 mod 4" means, surprisingly, "-(5 mod 4)", so the > >table entry for X mod 4 when X = -5 is NOT the value of -5 mod 4.) > > I don't understand what is surprising about -5 mod 4 being the same > as -(5 mod 4). After all, you expect -x to be the same as 0-x, > and 0 - 5 mod 4 is > ADD MULTIPLY > which naturally groups as > ADD ( MULTIPLY ) > > The grouping, at least, is identical in Pascal. This is a fine explanation of why, according to the language rules, -5 mod 4 does indeed mean -(5 mod 4). However, whether or not this is "surprising" is a psychological issue, not an issue of programming-language rules. I suppose you could argue that if -(5 mod 4) is the interpretation that follows from the language rules, nobody able to read the reference manual should be "surprised" by it. Nonetheless, the fact is that many Ada experts, including the designer of the language, *were* fooled by an expression of this form. It's not that they didn't understand the precedence rules--if you had asked straight out whether -5 mod 4 means (-5) mod 4 or -(5 mod 4), all would have answered correctly. However, when asked "What is the value of -5 mod 4"?, they assumed that this was a question about the semantics of mod for negative operands rather than a question about precedence rules, since interesting questions are more often questions of semantics rather than syntax, and familiarity with the behavior of rem and mod for negative operands is a good litmus test of Ada expertise. Thus distracted from questions of precedence, they intuitively interpreted the expression incorrectly. Whatever the language rules say, the brain apparently tends to see a unary minus as binding more tightly than a word surrounded by spaces. Perhaps writing - 5 mod 4 (with a space after the minus) would ameliorate this effect somewhat, but the best advice to a programmer writing this expression is to use parentheses-- -(5 mod 4) --even though they are technically unnecessary. After all, one should not assume that whoever is going to read the program is less easily fooled than Jean Ichbiah and the other experts who answered incorrectly. -- Norman H. Cohen mailto:ncohen@watson.ibm.com http://www.research.ibm.com/people/n/ncohen