comp.lang.ada
 help / color / mirror / Atom feed
From: "Norman H. Cohen" <ncohen@watson.ibm.com>
Subject: Re: GNAT messages and the not operator (pitfall alert!)
Date: 1996/11/18
Date: 1996-11-18T00:00:00+00:00	[thread overview]
Message-ID: <32908796.294D@watson.ibm.com> (raw)
In-Reply-To: 55p8b6$l29$1@goanna.cs.rmit.edu.au


Richard A. O'Keefe wrote:
 
> "Norman H. Cohen" <ncohen@watson.ibm.com> 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
>         <operand 1> ADD <operand 2> MULTIPLY <operand 3>
> which naturally groups as
>         <operand 1> ADD (<operand 2> MULTIPLY <operand 3>)
> 
> 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




  reply	other threads:[~1996-11-18  0:00 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-10-24  0:00 GNAT messages and the not operator (pitfall alert!) Robert Dewar
1996-10-24  0:00 ` Robert Dewar
1996-10-25  0:00   ` whiting_ms@corning.com (Matt Whiting)
1996-10-26  0:00     ` David C. Hoos, Sr.
1996-10-27  0:00     ` Robert Dewar
1996-10-28  0:00       ` Matthew S. Whiting
1996-10-26  0:00   ` John Herro
1996-10-26  0:00     ` Matthew Heaney
1996-10-29  0:00       ` Robert Dewar
1996-10-26  0:00     ` Robert Dewar
1996-10-29  0:00       ` John Herro
1996-10-29  0:00         ` Robert Dewar
1996-10-30  0:00     ` Dr. Peter E. Obermayer
1996-10-30  0:00       ` John Herro
1996-10-30  0:00         ` Robert Dewar
1996-11-01  0:00         ` User Password expiration date extractor? Stein-Aksel Basma
1996-10-29  0:00   ` GNAT messages and the not operator (pitfall alert!) Norman H. Cohen
1996-10-29  0:00     ` whiting_ms@corning.com (Matt Whiting)
1996-10-30  0:00       ` Norman H. Cohen
1996-11-06  0:00         ` Richard A. O'Keefe
1996-11-18  0:00           ` Norman H. Cohen [this message]
1996-11-18  0:00             ` Robert Dewar
1996-10-30  0:00       ` David C. Hoos, Sr.
1996-10-30  0:00         ` whiting_ms@corning.com (Matt Whiting)
1996-10-30  0:00           ` Norman H. Cohen
1996-10-30  0:00       ` Robert Dewar
1996-11-05  0:00     ` Robert Dewar
1996-10-28  0:00 ` Cary Jamison
1996-10-29  0:00   ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox