Thread Closed 
(-) precedence
12-08-2022, 01:10 PM (This post was last modified: 12-08-2022 01:27 PM by jhallen.)
Post: #1
(-) precedence
All AOS, RPN and RPL calculators: 2 (+/-) x^2 gives 4.

HP-33s in algebraic mode: 2 (+/-) x^2 gives 4. This calculator is interesting- entry is like AOS, but it displays the entered expression in EOS format and delays the result until you press enter (on a classic AOS calculator, after typing "2 * 3 *" you would have "6 *", but on HP-33s you have "2*3*", but otherwise it's like AOS). On HP-33s, you can't edit the entered expression.

TI-68 and TI Galaxy 67 (EOS): (-) 2 x^2 gives 4. These are the only EOS ones I know of that do it correctly (maybe some lower end ones in this series are also good). Are there any others?

All other EOS calculators (Casios, modern TIs, including HP-35s in algebraic mode): (-) 2 x^2 gives -4. (on HP-35s, you have to use x^y to see this since x^2 generates SQ()).

HP-Prime: 2 (+/-) x^2 gives -4! Crazy!
Find all posts by this user
12-08-2022, 01:30 PM (This post was last modified: 12-11-2022 09:40 AM by Thomas Okken.)
Post: #2
RE: (-) precedence
-2^2 = -4, that is the only correct answer.

I know that, for some reason, among computer types, there is this idea that unary minus should have higher precedence than exponentiation, but that is not how it works in the commonly used mathematical notation.

For example, look up the Taylor series for sine. It is typically shown like this:

\[ \sin(x) = \sum_{i=0}^\infty \frac{(-1)^i}{(2 i + 1)!} x^{2 i + 1}\]

Note those parentheses around the -1. Those are not redundant.
Visit this user's website Find all posts by this user
12-08-2022, 01:41 PM
Post: #3
RE: (-) precedence
The unary minus versus the subtraction operator should be address in school when discussing order of operations.
Visit this user's website Find all posts by this user
12-08-2022, 01:47 PM (This post was last modified: 12-08-2022 02:29 PM by jhallen.)
Post: #4
RE: (-) precedence
(12-08-2022 01:30 PM)Thomas Okken Wrote:  -2^2 = -4, that is the only correct answer.

I know that, for some reason, among computer types, there is this idea that unary minus should have higher precedence than exponentiation, but that is not how it works in the commonly used mathematical notation.

Note those parentheses around the -1. Those are not redundant.

This is true in mathematical notation because subtract and negate use the same symbol. On a calculator, we have two different keys, so this limitation is not required. EOS is really FORTRAN anyway (i.e., squeezing math onto a single teletype or punched-card line), no mathematician writes equations that way.. so why conform on this specific point?

On HP-33s, when you type 2 (+/-) x^2, it displays (-2)^2: very nice for clarity.

One more argument: it's confusing when students are learning substitution that:
f(-1) = x^2 gives a different answer than -1^2


Anyway, it's interesting that TI tried it with TI-68 and Galaxy-67.
Find all posts by this user
12-08-2022, 02:21 PM (This post was last modified: 12-08-2022 02:22 PM by Maximilian Hohmann.)
Post: #5
RE: (-) precedence
Hello!

(12-08-2022 01:41 PM)Eddie W. Shore Wrote:  The unary minus versus the subtraction operator should be address in school when discussing order of operations.

It is a while ago that I left school (1980) but this topic was already addressed back then. But I must say that we had a very computer- and calculator-minded math teacher around the time when calculators started to be allowed for use in classes (1977 onward if I remember well). He used to bring his own private HP-97 to school, which must have cost him a month's salary or even more, to demonstrate calculus and simple programming techniques. So yes, I was made aware of those issues right from the start. When writing code, no matter what language or environment, I always use parentheses in these cases. Who knows which version of what compiler handles it which way? What are two parentheses against a working day lost for troubleshooting!

Regards
Max
Find all posts by this user
12-08-2022, 02:38 PM (This post was last modified: 12-08-2022 02:39 PM by KeithB.)
Post: #6
RE: (-) precedence
Not a new problem!
From Kernighan and Plauger "The Elements of Programming Style":

"A more insidious operator ambiguity occurs in this expression from an arctangent routine:
TERM = TERM*(-X**2)/DENOM

Is X negated and then squared, or squared and then negated? Fortran reference manuals seldom treat such fine points in detail; this may be a hard question to answer without running a test program. As a matter of fact the ANSI standard for Fortran calls for the latter interpretation (fortunate in this case) - the variable X is squared and then negated - but the line should still be rewritten as:
TERM = -TERM * X**2 / DENOM

The first form invites misunderstanding on the part of the reader, if not the compiler. Unless reader and compiler both understand the writer, the program is not communicating properly.

[Rule of Thumb] Parenthesize to avoid ambiguity."
Find all posts by this user
12-08-2022, 03:38 PM
Post: #7
RE: (-) precedence
Out of curiosity I typed -2^2 into the Google search field. Google adds parentheses on it's own and displays: -(2^2)=-4
Find all posts by this user
12-08-2022, 04:01 PM
Post: #8
RE: (-) precedence
Note that keying in 2 +/- is very different from writing -2 in a program. The first essentially creates a variable with the value of -2 while the other throws operator precedence into the mix.

Everyone agrees that :
X= -2
X = X^2
should equal 4

(unless you treat ^ as the exclusive or operator...)
Find all posts by this user
12-08-2022, 05:43 PM
Post: #9
RE: (-) precedence
I think some of the confusion may stem from early textbooks that demonstrated parsers that treated negative numbers as self-contained entities. So, -2 was treated as the number -2, rather than the number 2 to which a unary minus is applied.

If you use the latter approach, and then optimize for negative numbers by transforming "number with unary minus applied" to "negative number" after parsing, then everything is fine, but if you use the former approach, you end up with unary minus having highest priority -- and then you have to make sure that unary minus applied to anything that isn't a number literal behaves the same way, and then you end up with -2^2 being something different than 0-2^2, and that makes mathematicians sad.
Visit this user's website Find all posts by this user
12-08-2022, 06:27 PM (This post was last modified: 12-08-2022 06:29 PM by jhallen.)
Post: #10
RE: (-) precedence
(12-08-2022 05:43 PM)Thomas Okken Wrote:  I think some of the confusion may stem from early textbooks that demonstrated parsers that treated negative numbers as self-contained entities. So, -2 was treated as the number -2, rather than the number 2 to which a unary minus is applied.

If you use the latter approach, and then optimize for negative numbers by transforming "number with unary minus applied" to "negative number" after parsing, then everything is fine, but if you use the former approach, you end up with unary minus having highest priority -- and then you have to make sure that unary minus applied to anything that isn't a number literal behaves the same way, and then you end up with -2^2 being something different than 0-2^2, and that makes mathematicians sad.

It sounds like you are denying the reality of negative numbers...

If (-) is going to be a distinct key from -, maybe it should only be allowed to be applied to numbers, so (-) X is not allowed, but (-) 1 is. But also allow -X or -1 with the usual lower precedence. It would be OK if they are displayed differently..

Alternatively, get rid of the (-) key.. just use -. Sharp pocket computers do this. The ones that have a calculator mode disable the (-) key when they are in BASIC mode.

BTW, -2^2 gives 4 in Excel, but -4 in Matlab.
Find all posts by this user
12-08-2022, 06:39 PM
Post: #11
RE: (-) precedence
(12-08-2022 06:27 PM)jhallen Wrote:  It sounds like you are denying the reality of negative numbers...

What?
Visit this user's website Find all posts by this user
12-08-2022, 06:49 PM
Post: #12
RE: (-) precedence
(12-08-2022 06:39 PM)Thomas Okken Wrote:  
(12-08-2022 06:27 PM)jhallen Wrote:  It sounds like you are denying the reality of negative numbers...

What?

I'm kidding, but you could imply that from "So, -2 was treated as the number -2, rather than the number 2 to which a unary minus is applied."
Find all posts by this user
12-08-2022, 07:04 PM (This post was last modified: 12-08-2022 07:05 PM by Thomas Okken.)
Post: #13
RE: (-) precedence
My point is, when parsing expressions, you have to avoid being too aggressive when trying to parse negative numbers. This shouldn't be hard to understand. Just consider -2^2: you need to understand that there is no negative number there. It's the expression 2^2 to which unary minus is applied.

In the case of 3*-2, on the other hand, treating the -2 as a negative number does make sense. But in terms of implementing a parser, it's just easier to ignore negative numbers.

This is what the Plus42 expression parser does. You can see this in the generated code: -2 ends up as 2 +/-. Of course it would be slightly more efficient to generate -2 instead, and that could be achieved by replacing "number literal with unary minus applied" to "negative number," either by transforming the parse tree, or by performing a peephole optimization step when the code is being emitted.

The bottom line is that "negative number" is a special case that parsers are better off ignoring. Unless you actually want unary minus to have high precedence, of course.
Visit this user's website Find all posts by this user
12-08-2022, 07:18 PM
Post: #14
RE: (-) precedence
Hi, jhallen

Unary minus is stripped from the number, when building tokens
Otherwise, unary minus operator token is lost.

Example, a snippet for lua llex.c
Code:
case '-': {  /* '-' or '--' (comment) */
  next(ls);
  if (ls->current != '-') return '-'; // <--- NUMBER NEVER SEE THE MINUS SIGN
  /* else is a comment */

-2 ^ 2 will be parsed as '-' '2' '^' '2'

Then, from lua precedence table, it converted to ( - (2 ^ 2))
Find all posts by this user
12-08-2022, 07:33 PM
Post: #15
RE: (-) precedence
This is just a guess, but I think all this unary minus stuff orginated from Microsoft Excel.

I had checked Lotus 123. Regarding unary minus, it got it right (-2^2 = -4)
There was no reason for Excel to veer away from the spreadsheet market leader. (back then)

Excel probably get the precedence wrong, but decided to "fix" its documentation, rather than the code.

Just change the precedence table!
It is a feature ... not a bug!

It is all donwhill from here. Others, knowing the flaw, were forced to follow, to be Excel compatible.
As an example, OpenOffice Calc had the same bug (-2^2 = 4)
Find all posts by this user
12-08-2022, 07:56 PM
Post: #16
RE: (-) precedence
(12-08-2022 07:33 PM)Albert Chan Wrote:  This is just a guess, but I think all this unary minus stuff orginated from Microsoft Excel.

I had checked Lotus 123. Regarding unary minus, it got it right (-2^2 = -4)
There was no reason for Excel to veer away from the spreadsheet market leader. (back then)

Excel probably get the precedence wrong, but decided to "fix" its documentation, rather than the code.

Just change the precedence table!
It is a feature ... not a bug!

It is all donwhill from here. Others, knowing the flaw, were forced to follow, to be Excel compatible.
As an example, OpenOffice Calc had the same bug (-2^2 = 4)

Probably because Excel was first developed for the Mac and, you know, Apple has to impose its own standards, on everything.

Just kiddin' guys. No flames, please. Wink

Greetings,
    Massimo

-+×÷ ↔ left is right and right is wrong
Visit this user's website Find all posts by this user
12-08-2022, 08:25 PM
Post: #17
RE: (-) precedence
(12-08-2022 01:47 PM)jhallen Wrote:  
(12-08-2022 01:30 PM)Thomas Okken Wrote:  -2^2 = -4, that is the only correct answer.

I know that, for some reason, among computer types, there is this idea that unary minus should have higher precedence than exponentiation, but that is not how it works in the commonly used mathematical notation.

Note those parentheses around the -1. Those are not redundant.

This is true in mathematical notation because subtract and negate use the same symbol. On a calculator, we have two different keys, so this limitation is not required. EOS is really FORTRAN anyway (i.e., squeezing math onto a single teletype or punched-card line), no mathematician writes equations that way.. so why conform on this specific point?

On HP-33s, when you type 2 (+/-) x^2, it displays (-2)^2: very nice for clarity.

One more argument: it's confusing when students are learning substitution that:
f(-1) = x^2 gives a different answer than -1^2


Anyway, it's interesting that TI tried it with TI-68 and Galaxy-67.

Now I'm confused. How can it be intuited that the - is applied to the entire
2x2, thus -4 and the - is not exclusive to the 2, thus -2? Or are you saying that unary minus is lower in precedence (i.e. 2*2, then negate the answer, 4)?
Find all posts by this user
12-08-2022, 09:21 PM
Post: #18
RE: (-) precedence
Unary minus should have the same precedence as binary minus, that's the way it works in standard mathematical notation. Another way of understanding it is that -x is shorthand for 0-x.
Visit this user's website Find all posts by this user
12-08-2022, 09:47 PM
Post: #19
RE: (-) precedence
(12-08-2022 09:21 PM)Thomas Okken Wrote:  Unary minus should have the same precedence as binary minus, that's the way it works in standard mathematical notation. Another way of understanding it is that -x is shorthand for 0-x.

Good point!
Find all posts by this user
12-08-2022, 10:02 PM
Post: #20
RE: (-) precedence
(12-08-2022 09:21 PM)Thomas Okken Wrote:  Unary minus should have the same precedence as binary minus, that's the way it works in standard mathematical notation. Another way of understanding it is that -x is shorthand for 0-x.

So, if I understand you correctly, X*X first, then change sign. That makes sense to me since MDAS (since multiplication has higher precedence.
Next, so I understand correctly, every time I see a unary minus, if the next operator is higher in precedence *, /, y^x, etc.), do that and then change sign. Right?
Find all posts by this user
Thread Closed 




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