Post Reply 
Negative numbers with exponents in RPN
01-27-2021, 07:06 PM
Post: #1
Negative numbers with exponents in RPN
I recently switched from a TI-Nspire to HP Prime after the former got stolen from my car. As such I am learning how to use RPN and I have been trying to figure out how I would execute something like this in RPN
-2^2

When I try this:
2
+/-
x^2
it gives me 4 which is essentially (-2)^2 and not -2^2 which would come out as -4

Am I doing something wrong or is it not possible on RPN to do -2^2?
Find all posts by this user
Quote this message in a reply
01-27-2021, 07:45 PM
Post: #2
RE: Negative numbers with exponents in RPN
The appropriate sequence of steps for this is:

2
x^2
+/-


It probably helps to think of this as -(2^2).

When you apply the negative first you are getting the interim result of -2, which is 4 when squared. So you are calculating (-2)^2, as you noted.
Find all posts by this user
Quote this message in a reply
01-27-2021, 07:57 PM (This post was last modified: 01-27-2021 07:59 PM by ijabbott.)
Post: #3
RE: Negative numbers with exponents in RPN
It helps to think in RPN:

+/- negates the number in stack position 1

x^2 squares the number in stack position 1

Obviously, the answer depends on the order in which those operations are applied.

— Ian Abbott
Find all posts by this user
Quote this message in a reply
01-27-2021, 07:59 PM
Post: #4
RE: Negative numbers with exponents in RPN
Further testing this on the Prime I calculated 5-7 (using textbook mode). Then I copied the result of this calculation to the command line and applied x^2. The result was -4. So the prime is applying the square inside the negative symbol on a result that is clearly a negative number.

The Prime is not the only calculator I tried that made the same error.
Find all posts by this user
Quote this message in a reply
01-27-2021, 11:58 PM (This post was last modified: 01-28-2021 12:02 AM by trojdor.)
Post: #5
RE: Negative numbers with exponents in RPN
(01-27-2021 07:59 PM)john gustaf stebbins Wrote:  Further testing this on the Prime I calculated 5-7 (using textbook mode). Then I copied the result of this calculation to the command line and applied x^2. The result was -4. So the prime is applying the square inside the negative symbol on a result that is clearly a negative number.

The Prime is not the only calculator I tried that made the same error.

In your example....if you first hit enter after you copy the result to the command line, you can then use ANS^2 to get the correct result.

You can also skip the entire copy/paste process by using the ANS key directly:
(textbook mode)
5-7 enter (shows -2)
ANS^2 (shows 4)

Interestingly, this (the ANS key) trick works correctly on HP, TI and Casio...

ENTER > =
Find all posts by this user
Quote this message in a reply
01-28-2021, 12:08 AM (This post was last modified: 01-28-2021 12:19 AM by ijabbott.)
Post: #6
RE: Negative numbers with exponents in RPN
(01-27-2021 07:59 PM)john gustaf stebbins Wrote:  Further testing this on the Prime I calculated 5-7 (using textbook mode). Then I copied the result of this calculation to the command line and applied x^2. The result was -4. So the prime is applying the square inside the negative symbol on a result that is clearly a negative number.

The Prime is not the only calculator I tried that made the same error.

So you selected the result -2, copied it into the clip buffer, changed to RPN entry mode, pasted the -2 into the command line, pressed the x^2 key and got the answer -4? Because I just did that and got the answer 4 as expected.

EDIT: Perhaps you did the pasting and squaring in textbook or algebraic mode? In those modes, -2 x^2 will produce the result -4 because the x^2 operation has higher precedence than the negative sign. One of the advantages of RPN is that there are no operator precedence rules.

— Ian Abbott
Find all posts by this user
Quote this message in a reply
01-28-2021, 07:18 AM
Post: #7
RE: Negative numbers with exponents in RPN
Quote:Perhaps you did the pasting and squaring in textbook or algebraic mode? In those modes, -2 x^2 will produce the result -4 because the x^2 operation has higher precedence than the negative sign. One of the advantages of RPN is that there are no operator precedence rules.

Yes this is the mode I was working in. It means that when I copy a previous result, it gets edited by the default entry mode. The system should have invisible parenthesis or even visible ones when you copy so that the result is not messed with by the editor.
Find all posts by this user
Quote this message in a reply
01-28-2021, 08:36 AM
Post: #8
RE: Negative numbers with exponents in RPN
(01-28-2021 07:18 AM)john gustaf stebbins Wrote:  
Quote:Perhaps you did the pasting and squaring in textbook or algebraic mode? In those modes, -2 x^2 will produce the result -4 because the x^2 operation has higher precedence than the negative sign. One of the advantages of RPN is that there are no operator precedence rules.

Yes this is the mode I was working in. It means that when I copy a previous result, it gets edited by the default entry mode. The system should have invisible parenthesis or even visible ones when you copy so that the result is not messed with by the editor.

I see what you are getting at. The copy and paste functions are really just copy and pasting text but you want it to be pasted as an isolated sub-expression surrounded by parentheses when necessary. "Hidden" parentheses would be a bad idea though. What you see on the input line is what you should get.

— Ian Abbott
Find all posts by this user
Quote this message in a reply
01-28-2021, 12:28 PM
Post: #9
RE: Negative numbers with exponents in RPN
(01-28-2021 12:08 AM)ijabbott Wrote:  One of the advantages of RPN is that there are no operator precedence rules.

This is also RPN's disadvantage. Precedence rules did not disappear.
The burden just get shifted to the user, instead of a machine.

-2^2 ==> RPN steps: 2 [X^2] [CHS]

(01-28-2021 08:36 AM)ijabbott Wrote:  I see what you are getting at. The copy and paste functions are really just copy and pasting text but you want it to be pasted as an isolated sub-expression surrounded by parentheses when necessary. "Hidden" parentheses would be a bad idea though. What you see on the input line is what you should get.

Explicit parentheses might be a good idea, especially with complex numbers.

I just patched Lua complex.dll to do just that. (Python already do this)

lua> I = require'complex'.I
lua> 1e154+2e154*I
(1e+154+2e+154*I)
lua> 2e154+1e154*I
(2e+154+1e+154*I)

Divide above complex numbers by cut/paste operation.

lua> (1e+154+2e+154*I) / (2e+154+1e+154*I)
(0.8+0.6000000000000001*I)
Find all posts by this user
Quote this message in a reply
01-31-2021, 05:08 PM (This post was last modified: 01-31-2021 05:13 PM by Peet.)
Post: #10
RE: Negative numbers with exponents in RPN
The easiest way to understand RPN is to imagine how to calculate in your head or you do it on paper.

a) if you want to calculate -2^2 then first you take 2^2 and then you set the "-" sign
in RPN: [2] [X^2] [+/-] and you get -4

b) if you want to calculate (-2)^2 then you have to take -2 an then build the square
in RPN: [2] [+/-] [X^2] and get 4

At RPN, always calculate in the exact order a person would calculate, not as it was written down.

e.g. if you have √(2+2) than you first take the numbers 2 and 2, add them together an than build the square root
in RPN: [2] [enter] [2] [+] [√]

My calculators - former: CBM PR100, HP41CV, HP11C, HP28S - current: HP48G, HP35S, Prime, DM41X, DM42, HP12C
Find all posts by this user
Quote this message in a reply
Post Reply 




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