Unary minus precedence preference
07-21-2014, 08:00 PM
Post: #1
 Claudio L. Senior Member Posts: 1,682 Joined: Dec 2013
Unary minus precedence preference
While symbolic object support in newRPL is getting ready to be released in a new demo, I stumbled upon the unary minus precedence ambiguity:

Excel does: -2^2 = 4
while almost everybody else give -2^2= -4

I implemented it as -2^2=-4 for the time being (just to follow the herd), but are there any good reasons to choose one precedence or the other?

Are there any other ambiguities that I need to be aware of regarding other operators?

Claudio
07-21-2014, 08:09 PM
Post: #2
 walter b On Vacation Posts: 1,957 Joined: Dec 2013
RE: Unary minus precedence preference
Seems unary minus being the Loch Ness monster of our forum: tends to appear every once and a while and is almost impossible to get rid of in times where news are scarce.

IIRC, we had a very similar thread recently. Anyway (and once more):
-2^2 = (-1)*2^2 = (-1)*4 = -4 , unambiguously.

And now you've learned that Excel is no real authority in mathematics (it was written for controllers, if that suffices as a reason).

d;-)
07-21-2014, 09:13 PM
Post: #3
 Claudio L. Senior Member Posts: 1,682 Joined: Dec 2013
RE: Unary minus precedence preference
(07-21-2014 08:09 PM)walter b Wrote:  Seems unary minus being the Loch Ness monster of our forum: tends to appear every once and a while and is almost impossible to get rid of in times where news are scarce.

IIRC, we had a very similar thread recently. Anyway (and once more):
-2^2 = (-1)*2^2 = (-1)*4 = -4 , unambiguously.

it's always unambiguous when it's done your way, isn't it?
Your algebra just shows that you choose to assign a precedence identical to multiplication, but I wasn't looking for a proof, but for any reasons that could push you to choose one or the other way, because we could have a system flag that allows you to choose one way or the other if there's enough reason to justify that.
Everybody seems to stick to one rigid position, while some people keep getting confused by this, so is it justified to give the option? Or such an option would cause even more confusion?

Claudio
07-22-2014, 07:56 AM
Post: #4
 Thomas Radtke Senior Member Posts: 730 Joined: Dec 2013
RE: Unary minus precedence preference
(07-21-2014 09:13 PM)Claudio L. Wrote:  [...] I wasn't looking for a proof, but for any reasons that could push you to choose one or the other way [...]
There is no proof. It's a convention. Once again: Treat an expression "-a" as shortcut to "zero-a" and you're done.
07-22-2014, 06:18 PM
Post: #5
 SlideRule Senior Member Posts: 1,068 Joined: Dec 2013
RE: Unary minus precedence preference
I'm inspired by Captain Father John Francis Patrick Mulcahy;"jocularity...jocularity".

BEST!

SlideRule
07-22-2014, 06:50 PM
Post: #6
 Don Shepherd Senior Member Posts: 688 Joined: Dec 2013
RE: Unary minus precedence preference
(07-22-2014 06:18 PM)SlideRule Wrote:  I'm inspired by Captain Father John Francis Patrick Mulcahy;"jocularity...jocularity".

BEST!

SlideRule

Ah, another M*A*S*H fan.

My favorite quote, from Margaret Houlihan:

Simple respect. I expect nothing more, and I'll accept nothing less.

Priceless.
07-22-2014, 09:09 PM
Post: #7
 walter b On Vacation Posts: 1,957 Joined: Dec 2013
RE: Unary minus precedence preference
(07-21-2014 09:13 PM)Claudio L. Wrote:  Everybody seems to stick to one rigid position, while some people keep getting confused by this, so is it justified to give the option? Or such an option would cause even more confusion?

There are 10 kinds of people: those who understand math and those who don't. If you can't convince them, confuse them ...
07-23-2014, 01:59 AM
Post: #8
 CosmicTruth Member Posts: 164 Joined: May 2014
