The RPN Calculator

Alcula’ has finally a simple RPN calculator. It’s a very simple small program entirely written in Javascript that loads lightning-fast, ideal for slow internet connections. This article describes how it works.

What is RPN?

RPN stands for Reverse Polish Notation. It is a standard for writing expressions based on Polish Notation, a concept originally invented by Jan Ɓukasiewicz, a Polish mathematician. In the fifties, it was used by scientists as the base for a very important concept in computer science: the memory stack.

For the purpose of understanding Reverse Polish Notation, we can represent the stack as a pile of objects – imagine sheets of paper – where we can write numbers. We can add papers to the top of the stack, we can read or write to the top sheet, remove the top sheet or add a new sheet to the top. We cannot access directly the sheets that are underneath, without first removing the ones that lie above them. This concept is summarized with the acronym LIFO: Last In, First Out.

RPN, uses this data structure to store the operands (the numbers) of an expression. Operators (+, -, etc.) are applied to the operands that are stored in the stack. To enter an expression, we must first put the operands in the stack, then specify the operator to apply. Instead of entering an expression like this: 2 + 3 (operand, operator, operand), we enter it like this: 2 3 + (operand, operand,operator). For this reason, Reverse Polish Notation is sometimes referred to as ‘Postfix Notation‘.

The operation of adding a number to the stack is called ‘push‘. The operation of removing a number from the top of the stack is called ‘pop‘.

The Advantages of RPN

RPN allows us to type complex expressions without ever needing to use parenthesis. This may not necessarily save time, when entering data, but prevents possible mistakes like omitting, mismatching or misplacing parenthesis, mistakes that would alter the final result.

Furthermore, in computer’s early years, when memory was very limited and very expensive, storing expressions in this manner provided a huge saving of memory that could be dedicated to better uses. With RPN notation, infact, there is no need to store the entire expression in memory before evaluating it. The operands are automatically discarded once the operator has been evaluated, therefore only a small portion of the expression is ever stored.

These memory limitations, of course, don’t exist anymore, but many people still prefer to use RPN in their calculators. For this reason, Alcula has a simple RPN calculator that can be used online. Here is how it works:

Alcula’s Online RPN Calculator

Alcula's new RPN calculator

Alcula's new RPN calculator

The four fields at the top of the calculator, represent the visible portion of the stack. You can only edit the bottom line, which is the top of the stack. This maybe a little confusing at the beginning but it will become more natural as we use the calculator.

The first difference that catches the eye when comparing the keyboard of an RPN calculator with that of a traditional, ‘Immediate Execution’ calculator is the ‘Enter’ button, that replaces the common ‘=’.

Pressing ‘Enter’ executes a ‘push’; a new value operand is added to the top of the stack. As described above, to perform an operation, we first add the operands to the stack, then we perform the operation. For example, to calculate 3 + 2:

  1. We write ‘3’ in the register at the top of the stack: We type ‘3’, then ‘Enter’. This pushes a new element in the stack, ‘3’ is now the second element of the stack.
  2. We write ‘2’ in the top element of the stack: We type ‘2”. Now the stack contains ‘3’ and ‘2’.
  3. We apply the ‘+’ operator: We type ‘+’. The top 2 elements of the stack are added together and removed from the stack. The stack now contains only one element, that is the result of the addition.

For operations that take a single operand we don’t need to press ‘Enter’, because the numbers we are directly typing inside the topmost element of the stack. So if we want to calculate the square root of 2, We just type ‘2’ followed by the square root button (sqrt).

Expressions with parenthesis

As we mentioned, there is no need for parenthesis in RPN notation, so how do we enter a more complex expression?

We enter the expression inside-out. For example, if we want to calculate the following ((256+sqrt(12))^2)-51 we can enter the elements in the following order:

  1. 256
  2. Enter
  3. 12
  4. SQRT
  5. +
  6. 2
  7. POW
  8. 51

Where is Alcula’s RPN calculator?

To try the calculator follow this link: Alcula’s new online RPN calculator.

Tags: , , , , , ,

Comments are closed.