While using GPC (both an older version and a more current version, info at bottom) the results of using the pow operator were not always correct (as shown in the typescript, results were same regardless of system).
System #1
uname -a
SunOS csd 5.7 Generic_106541-15 sun4u sparc SUNW,Ultra-2
gpc -v
Reading specs from /usr/local/lang/lib/gcc-lib/sparc-sun-solaris2.7/2.8.1/specs gpc version 19990118, based on gcc-2.8.1
System #2
uname -a
SunOS cse 5.8 Generic_108528-10 sun4u sparc SUNW,Ultra-2
gpc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/specs gpc version 20010623, based on 2.95.3 20010315 (release)
Thanks, Fazel
P.S. I apologize in the event that this this infact is not an error or I have emailed the wrong party.
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.
Maurice
Maurice Lombardi wrote:
Irfan Fazel wrote:
IIRC priority of pow should be greater than binary - but smaller than unary -
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. In fact I always use () in those possibly ambiguous cases to be sure. What says the standard ?
Maurice
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