RE: Unary minus precedence preference
(07-21-2014 08:00 PM)Claudio L. Wrote:  While symbolic object support in newRPL is getting ready to be released in a new demo, I stumbled upon the unary minus precedence ambiguity:

Excel does: -2^2 = 4
while almost everybody else give -2^2= -4

I implemented it as -2^2=-4 for the time being (just to follow the herd), but are there any good reasons to choose one precedence or the other?

Are there any other ambiguities that I need to be aware of regarding other operators?

Claudio

A way to remember the order of operations is to use the acronym PEMDAS Observe that PEMDAS is formed using the first letter of each of the above operators. (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction). Even tho 50g says no, you gotta subtract last.

Thanks
~~~~8< Art >8~~~~

PS: Please post more 50G stuff :)
07-23-2014, 02:36 AM (This post was last modified: 07-23-2014 02:37 AM by John R.)
Post: #9
 John R Member Posts: 101 Joined: Dec 2013
RE: Unary minus precedence preference
(07-22-2014 07:56 AM)Thomas Radtke Wrote:  There is no proof. It's a convention.

That said, it's a very useful convention, symbolically speaking. It seems that if one chose the opposite convention, making $$-2^2=4$$, then consistency would demand that $$-x^2=x^2$$ for all $$x$$. Algebraic expressions would then be fraught with many more parentheses than we are used to, with $$-(x^2)$$ being the expression required to denote the negation of $$x^2$$, for instance.

John
07-23-2014, 03:14 AM
Post: #10
 htom trites Junior Member Posts: 33 Joined: Dec 2013
RE: Unary minus precedence preference
Do you take -2^2 to mean: (-2)^2, -(2^2), or 0-(2^2)?

I've always taught that unary minus is the last (and should be parsed as if it was that by a computer), even though it's usually written as the second.

(I admit I came to this conclusion writing a compiler that emitted code for both one's and two's complement ALUs.)
07-23-2014, 03:40 AM
Post: #11
 ttw Member Posts: 191 Joined: Jun 2014
RE: Unary minus precedence preference
I suppose one could consider the expressions:

A: x-x^2
B: x-(x^2)

and decide which should be default.
07-23-2014, 04:16 AM
Post: #12
 John R Member Posts: 101 Joined: Dec 2013
RE: Unary minus precedence preference
(07-23-2014 03:14 AM)htom trites Wrote:  Do you take -2^2 to mean: (-2)^2, -(2^2), or 0-(2^2)?

I've always taught that unary minus is the last (and should be parsed as if it was that by a computer), even though it's usually written as the second.

(I admit I came to this conclusion writing a compiler that emitted code for both one's and two's complement ALUs.)

In the context of a compiler, it needs to be dependent on the standard of the language in question. Dig far enough into any language standard and you should find (1) precedence rules for the exponentiation and unary minus operators, and (2) a precise definition of what is meant by the unary minus.

The C standard sidesteps this issue in part by not providing a built-in exponentiation operator. It has this to say about the unary minus: "The result of the unary - operator is the negative of its (promoted) operand." This may leave some room for interpretation and implementation of "negative," but this is generally well-defined in the context of established binary integer representations and floating-point standards.

So, again, it essentially comes down to a convention. And any language that does not specify a clear-cut convention for operations like -2^2 is certainly asking for trouble!

John
07-23-2014, 12:08 PM (This post was last modified: 07-23-2014 01:40 PM by Wes Loewer.)
Post: #13
 Wes Loewer Member Posts: 227 Joined: Jan 2014
RE: Unary minus precedence preference
(07-21-2014 08:00 PM)Claudio L. Wrote:  While symbolic object support in newRPL is getting ready to be released in a new demo, I stumbled upon the unary minus precedence ambiguity:

Excel does: -2^2 = 4
while almost everybody else give -2^2= -4

I implemented it as -2^2=-4 for the time being (just to follow the herd), but are there any good reasons to choose one precedence or the other?

