Reverse Polish notation

Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, in contrast to Polish notation, which puts the operator in the prefix position. It is also known as postfix notation and is parenthesis-free as long as operator arities are fixed. The description "Polish" refers to the nationality of logicianJan Łukasiewicz, who invented (prefix) Polish notation in the 1920s.[1]

In reverse Polish notation the operators follow their operands; for instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand; so the expression written "3 − 4 + 5" in conventional notation would be written "3 4 − 5 +" in RPN: 4 is first subtracted from 3, then 5 added to it. An advantage of RPN is that it removes the need for parentheses that are required by infix. While "3 − 4 × 5" can also be written "3 − (4 × 5)", that means something quite different from "(3 − 4) × 5". In postfix, the former could be written "3 4 5 × −", which unambiguously means "3 (4 5 ×) −" which reduces to "3 20 −"; the latter could be written "3 4 − 5 ×" (or 5 3 4 − ×, if keeping similar formatting), which unambiguously means "(3 4 −) 5 ×".

Despite the name, reverse Polish notation is not exactly the reverse of Polish notation, for the operands of non-commutative operations are still written in the conventional order (e.g. "÷ 6 3" in Polish notation and "6 3 ÷" in reverse Polish both evaluate to 2, whereas "3 6 ÷" in reverse Polish notation would evaluate to ½).

In comparison testing of reverse Polish notation with algebraic notation, reverse Polish has been found to lead to faster calculations, for two reasons. Because reverse Polish calculators do not need expressions to be parenthesized, fewer operations need to be entered to perform typical calculations. Additionally, users of reverse Polish calculators made fewer mistakes than for other types of calculator.[5][6] Later research clarified that the increased speed from reverse Polish notation may be attributed to the smaller number of keystrokes needed to enter this notation, rather than to a smaller cognitive load on its users.[7] However, anecdotal evidence suggests that reverse Polish notation is more difficult for users to learn than algebraic notation.[6]

The infix expression "5 + ((1 + 2) × 4) − 3" can be written down like this in RPN:

5 1 2 + 4 × + 3 −

The expression is evaluated left-to-right, with the inputs interpreted as shown in the following table (the Stack is the list of values the algorithm is "keeping track of" after the Operation given in the middle column has taken place):

Input

Operation

Stack

Comment

5

Push value

5

1

Push value

1
5

2

Push value

2
1
5

+

Add

3
5

Pop two values (1, 2) and push result (3)

4

Push value

4
3
5

×

Multiply

12
5

Pop two values (3, 4) and push result (12)

+

Add

17

Pop two values (5, 12) and push result (17)

3

Push value

3
17

−

Subtract

14

Pop two values (17, 3) and push result (14)

Result

(14)

When a computation is finished, its result remains as the top (and only) value in the stack; in this case, 14.

The above example could be rewritten by following the "chain calculation" method described by HP for their series of RPN calculators:[8]

As was demonstrated in the Algebraic mode, it is usually easier (fewer keystrokes) in working a problem like this to begin with the arithmetic operations inside the parentheses first.

The first computers to implement architectures enabling RPN were the English Electric Company's KDF9 machine, which was announced in 1960 and delivered (i.e. made available commercially) in 1963, and the American Burroughs B5000, announced in 1961 and also delivered in 1963. One of the designers of the B5000, Robert S. Barton, later wrote that he developed RPN independently of Hamblin sometime in 1958 while reading a textbook by "Kopi" (likely Irving Copi, who was at the University of Michigan at the time) on symbolic logic[9][10] and before he was aware of Hamblin's work. Friden introduced RPN to the desktop calculator market with the EC-130 in June 1963.

A promotional Hewlett-Packard "No Equals" hat from the 1980s - both a boast and a reference to RPN.

Hewlett-Packard engineers designed the 9100A Desktop Calculator in 1968 with RPN with only three stack levels, a RPN variant later referred to as three-level RPN. This calculator popularized RPN among the scientific and engineering communities, even though early advertisements for the 9100A failed to mention RPN. The HP-35, the world's first handheld scientific calculator, introduced the classical four-level RPN in 1972. HP used RPN on every handheld calculator it sold, whether scientific, financial, or programmable, until it introduced the HP-10 adding machine calculator in 1977. By this time HP was the leading manufacturer of calculators for professionals, including engineers and accountants.

HP introduced an LCD-based line of calculators in the early 1980s that used RPN, such as the HP-10C, HP-11C, HP-15C, HP-16C, and the famous financial calculator, the HP-12C. In 1988 Hewlett-Packard introduced a business calculator, the HP-19B, without RPN, but its 1990 successor, the HP-19BII, gave users the option of using algebraic notation or RPN.

Around 1987, HP introduced RPL, an object-oriented successor to RPN. It deviates from classical RPN by utilizing a stack only limited by the amount of available memory (instead of three or four fixed levels) and which can hold all kinds of data objects (including symbols, strings, lists, matrices, graphics, programs, etc.) instead of just numbers. It also changed the behaviour of the stack to no longer duplicate the top register on drops (since in an unlimited stack there is no longer a top register) and the behaviour of the ↵ Enter key so that it no longer duplicates values into Y under certain conditions, both part of the specific ruleset of the automatic operational stack in classical RPN in order to ease some calculations and to save keystrokes, but which had shown to also sometimes cause confusion among users not familiar with these properties.

From 1990 to 2003 HP manufactured the HP-48 series of graphing RPL calculators and in 2006 introduced the HP 50g with a 131x80 LCD and a 75 MHz ARM CPU that emulates the Saturn CPU of the HP-48 series.

While calculators emulating classical models continue to support classical RPN, new RPN calculators feature a variant of RPN, where the Enter key behaves as in RPL. This latter variant is sometimes known as entry RPN.[12]

Prinz and Prinztronic were own-brand trade names of the British Dixons photographic and electronic goods stores retail chain, which was later rebranded as Currys Digital stores, and became part of DSG International. A variety of calculator models was sold in the 1970s under the Prinztronic brand, all made for them by other companies.

Among these was the PROGRAM Programmable Scientific Calculator which featured RPN.

^"Charles L. Hamblin: Computer Pioneer" by Peter McBurney, July 27, 2008. "Hamblin soon became aware of the problems of (a) computing mathematical formulae containing brackets, and (b) the memory overhead in having dealing with memory stores each of which had its own name. One solution to the first problem was Jan Lukasiewicz's Polish notation, which enables a writer of mathematical notation to instruct a reader the order in which to execute the operations (e.g. addition, multiplication, etc) without using brackets. Polish notation achieves this by having an operator (+, ×, etc) precede the operands to which it applies, e.g., +ab, instead of the usual, a+b. Hamblin, with his training in formal logic, knew of Lukasiewicz's work."