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
Maurice Lombardi wrote:
Maurice Lombardi wrote:
Irfan Fazel wrote:
writeln(-2 pow 0); (* should be 1 *)
writeln(-2 pow 1);
writeln(-2 pow 2); writeln; (* should be 1 *)
Problem of relative priority between - and pow.
writeln(-2 pow 0,' ',(-2) pow 0);
gives
-1 1
IIRC priority of pow should be greater than binary - but smaller than unary - This would give the behaviour that everybody expects.
No this is stupid: -a pow 2 should be -(a pow 2) to give what everybody expects.
Only for a number -2 the - should take precedence.
This would seem quite inconsistent. If you replace the number 2 by, say, a constant a = 2, you get a different precedence!?
J. David Bryan wrote:
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)
I consider this one of the real design mistakes of Pascal (of course, we can't do anything about it now).
da Silva, Joe wrote:
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)?
6.8.1 defines a "primary" in an expression to be (among other things) an unsigned constant, and allows a sign only in "simple_expression" (on the same level as adding operators).
This is what Maurice suggested in his second posting, and is the natural/intuitive interpretation of "-2".
In mathematics, it's not: -2^2 = -4 (just as in Pascal).
(In non-mathematic usage, you rarely deal with situation like powers of negative numbers, where the difference actually matters.)
Frank
On 10 Sep 2001, at 9:57, da Silva, Joe wrote:
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)?
The Extended Pascal standard, section 6.8.1, "Expressions, General," says that the expression "-2" is a simple-expression, which is defined as a sign followed by a term. So "-" is the sign, and "2" is the term. The standard therefore treats any expression with a leading sign as a unary operator applied to an unsigned expression.
To continue, "-2 pow 2" is parsed as the sign "-" and the term "2 pow 2". The term is parsed as a factor, which is defined as a primary, followed by an exponentiating-operator (in this case, "pow"), followed by another primary. "2" is a primary because primaries may be unsigned-constants.
This is what Maurice suggested in his second posting, and is the natural/intuitive interpretation of "-2".
Unfortunately, "intuitive interpretations" don't count when a standard says otherwise.... :-)
-- Dave
Hi Folks,
the latest version I can build on IRIX 6.5 on the top of gcc-core-2.95.2 is gpc-20010502. All the versions more recent than that will fail at the same point. The result is the same using the native make or its GNU counterpart. The log of the make of the working and a faulty versions didn't went trough as attachments. If someone is ready to deal with this problem I can send those directly.
(There are serious bugs in the "working" version but I can not debug the recent ones without actually compiling them of course.)
Cheers,
miklos
Miklos Cserzo University of Birmingham c/o School of Biosciences MRC - Bioinformatics Project Tel: +44-121-414-3037 Schools of Biosciences and Medicine Fax: +44-121-414-5925 Edgbaston, Birmingham B15 2TT E-mail: miklos@bip.bham.ac.uk United Kingdom