Re: Unary minus and exponentiation precedence
Re: Unary minus and exponentiation precedence
- Subject: Re: Unary minus and exponentiation precedence
- From: John Baltutis <email@hidden>
- Date: Thu, 22 Aug 2002 00:13:15 -0700
The exponential does have precedence. However, the original term in the
binomial expansion is properly -(x^2), thus,
set y to -(x ^ 2)
-> -4.0
Your examples are number raised to a power (1x)^n with results as learned
in school
set y to -x ^ 2
-> 4.0
set y to -x ^ 3
-> -8
set y to -x ^ 4
-> 16.0
set y to -x ^ (0.5) --(root of negative number is an imaginary number and
not well handled)
On 08/21/02, Doug McNutt <email@hidden> wrote:
>
>
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.