Post Reply 
Unary minus precedence preference
07-24-2014, 01:57 AM
Post: #21
RE: Unary minus precedence preference
(07-23-2014 10:10 PM)htom trites Wrote:  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.

Regarding the existence or not of the unary minus operator, I think in computing the unary minus is very real. I don't know if it meets your standards to be "truly unary" but it is real enough for all HP calculators to have a key dedicated to the unary minus (NEG, +/-, CHS), and a separate one for the binary minus.
And more so thinking in RPN, where there's no ambiguity, no operator precedence, and not even algebraic expressions. Yet the unary minus exists by itself.
I could also point out that in two's complement, the unary minus is not defined as (0-x), but it's defined as (~x)+1, where ~ is an operator that toggles all bits (XOR 0xFFFFFFF...). Granted, with the properly defined subtraction in the same domain, 0-x should give the same result as -x, but the definition of the unary minus exists by itself and is independent from the binary minus. And in hardware where you have only adders, it's common to define the binary minus in terms of the unary minus:
a-b = a+(-b) = a+(~b)+1
So in that case, the unary minus exists, but the binary minus doesn't!
And I guess you could generalize that and say that the subtraction doesn't exist, it's actually the addition of a negative number.
It's all a matter of perspective.

Claudio
Find all posts by this user
Quote this message in a reply
07-24-2014, 07:31 AM
Post: #22
RE: Unary minus precedence preference
(07-23-2014 08:27 PM)Wes Loewer Wrote:  Well, except for that pesky Excel again. It evaluates 2^3^2 to be 64 instead of 512.

In fact, the only spreadsheets that I've found so far that give 512 are Google Sheets and Gnumeric on Linux. Gnumeric is even kind enough to automatically insert parentheses to explicitly show 2^(3^2) as well as (-2)^2.

So, a few spreadsheets do -2^2 correctly and a few do 2^3^2 correctly, but I'm not aware of any spreadsheets that do both of these operations correctly.
Find all posts by this user
Quote this message in a reply
07-24-2014, 08:53 AM
Post: #23
RE: Unary minus precedence preference
(07-24-2014 07:31 AM)Wes Loewer Wrote:  So, a few spreadsheets do -2^2 correctly and a few do 2^3^2 correctly, but I'm not aware of any spreadsheets that do both of these operations correctly.

I suppose this has to do with the intended audience of a spreadsheet application: It's the same group of people who set their business calculators to chain mode. Don't expect anything more complicated from them. Wink

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
Find all posts by this user
Quote this message in a reply
07-24-2014, 09:01 AM
Post: #24
RE: Unary minus precedence preference
(07-24-2014 08:53 AM)Marcus von Cube Wrote:  
(07-24-2014 07:31 AM)Wes Loewer Wrote:  So, a few spreadsheets do -2^2 correctly and a few do 2^3^2 correctly, but I'm not aware of any spreadsheets that do both of these operations correctly.

I suppose this has to do with the intended audience of a spreadsheet application: It's the same group of people who set their business calculators to chain mode. Don't expect anything more complicated from them. Wink

Smile As mentioned above already. Wink

d:-)
Find all posts by this user
Quote this message in a reply
07-24-2014, 01:35 PM
Post: #25
RE: Unary minus precedence preference
(07-24-2014 07:31 AM)Wes Loewer Wrote:  
(07-23-2014 08:27 PM)Wes Loewer Wrote:  Well, except for that pesky Excel again. It evaluates 2^3^2 to be 64 instead of 512.

In fact, the only spreadsheets that I've found so far that give 512 are Google Sheets and Gnumeric on Linux. Gnumeric is even kind enough to automatically insert parentheses to explicitly show 2^(3^2) as well as (-2)^2.

So, a few spreadsheets do -2^2 correctly and a few do 2^3^2 correctly, but I'm not aware of any spreadsheets that do both of these operations correctly.

It's sad that developers are so careless.
Here's a quote from the XLS format specification:
Quote:With the exception of control tokens (section 2.2.2.3), display tokens (section 2.2.2.4), and mem
tokens (section 2.2.2.5) that are described in subsequent sections, parsed expressions are stored in
Rgce (section 2.5.198.104) using Reverse-Polish notation.