I'd say the best reason for choosing -4 is because that's the one that is correct. :-)

Every math textbook will indicate that -2^2 = -4. Yes, there are programmers that have chosen to make their product produce a result of 4, but the standard convention is -4.

I like this guy's slant on it:
http://lpsolit.wordpress.com/2011/05/17/...y-to-them/

Interesting that MS-Excel did not follow the then de-facto standard Lotus 1-2-3 in this. Lotus 1-2-3, along with Quattro-Pro, Lotus Improv, Apple-Works, and even MS-Works all used the standard behavior. I can't help but wonder if it was a programmer error that then had to be maintained for compatibility. Now that Excel is the de-facto standard, OpenOffice/LibreOffice, Google Docs, and others feel the pressure to follow suit.

(07-21-2014 08:00 PM)Claudio L. Wrote:  Are there any other ambiguities that I need to be aware of regarding other operators?
Well, there's always a^b^c. Some treat it as (a^b)^c (Casio & older TI calculators) while others use a^(b^c) (HP in Algebraic mode & newer TI calculators). This one doesn't have a standard mathematical equivalent since the ^ character is not used in math for exponentiation, but a^(b^c) more likely matches what the user intended since the rising superscript fonts are evaluated from the top down.

-wes

[minor correction: The site http://macnauchtan.com/pub/precedence.html indicated that MS-Works did not behave the same as Excel, but when I tried versions 2.0 (DOS) and 4.5a (Win95) I found that they did behave like Excel.]
07-23-2014, 01:45 PM
Post: #14
 Claudio L. Senior Member Posts: 1,682 Joined: Dec 2013
RE: Unary minus precedence preference
(07-23-2014 02:36 AM)John R Wrote:
(07-22-2014 07:56 AM)Thomas Radtke Wrote:  There is no proof. It's a convention.

That said, it's a very useful convention, symbolically speaking. It seems that if one chose the opposite convention, making $$-2^2=4$$, then consistency would demand that $$-x^2=x^2$$ for all $$x$$. Algebraic expressions would then be fraught with many more parentheses than we are used to, with $$-(x^2)$$ being the expression required to denote the negation of $$x^2$$, for instance.

Finally, a well funded response. So one good reason to choose -2^2=-4 is to avoid unnecessary parenthesis and the ambiguity of:
x-x^2 producing a different result from -x^2+x.
Notice the first minus is a subtraction, while the second minus becomes a unary minus just by commutativity, so they are two different operators but the expressions are mathematically equivalent and should give the same result.
This alone is reason enough to discard the other option as "incorrect", as a CAS cannot give 2 different results just for swapping terms in the expression.
Thank, John, your argument is enough to convince me that there should NEVER be an option to do -2^2=4, because when used in more complex expressions it has disastrous consequences.

Claudio
07-23-2014, 02:05 PM
Post: #15
 Claudio L. Senior Member Posts: 1,682 Joined: Dec 2013
RE: Unary minus precedence preference
(07-23-2014 12:08 PM)Wes Loewer Wrote:  Well, there's always a^b^c. Some treat it as (a^b)^c (Casio & older TI calculators) while others use a^(b^c) (HP in Algebraic mode & newer TI calculators). This one doesn't have a standard mathematical equivalent since the ^ character is not used in math for exponentiation, but a^(b^c) more likely matches what the user intended since the rising superscript fonts are evaluated from the top down.

Yet there doesn't seem to be a big disagreement about this case. I found exponentiation to be right-associative everywhere I looked, so even though there is an ambiguity, the convention is clear and followed by almost everybody nowadays (perhaps a few years back it was different, but today everybody seems to agree).

-wes

(07-23-2014 12:08 PM)Wes Loewer Wrote:  [minor correction: The site http://macnauchtan.com/pub/precedence.html indicated that MS-Works did not behave the same as Excel, but when I tried versions 2.0 (DOS) and 4.5a (Win95) I found that they did behave like Excel.]

I like how the cases are cataloged as "safe for engineers" or not. For mathematicians and students this might be a just a nuisance, but for us engineers it can be dangerous (literally).

Claudio
07-23-2014, 05:57 PM
Post: #16
 Tim Wessman Senior Member Posts: 2,228 Joined: Dec 2013
RE: Unary minus precedence preference
Just a note to further back things up, on Prime the priority for the unary minus falls immediately below ^ between MOD and * /

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
07-23-2014, 07:59 PM
Post: #17
 Wes Loewer Member Posts: 227 Joined: Jan 2014
RE: Unary minus precedence preference
(07-23-2014 01:45 PM)Claudio L. Wrote:  Finally, a well funded response. So one good reason to choose -2^2=-4 is to avoid unnecessary parenthesis and the ambiguity of:
x-x^2 producing a different result from -x^2+x.
Notice the first minus is a subtraction, while the second minus becomes a unary minus just by commutativity, so they are two different operators but the expressions are mathematically equivalent and should give the same result.
This alone is reason enough to discard the other option as "incorrect", as a CAS cannot give 2 different results just for swapping terms in the expression.

I completely agree with you, but let me play devil's advocate here. One could argue that if -2^2=4, it would avoid the unnecessary parentheses in (-2)^2. Similarly, one could argue that -x^2 + x turned around is actually x + -x^2 which doesn't avoid the ambiguity.

My point is simply that it really boils down to convention. Never-the-less, I'm going to use your example in class next time as a way of convincing students that the standard convention makes sense. :-)
07-23-2014, 08:27 PM
Post: #18
 Wes Loewer Member Posts: 227 Joined: Jan 2014
RE: Unary minus precedence preference
(07-23-2014 02:05 PM)Claudio L. Wrote:  Yet there doesn't seem to be a big disagreement about this case. I found exponentiation to be right-associative everywhere I looked, so even though there is an ambiguity, the convention is clear and followed by almost everybody nowadays (perhaps a few years back it was different, but today everybody seems to agree).

Well, except for that pesky Excel again. It evaluates 2^3^2 to be 64 instead of 512.

In class I have fun with students entering 2^3^2. Those with older TI-84+ models get 64 while newer TI-84+ models get 512. It always leads to an interesting discussion. That's how I convince the owners of the older ones to upgrade their OS. (The newer OS has a "Classic" Mode which reverts to the old behavior.)
07-23-2014, 09:10 PM
Post: #19
 Claudio L. Senior Member Posts: 1,682 Joined: Dec 2013
RE: Unary minus precedence preference
(07-23-2014 07:59 PM)Wes Loewer Wrote:  My point is simply that it really boils down to convention. Never-the-less, I'm going to use your example in class next time as a way of convincing students that the standard convention makes sense. :-)

You should do a poll, and ask the class what's the result of the following expressions:
4-2^2= (I bet 100% of the students will say it's zero)
-2^2 + 4 = (I think 100% of them will say it's zero as well).
I think if you show first 4-2^2=0, 100% will see the similarity of the expressions written on the board and will immediately agree on the zero result of the second one.
Now try reversing the order (to a different group of students):
-2^2+4 = goes first (I think some students will say 0 and some will say 8)
4-2^2 = (100% will agree it's zero)

I think only in the second group, when you show the ambiguous one first (by itself), the confusion will appear.

Sorry, now we are doing social experiments but would be interesting to know how students perceive the formula just by looking at it.
Claudio
07-23-2014, 10:10 PM
Post: #20
 htom trites Junior Member Posts: 33 Joined: Dec 2013
RE: Unary minus precedence preference
This really comes down to precedence and associations. Part of the reason that 0-2^2 "feels like" -4 is that our minds bind the - as a subtraction (which it is, it's not unary!)

The conflict is that unary - , to be truly unary, has to bind as or higher than parentheses; -2^2 has to become (-2)^2.

So there is no unary -, it's just a writing convention of deleting the 0.
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)