The basis of RPN - Chapter 1

go back to Tutorials page
You won't learn how to use RPN-Enh here, please go back to main tutorial page if you already know rpn.

lets start, lets compare

Most of you know how to use numeric calculator for many year now, you know that pressing 2 + 3 = gives 5.
That looks totaly obvious..
When you want to perform sin(2), generaly, you start entering 2 and then press sin button. In this case you first enter the argument and then the function. this is less obvious..

The idea of RPN (Reverse Polish Notation) is to use always the same way to enter expression. (first the arguments, and then the function)
2+3= will be entered as follows 2 3 +
and sin(2) will be entered as follows 2 sin
The + and the sin functions/operands are used after the values/arguments.

You may find it very strange. Every one find it strange at first, but I can promise you that's a very efficient way of working for many reasons..
You can totaly avoid parentesis with this method, and if you think carfully, that's also how your brain compute an expression. Let's see the following expression :
your brains manipulation on a RPN calculator
Your brain, (normally) start spliting numerator (2+4)*(6+8) and denominator (5-7)
You brain start (for example) with numerator, there are two expression : 2+4 and 6+8,
it starts computing each of them
It compute 2+4 it gives 6 2 4 +

your stack display now 6, the result of 2+4
and now it compute 6+8, it gives 14 6 8 +

you stack display now 14 and 6, look how it keep all previous result.
Now it multiply the result of the two previous operations 6*14 gives 84 *

yes, we just need to press one key : *
the stack now display only 84, the 6 and 14 have been consumed by multiply.
Now (we keep 84 in mind) it keeped on the stack, naturally.
Let start with 5-7 5 7 -
it gives, -2 as expected.
now on the stack, we have 84 and -2
and finally we divide 84 by -2. /

that's it, you get -42 as result.
You see, the way your brain is computing (2+4)*(6+8)/(5-7) is very similar the way we use RPN.
You always see intermediate results in the stack, you are sure you never make mistake, there are no parenthesis, you exactly know what you are doing, you are not using a numeric calculator as dummies can do.


ok, lets take a look at some basic calulation examples :
Expression on a classic calulator on any RPN calculator
1+2 1 + 2 =
4 keystrokes
1 enter 2 +
4 keystrokes
(1+2)/3 typing 1+2/3= is not safe, you have to use parethesis
( 1 + 2 ) / 3 =
7 keystrokes
1 enter 2 + 3 /
6 keystrokes
you can see that in RPN, to enter serveral numbers, you have to use enter as separator :
1 enter 2 enter 1 and 2 in the stack. you can use the space key instead
1+2+3+4 1 + 2 + 3 + 4 =
8 keystrokes
1 enter 2 + 3 + 4 +
8 keystrokes
sin(2) 2 SIN
2 keystrokes
2 keystrokes
sin(1+1) SIN ( 1 + 1 )
6 keystrokes (more and less, you may need to add = ,or maybe first parenthesis is already placed)
1 enter 1 + SIN
4 keystrokes
in RPN, argument(s) must always be placed before the operator, The following example demonstrates the difference between the RPN and non-RPN calculator.
sqr( (3*4) )
sqr( (3*4)+1 )
SQR ( 3 * 4 ) = : you have first result here
SQR ( ( 3 * 4 ) + 1 ) =
18 keystrokes
3 enter 4 * DUP SQR : you have first result here
9 keystrokes

It dupplicate intermediate result (3*4) for later use, to dupplicate something I introduce here the DUP command, to do a DUP, just press the enter key.

we just continue our calculation, we compute SQR of 12...
now on stack, we have : 12 and 3.46 (sqr(12))
Now, we want to use 12, we need to swap 12 and 3.46, to do that, lets introduce the SWAP command (in RPN-Enh and MiniRPN, use the RIGHT-Arrow key)
after the SWAP, the stack look like :
we continue by adding 1 : 1 +, and performing the SQR of 13. now the stack looks like :
You have on your screen, the both results : sqr(3*4) and sqr ((3*4)+1).

You can now do what you want with these numbers,
to divide them, just press the / key.

The ENTER key

If you have follows the previous example, you have notified that the enter key is used to enter a number or to dupplicate it.
the rule is very simple :
  • If you are entering a number, it validate it onto the stack.
  • If you are not entering a number, it duplicates the object in level 1

  • For example
    Press : 1 2 3 enter enter
    The first enter validate 123, and the second dupplicate it.
    you will have on stack 123 and 123.


    The swap is very usefull to altern two number into stack. you usualy find this function by pressing RIGHT arrow key.
    ex : 1 enter 2 enter swap


    this function delete the last item of the stack.
    you can usualy find it with the BackSpace key

    ex : 1 enter 2 enter DROP
    now , 2 have been deleted, you just have 1 in the stack.

    End of first chapter

    You have now reached the end of this first chapter tutorial.
  • you can now compute simple calculation (like sin(3*4) or (41+3)/(5-9)...)
  • you are able to use DUP SWAP and DROP commands
  • In the next tutorial, you will learn what is the stack and how to use more stack command (like DUP2 ROT), how to handle lists or matrix...

    Let's continue with Chapter 2.

    go back to Tutorials Main Page