So it's clear that Excel uses RPN to store and evaluate expressions. This means that OpenOffice, Gnumeric, etc. could choose the other convention without breaking compatibility with existing spreadsheets (since existing expressions are pre-compiled to RPN, there's no ambiguity!!). They could simply open an old spreadsheet written with Excel and the same formula would display (-2)^2 under the "correct" convention, but will always give the same result of 4. When the user types -2^2 on a new formula, give -4. When you go back to Excel and open that file, Excel will display -(2^2) (under the "wrong" convention) and keep giving the -4 result you got before. Nothing really breaks, so I don't understand their reasoning.
Same thing for the associativity of exponents.

Claudio
Find all posts by this user
Quote this message in a reply
07-24-2014, 01:53 PM
Post: #26
RE: Unary minus precedence preference
XLSX stores formulas as text... No preconpiled RPN expressions whatsoever in recent versions of the Excel file format.

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
Find all posts by this user
Quote this message in a reply
07-24-2014, 04:11 PM
Post: #27
RE: Unary minus precedence preference
I'll start by saying that I agree that -2^2 should be -4.

Now some philosophical thoughts. It seems to me that the problem is that we use "-" for at least 3 different operations:
  1. subtraction: a - b
  2. unary minus: -x is shorthand for 0-x
  3. specifier for negative numbers: -5 is a number on the number line.


The last one may require some explanation: I'm saying that in this case, the "-" is part of the number, not an operation. The same is true of the decimal mark: in "5.2", the "." isn't an operation, it's part of the way we specify the number itself.


To see the difference, consider the following on a 50g:
Code:
X [ENTER] 5 [+/-] -   ---> 'X--5'
In other words, this is the variable X minus a real number (negative 5)

Or the strange case where sometimes numbers must be enclosed in parenthesis:
5 X Y^X [ENTER] -> '5^X'
5 [+/-] X Y^X [ENTER] ---> '(-5)^X'

This means that decompiling symbolic expressions is hard: negative numbers decompile differently depending on the operations they're involved in.

All of this would be easier of we used different symbols. Suppose:
  • negative numbers are specified with prefix "n". e.g. n5 means -5
  • subtraction is the negative sign: -
  • unary minus is "um"

Now nX^2 isn't a valid math expression. Neither is -X^2 since "-" is a binary operator. umX^2 is the original problem. This is NOT subtraction, so its precedence isn't specified by the PMDAS rules. Mathmatically, it would seem to come between Multiplication/Division and Addition/subtraction, so school children would be taught PMDUAS. Smile

What about 4-2^2 vs -2^2+4? In the new notation it's clear that you're using different operations here: 4-2^2, 4+n2^2 and 4+um2^2 are all different expressions (the latter simplifies to 4-2^2).
Find all posts by this user
Quote this message in a reply
07-25-2014, 03:15 AM
Post: #28
RE: Unary minus precedence preference
All the above just reinforces my love for RPN. My calculators are never in any confusion about what I mean. Wink

--- Les
[http://www.lesbell.com.au]
Visit this user's website Find all posts by this user
Quote this message in a reply
07-25-2014, 11:26 AM
Post: #29
RE: Unary minus precedence preference
(07-24-2014 04:11 PM)David Hayden Wrote:  school children would be taught PMDUAS. Smile

Please excuse my dear unmarried aunt Sally?

Poor kids!
Find all posts by this user
Quote this message in a reply
07-29-2014, 07:25 PM (This post was last modified: 07-29-2014 07:46 PM by Eddie W. Shore.)
Post: #30
RE: Unary minus precedence preference
-8^2 returns -64 but -8 * -8 returns 64:

-8 * -8
(0 - 8) * (0 - 8)
—8 * —8 // — used as a negative sign.
—1 * 8 * —1 * 8
64

I don't recall any calculator manual mentioning that the [ - ] really meant unary subtraction. That key was always labeled negation. Thankfully, manuals do state that this operation is executed after powers and roots in the order of operations.

We also have the problem of inverse functions. Clearly, when the user presses [ - ] [ 8 ] [x^2], the user was not intending to enter an imaginary number. But on software with algebraic entry:

-8^2 returns 64
SQRT(Ans) returns 8*i or Error if complex mode is turned off or not available.

Personally, I prefer if [ - ] just negates the number and make it part of the number.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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