Re: Can someone explain this?
Re: Can someone explain this?
- Subject: Re: Can someone explain this?
- From: "Mark J. Reed" <email@hidden>
- Date: Mon, 16 Mar 2009 11:05:04 -0400
On Sat, Mar 14, 2009 at 11:14 PM, Doug McNutt <email@hidden> wrote:
> The problem for us physicists is that we look at x^2 as a function of x.
> Isn't it?
Of course x^2 is a function of x. And if you define f(x) as x^2 and
then write -f(x), that's clear. But if you just write -x^2, that's
not as clear.
Evaluating the string "-3^2" as a mathematical expression poses
exactly the same problem as evaluating the string "1+2*3". In each
case, there are two operators, and the result you get depends on the
order in which you apply those operators.
1 + 2 * 3 is, according to My Dear Aunt Sally, equal to 7.
But if you type in 1, +, 2, ×,3,= on a typical four-banger calculator,
it will tell you the answer is 9. Some programming languages act the
same way - they're explicitly designed with no precedence, and just
evaluate left-to-right.
According to the extended version of the traditional precedence rules
(with operations like exponentiation that aren't well-known enough to
be included in the popular mnemonics), exponentiation comes before
multiplication, and negation is treated as mulitplication by -1, so
-3^2 = -1 * 3^2 = -1 * 9 = -9
But if you instead negate the 3 before squaring it, you'll get +9,
which is what Applescript, Excel, etc. do.
> f(x,y) = x^2 - y^2
> is surely a function of x and y. Why in the world should its value depend on
> the order in which the two parts are evaluated?
It doesn't. But unary minus and binary minus are not the same thing;
they're two different operations that happen to be represented by the
same symbol.
> And how come, for extra credit, is -2^2 = -4 in good old K&R C run in a 2's
> complement machine?
Because K&R C has no exponentiation operator. You have to use the
pow() function from <math.h>. ^ is the exclusive-or operator. So
while -2^2 happens to be -4, -3^2 is -1, -4^2 is -2 . . .
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden