Unary minus and exponentiation precedence
Unary minus and exponentiation precedence
- Subject: Unary minus and exponentiation precedence
- From: Doug McNutt <email@hidden>
- Date: Wed, 21 Aug 2002 13:43:48 -0600
The problem is that, to match algebra as learned in school, the exponentiation operator ought to have higher precedence than the unary minus at least when there is only one term in an expression.
In Applescript it doesn't and that makes for a no-warning trap for the unwary. But I guess it's the way of life in modern programming. Microbesoft Excel has it wrong in worksheet formulas but right in Visual Basic. FORTRAN, Maple, and TI calculators have it right.
I don't expect to get it fixed so we'll consider this just for the list archives where it isn't right now.
set x to 2
set y to (1 - x) * (1 + x) - 1 -- OK so multiply the binomials
set y to 1 - x ^ 2 - 1 -- OK so cancel out the +1 and -1
set y to -x ^ 2 -- oops. Not as learned in school. (returns +4)
set y to -x * x -- OK
-- set y to - powr(x, 2) -- Won't compile
set y to 0 - powr(x, 2) -- OK
set y to -(powr(x, 2)) -- OK
set y to -1 * (powr(x, 2)) -- OK
-- set y to -1 * powr(x, 2) -- Editor changes the formula, why?
set y to 0 - x ^ 2 -- OK
set y to -x ^ 3 -- Correct answer but probably for wrong reason
set y to -x ^ 4 -- Not as learned in school
-- set y to -x ^ (1 / 2) -- error, numeric operation too large
-- set y to -x ^ (1 / 3) -- error, numeric operation too large
set y to x ^ (1 / 2) -- OK
set y to -(x ^ (1 / 2)) -- OK
set y to 0 - x ^ (1 / 2) -- OK
on powr(a, b)
set ans to a ^ b
return ans
end powr
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.