Hmmm ...
FWIW: I'm not good on the jargon, but is "-2" really a unary "-" and a number (2), or is "-2" a number in itself (ie. the sign is not an operator)? This is what Maurice suggested in his second posting, and is the natural/intuitive interpretation of "-2".
Joe.
-----Original Message----- From: J. David Bryan [SMTP:dbryan@bcpl.net] Sent: Sunday, September 09, 2001 4:20 PM To: GNU Pascal List Subject: Re: pow operator miscalculation
On 8 Sep 2001, at 10:44, Maurice Lombardi wrote:
What says the standard ?
The Extended Pascal standard, Section 6.8.1 "Expressions, General" says:
"Operator precedences shall be according to five classes of operators as follows. The operator 'not' shall have the highest precedence, followed by the exponentiating-operators, followed by the multiplying-operators, the adding-operators and signs, and finally, with the lowest precedence, the relational-operators. Sequences of two or more operators of the same precedence shall be left associative."
Section 6.8.3.1, "Operators, General" defines the types of operators mentioned above:
exponentiating-operator = "**" | "pow"
multiplying-operator = "*" | "/" | "div" | "mod" | "and" | "and_then"
adding-operator = "+" | "-" | "><" | "or" | "or_else"
relational-operator = "=" | "<>" | "<" | ">" | "<=" | ">=" | "in"
So that means that unary minus has the same precedence as binary minus, which is lower than that of "pow". So:
-2 pow 2
is evaluated as:
-(2 pow 2)
and so equals -4. GPC is correct.
As an aside, these rules are a bit odd, in that:
a = b and c = d
is evaluated as:
a = (b and c) = d
rather than as the more likely intended:
(a = b) and (c = d)
(In Ada, by contrast, the logical operators "and", "or", and "xor" have the lowest priority, so the first expression is evaluated as shown in the third expression.)
-- Dave