(-) precedence - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not remotely HP Calculators (/forum-9.html) +--- Thread: (-) precedence (/thread-19269.html) |
(-) precedence - jhallen - 12-08-2022 01:10 PM 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! RE: (-) precedence - Thomas Okken - 12-08-2022 01:30 PM -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. RE: (-) precedence - Eddie W. Shore - 12-08-2022 01:41 PM The unary minus versus the subtraction operator should be address in school when discussing order of operations. RE: (-) precedence - jhallen - 12-08-2022 01:47 PM (12-08-2022 01:30 PM)Thomas Okken Wrote: -2^2 = -4, that is the only correct answer. 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. RE: (-) precedence - Maximilian Hohmann - 12-08-2022 02:21 PM 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 RE: (-) precedence - KeithB - 12-08-2022 02:38 PM 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." RE: (-) precedence - Maximilian Hohmann - 12-08-2022 03:38 PM Out of curiosity I typed -2^2 into the Google search field. Google adds parentheses on it's own and displays: -(2^2)=-4 RE: (-) precedence - KeithB - 12-08-2022 04:01 PM 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...) RE: (-) precedence - Thomas Okken - 12-08-2022 05:43 PM 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. RE: (-) precedence - jhallen - 12-08-2022 06:27 PM (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. 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. RE: (-) precedence - Thomas Okken - 12-08-2022 06:39 PM (12-08-2022 06:27 PM)jhallen Wrote: It sounds like you are denying the reality of negative numbers... What? RE: (-) precedence - jhallen - 12-08-2022 06:49 PM (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... 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." RE: (-) precedence - Thomas Okken - 12-08-2022 07:04 PM 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. RE: (-) precedence - Albert Chan - 12-08-2022 07:18 PM 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) */ -2 ^ 2 will be parsed as '-' '2' '^' '2' Then, from lua precedence table, it converted to ( - (2 ^ 2)) RE: (-) precedence - Albert Chan - 12-08-2022 07:33 PM 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) RE: (-) precedence - Massimo Gnerucci - 12-08-2022 07:56 PM (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. 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. RE: (-) precedence - Matt Agajanian - 12-08-2022 08:25 PM (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. 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)? RE: (-) precedence - Thomas Okken - 12-08-2022 09:21 PM 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. RE: (-) precedence - Matt Agajanian - 12-08-2022 09:47 PM (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! RE: (-) precedence - Matt Agajanian - 12-08-2022 10:02 PM (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? |