| • Science | • People | • Locations | • Timeline |
| Contents | ||
As a user interface for calculation the notation was first used in Hewlett-Packard's desktop calculators from the late 1960s and then in the HP-35 handheld scientific calculator launched in 1972. In RPN the operands precede the operator, thus dispensing with the need for parentheses. For example, the expression 3 * ( 4 + 7) would be written as 3 4 7 + *, and done on an RPN calculator as "3", "Enter", "4", "Enter", "7", "+", "*".
Implementations of RPN are stack-based; that is, operands are popped from a stack, and calculation results are pushed back onto it. Although this concept may seem obscure at first, RPN has the advantage of being extremely easy, and therefore fast, for a computer to analyze due to it being a regular grammarIn computer science a right regular grammar is a formal grammar N Σ, P S such that all the production rules in P are of one of the following forms: # A → a where A a non-terminal in N and a a terminal in Σ # A → aB where A and B in N.
The calculation: ((1 + 2) * 4) + 3 can be written down like this in RPN:
1 2 + 4 * 3 +The expression is evaluated in the following way (the Stack is displayed after Operation has taken place):
| Input | Stack | Operation |
|---|---|---|
| 1 | 1 | Push operand |
| 2 | 1, 2 | Push operand |
| + | 3 | Addition |
| 4 | 3, 4 | Push operand |
| * | 12 | Multiplication |
| 3 | 12, 3 | Push operand |
| + | 15 | Addition |
The final result, 15, lies on the top of the stack at the end of the calculation.
An alternate way of viewing the stack during the above operation is shown below (as seen on HP48S calculator).
+---------------+ + + + + + 1 + 1 enter +---------------+ +---------------+ + + + 1 + + 2 + 2 [enter] +---------------+ +---------------+ + + + + + 3 + + +---------------+ +---------------+ + + + 3 + + 4 + 4 [enter] +---------------+ +---------------+ + + + + + 12 + * +---------------+ +---------------+ + + + 12 + + 3 + 3 [enter] +---------------+ +---------------+ + + + + + 15 + + +---------------+The enters are in brackets because they are optional when proceded by an operator press. An enter is only needed to clear the insertion mark from the line. Thus, RPN allows the expression to be entered and evaluated in eight rather than eleven or twelve steps.
Like the evaluation of RPN, conversion from infix notationInfix notation is the arithmetic formula notation known to most people, in which operators are written infix-style between the operands they act on. It is not as simple to parse by computer as postfix notation, but many programming languages use it to tak to RPN is stack-based. Infix expressions are the form of math most people are used to, for instance 3+4 or 3+4*(2-1). For the conversion there are 2 text variableIn computer science and mathematics, a variable is a symbol denoting a quantity or symbolic representation. In mathematics, a variable often represents an unknown quantity; in computer science, it represents a place where a quantity can be stored. Variabls ( stringGenerally, string is a thin piece of fiber which is used to tie, bind, or hang other objects. String can be made from a variety of fibres. You can get different kinds, twine for example. The term has more specific meanings, within certain academic discipls), the input and the output. There is also a stack holding operators not yet added to the output stack. To convert, the program reads each letter in order and does something based on that letter.