Reverse Polish notation
Template:Short description Script error: No such module "redirect hatnote". Template:Use list-defined references Template:Use dmy dates Template:Operator notation sidebar
Reverse Polish notation (RPN), also known as reverse Łukasiewicz notation, Polish postfix notation or simply postfix notation, is a mathematical notation in which operators follow their operands, in contrast to prefix or Polish notation (PN), in which operators precede their operands. The notation does not need any parentheses for as long as each operator has a fixed number of operands.
The term postfix notation describes the general scheme in mathematics and computer sciences, whereas the term reverse Polish notation typically refers specifically to the method used to enter calculations into hardware or software calculators, which often have additional side effects and implications depending on the actual implementation involving a stack. The description "Polish" refers to the nationality of logician Jan Łukasiewicz,[1][2] who invented Polish notation in 1924.[3][4][5][6]
The first computer to use postfix notation, though it long remained essentially unknown outside of Germany, was Konrad Zuse's Z3 in 1941[7][8] as well as his Z4 in 1945. The reverse Polish scheme was again proposed in 1954 by Arthur Burks, Don Warren, and Jesse Wright[9] and was independently reinvented by Friedrich L. Bauer and Edsger W. Dijkstra in the early 1960s to reduce computer memory access and use the stack to evaluate expressions. The algorithms and notation for this scheme were extended by the philosopher and computer scientist Charles L. Hamblin in the mid-1950s.[10][11][12][13][14][15]Template:Excessive citations inline
During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators, and has continued to use it in some models into the 2020s.[16][17] In computer science, reverse Polish notation is used in stack-oriented programming languages such as Forth, dc, Factor, STOIC, PostScript, RPL, and Joy.
Explanation
In reverse Polish notation, the operators follow their operands. For example, to add 3 and 4 together, the expression is 3 4 + rather than 3 + 4. The conventional notation expression 3 − 4 + 5 becomes 3 (enter) 4 − 5 + in reverse Polish notation: 4 is first subtracted from 3, then 5 is added to it.
The concept of a stack, a last-in/first-out construct, is integral to the left-to-right evaluation of RPN. In the example 3 4 −, first the 3 is put onto the stack, then the 4; the 4 is now on top and the 3 below it. The subtraction operator removes the top two items from the stack, performs 3 − 4, and puts the result of −1 onto the stack.
The common terminology is that added items are pushed on the stack and removed items are popped or pulled.
The advantage of reverse Polish notation is that it removes the need for order of operations and parentheses that are required by infix notation and can be evaluated linearly, left-to-right. For example, the infix expression (3 + 4) × (5 + 6) becomes 3 4 + 5 6 + × in reverse Polish notation.
Practical implications
Reverse Polish notation has been compared to how one had to work through problems with a slide rule.[18]
In comparison, testing of reverse Polish notation with algebraic notation, reverse Polish has been found to lead to faster calculations, for two reasons. The first reason is that reverse Polish calculators do not need expressions to be parenthesized, so fewer operations need to be entered to perform typical calculations. Additionally, users of reverse Polish calculators made fewer mistakes than for other types of calculators.[19][20] 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.[21] However, anecdotal evidence suggests that reverse Polish notation is more difficult for users who previously learned algebraic notation.[20]
Converting from infix notation
Script error: No such module "Labelled list hatnote". Edsger W. Dijkstra invented the shunting-yard algorithm to convert infix expressions to postfix expressions (reverse Polish notation), so named because its operation resembles that of a railroad shunting yard.
There are other ways of producing postfix expressions from infix expressions. Most operator-precedence parsers can be modified to produce postfix expressions; in particular, once an abstract syntax tree has been constructed, the corresponding postfix expression is given by a simple post-order traversal of that tree.
Implementations
Hardware calculators
Early history
The first computer implementing a form of reverse Polish notation (but without the name and also without a stack), was Konrad Zuse's Z3, which he started to construct in 1938 and demonstrated publicly on 12 May 1941.[22][23][24][25] In dialog mode, it allowed operators to enter two operands followed by the desired operation.Template:Refn It was destroyed on 21 December 1943 in a bombing raid.[23] With Zuse's help a first replica was built in 1961.[23] The 1945 Z4 also added a 2-level stack.[26][27]
Other early computers to implement architectures enabling reverse Polish notation were the English Electric Company's KDF9 machine, which was announced in 1960 and commercially available in 1963,[28] and the Burroughs B5000, announced in 1961 and also delivered in 1963:
Presumably, the KDF9 designers drew ideas from Hamblin's GEORGE (General Order Generator),[10][11][13][29][30][27] an autocode programming system written for a DEUCE computer installed at the University of Sydney, Australia, in 1957.[10][11][13][28]
One of the designers of the B5000, Robert S. Barton, later wrote that he developed reverse Polish notation independently of Hamblin sometime in 1958 after reading a 1954 textbook on symbolic logic by Irving Copi,[31][32][33] where he found a reference to Polish notation,[33] which made him read the works of Jan Łukasiewicz as well,[33] and before he was aware of Hamblin's work.
Friden introduced reverse Polish notation to the desktop calculator market with the EC-130, designed by Robert "Bob" Appleby Ragen,[34] supporting a four-level stack[5] in June 1963.[35] The successor EC-132 added a square root function in April 1965.[36] Around 1966, the Monroe Epic calculator supported an unnamed input scheme resembling RPN as well.[5]
Script error: No such module "anchor".Hewlett-Packard
Script error: No such module "Labelled list hatnote".
Script error: No such module "anchor".Hewlett-Packard engineers designed the 9100A Desktop Calculator in 1968 with reverse Polish notation[16] with only three stack levels with working registers X ("keyboard"), Y ("accumulate") and visible storage register Z ("temporary"),[37][38] a reverse Polish notation variant later referred to as three-level RPN.[39] This calculator popularized reverse Polish notation among the scientific and engineering communities. Script error: No such module "anchor".The HP-35, the world's first handheld scientific calculator,[16] introduced the classical four-level RPN with its specific ruleset of the so-called operational (memory) stack[40][nb 1] (later also called automatic memory stack[41][42][nb 1]) in 1972.[43] In this scheme, the Template:Key press key duplicates values into Y under certain conditions (automatic stack lift with temporary stack lift disable), and the top register T ("top") gets duplicated on drops (top copy on pop aka top stack level repetition) in order to ease some calculations and to save keystrokes.[42] HP used reverse Polish notation 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.
Later calculators with LCDs in the early 1980s, such as the HP-10C, HP-11C, HP-15C, HP-16C, and the financial HP-12C calculator also used reverse Polish notation. In 1988, Hewlett-Packard introduced a business calculator, the HP-19B, without reverse Polish notation, but its 1990 successor, the HP-19BII, gave users the option of using algebraic or reverse Polish notation again.
Script error: No such module "anchor".In 1986,[44][45] HP introduced RPL, an object-oriented successor to reverse Polish notation. It deviates from classical reverse Polish notation by using a dynamic stack only limited by the amount of available memory (instead of three or four fixed levels) and which could hold all kinds of data objects (including symbols, strings, lists, matrices, graphics, programs, etc.) instead of just numbers. The system would display an error message when running out of memory instead of just dropping values off the stack on overflow as with fixed-sized stacks.[46] 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 Template:Key press key so that it no longer duplicated values into Y, which had shown to sometimes cause confusion among users not familiar with the specific properties of the automatic memory stack. From 1990 to 2003, HP manufactured the HP-48 series of graphing RPL calculators, followed by the HP-49 series between 1999 and 2008. The last RPL calculator was named HP 50g, introduced in 2006 and discontinued in 2015. However, there are several community efforts like newRPL or DB48X to recreate RPL on modern calculators.
As of 2011, Hewlett-Packard was offering the calculator models 12C, 12C Platinum, 17bII+, 20b, 30b, 33s, 35s, 48gII (RPL) and 50g (RPL) which support reverse Polish notation.[47]
Script error: No such module "anchor".While calculators emulating classical models continued to support classical reverse Polish notation, new reverse Polish notation models feature a variant of reverse Polish notation, where the Template:Key press key behaves as in RPL. This latter variant is sometimes known as entry RPN.[48]
Script error: No such module "anchor".In 2013, the HP Prime introduced a 128-level form of entry RPN called advanced RPN. In contrast to RPL with its dynamic stack, it just drops values off the stack on overflow like other fixed-sized stacks do.[46] However, like RPL, it does not emulate the behaviour of a classical operational RPN stack to duplicate the top register on drops.
In late 2017, the list of active models supporting reverse Polish notation included only the 12C, 12C Platinum, 17bii+, 35s, and Prime. By July 2023, only the 12C, 12C Platinum, the HP 15C Collector's Edition, and the Prime remain active models supporting RPN. Script error: No such module "Labelled list hatnote".
Sinclair Radionics
In Britain, Clive Sinclair's Sinclair Scientific (1974) and Scientific Programmable (1975) models used reverse Polish notation.[49][50]
Script error: No such module "anchor".Commodore
In 1974, Commodore produced the Minuteman *6 (MM6) without an Template:Key press key and the Minuteman *6X (MM6X) with an Template:Key press key, both implementing a form of two-level RPN. The SR4921 RPN came with a variant of four-level RPN with stack levels named X, Y, Z, and W (rather than T) and an Template:Key press key (for "entry"). In contrast to Hewlett-Packard's reverse Polish notation implementation, W filled with 0 instead of its contents being duplicated on stack drops.[51]
Prinztronic
Prinz and Prinztronic were own-brand trade names of the British Dixons photographic and electronic goods stores retail chain, 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[52] Programmable Scientific Calculator which featured reverse Polish notation.
Script error: No such module "anchor".Heathkit
The Aircraft Navigation Computer Heathkit OC-1401/OCW-1401 used five-level RPN in 1978.
Soviet Union / Semico
Soviet programmable calculators (MK-52, MK-61, B3-34 and earlier B3-21[53] models) used reverse Polish notation for both automatic mode and programming. Modern Russian calculators MK-161[54] and MK-152,[55] designed and manufactured in Novosibirsk since 2007 and offered by Semico,[56] are backwards compatible with them. Their extended architecture is also based on reverse Polish notation.
Others
- Script error: No such module "anchor".A seven-level stack had been implemented in the MITS 7400C scientific desktop calculator in 1972[57][58][59]
- National Semiconductor 4615 and 4640
- Novus 650 Mathbox, 3500 Sliderule, 4510 Mathematician, 4515 Mathematician PRO/RG, 4520 Scientist and 4525 Scientist PR
- Some APF calculators like the Mark 55 (1976)[60]
- SwissMicros (originally firming as RPN-Calc) calculators including the DM-10CC (2012), DM-11CC (2012), DM-12CC (2012), DM-15CC (2012), DM-16CC (2012), DM10 (2013), DM11 (2013), DM12 (2013), DM15 (2013), DM16 (2013), DM10L Collector's Edition (2020), DM11L (2016), DM12L (2016), DM15L (2015), DM16L (2015), DM41 (2015), DM41L (2015), DM41X (2020), DM42 (2017) and DM32 (2023).
Script error: No such module "anchor".Community-developed hardware-based calculators
Script error: No such module "Labelled list hatnote". An eight-level stack was suggested by John A. Ball in 1978.[5]
The community-developed calculators WP 34S (2011), WP 31S (2014) and WP 34C (2015), which are based on the HP 20b/HP 30b hardware platform, support classical Hewlett-Packard-style reverse Polish notation supporting automatic stack lift behaviour of the Template:Key press key and top register copies on pops, but switchable between a four- and an eight-level operational stack.
In addition to the optional support for an eight-level stack, the newer SwissMicros DM42-based WP 43S as well as the WP 43C (2019) / C43 (2022) / C47 (2023) derivatives support data types for stack objects (real numbers, infinite integers, finite integers, complex numbers, strings, matrices, dates and times). The latter three variants can also be switched between classical and entry RPN behaviour of the Template:Key press key, a feature often requested by the community.[61] They also support a rarely seen significant figures mode, which had already been available as a compile-time option for the WP 34S and WP 31S.[62][63]
Since 2021, the HP-42S simulator Free42 version 3 can be enabled to support a dynamic RPN stack only limited by the amount of available memory instead of the classical 4-level stack. This feature was incorporated as a selectable function into the DM42 since firmware DMCP-3.21 / DM42-3.18.[64][65]
Software calculators
Software calculators:
- Atari Calculator
- Mac OS X Calculator
- Unix system calculator program dc
- Emacs lisp library package calc
- Xorg calculator (xcalc)
- F-Correlatives in MultiValue dictionary items
- RRDtool, a widely used tabulating and graphing software
- Template:Proper name, a program for algebraic operations on NetCDF grids, part of Generic Mapping Tools (GMT) suite
- Qalculate!, a powerful and versatile cross-platform desktop calculator
- WRPN Calculator
Programming languages
Existing implementations using reverse Polish notation include:
- Stack-oriented programming languages such as:
- Forth
- dc
- STOIC
- Factor
- PostScript page description language[66][67]
- BibTeX style files[68]
- Befunge
- Joy
- IPTSCRAE
- Lotus 1-2-3 and Lotus Symphony formulas[69][70]
- RPL (aka Reverse Polish Language), a programming language for the Commodore PET around 1979/1981
- RPL (aka Reverse Polish Lisp), a programming language for Hewlett-Packard calculators between 1986 and 2015[44][45]
- RPNL (Reverse Polish Notation Language)[71][72]
- Class libraries
- TRURL,[73] a class library for the construction of RPN calculators in Object Pascal
See also
- Calculator input methods
- FOCAL keystroke programming
- Stack machine
- Head-directionality parameter
- Scrambling (linguistics)
- Subject object verb and Object subject verb
Notes
Template:Reflist Template:Reflist
References
Further reading
- Script error: No such module "citation/CS1".
- Script error: No such module "citation/CS1". (13 pages)
- Script error: No such module "citation/CS1".
- Script error: No such module "citation/CS1".
- Script error: No such module "Citation/CS1". (4 pages)
- Script error: No such module "citation/CS1". [1] (13 of 702 pages)
- Script error: No such module "citation/CS1".
- Script error: No such module "citation/CS1".
- Script error: No such module "citation/CS1".
External links
- Rosettacode.org providing many implementations in several programming languages.
- http://rpn.codeplex.com/ Implementation of RPN with custom functions support and flexible list of operators.
- https://xrjunque.nom.es/ConvertAlg2RPN_RPL.aspx Free online Algebraic expression to RPN Converter
- ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedŁukasiewicz_1951 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedŁukasiewicz_1957 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedŁukasiewicz_1929 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHamblin_1962 - ↑ a b c d Cite error: Invalid
<ref>tag; no text was provided for refs namedBall_1978 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedKennedy_1982 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedCeruzzi_1980 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedRojas_1997 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBurks_1954 - ↑ a b c Cite error: Invalid
<ref>tag; no text was provided for refs namedHamblin_1957_1 - ↑ a b c Cite error: Invalid
<ref>tag; no text was provided for refs namedHamblin_1957_2 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHamblin_1957_3 - ↑ a b c Cite error: Invalid
<ref>tag; no text was provided for refs namedHamblin_1958 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedMcBurney_2008_2 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedMcBurney_2008_1 - ↑ a b c Cite error: Invalid
<ref>tag; no text was provided for refs namedOsborne_1994 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedPeterson_2011 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedWilliams_2023 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedKasprzyk-Drury-Bialas_1979 - ↑ a b Cite error: Invalid
<ref>tag; no text was provided for refs namedAgate_1980 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHoffman_1994 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedparTU_2000 - ↑ a b c Cite error: Invalid
<ref>tag; no text was provided for refs namedZuse_2008 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedDHM_2013 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBundesmann_2016 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBlaauw-Brooks_1997 - ↑ a b Cite error: Invalid
<ref>tag; no text was provided for refs namedLaForest_2007 - ↑ a b Cite error: Invalid
<ref>tag; no text was provided for refs namedBeard_1997 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedDuncan_1977 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedAllen_1985 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedNewApproach - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedB5000_1985 - ↑ a b c Cite error: Invalid
<ref>tag; no text was provided for refs namedGaller-Rosin_1985 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedRagen_2012 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedFriden_EC-130 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedFriden_EC-132 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedMonnier_1968 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHP9100A_1968 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHP9100A_1998 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHP35_UM - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHP42_OM - ↑ a b Cite error: Invalid
<ref>tag; no text was provided for refs namedHP15C_2011 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedLaporte_2014 - ↑ a b Cite error: Invalid
<ref>tag; no text was provided for refs namedWickes_1987 - ↑ a b Cite error: Invalid
<ref>tag; no text was provided for refs namedRPLMAN - ↑ a b Cite error: Invalid
<ref>tag; no text was provided for refs namedWessman_2016 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHP_Calc - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedHP_Evolves - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedShirriff_2013 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedSharwood_2013 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedCommodore_SR4921R - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedPrinztronic - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedElektronika_B3-21 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedElektronika_MK-161 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedElektronika_MK-61 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedSEMICO - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedRE_1972 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBerger_1973 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedMITS7400 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedTKE_1976 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedPaul_2015 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBit_2014 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBit_2015 - ↑ https://forum.swissmicros.com/viewtopic.php?f=16&t=2939
- ↑ https://forum.swissmicros.com/viewtopic.php?f=15&t=2845
- ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedAdobe_1986_PLTC - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedAdobe_PLRM3 - ↑ Script error: No such module "citation/CS1".
- ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBorn_2001_WKS - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedBorn_2001_WK3 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedFeichtinger_1987 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedWostrack_1989 - ↑ Cite error: Invalid
<ref>tag; no text was provided for refs namedDietrich_2019
Cite error: <ref> tags exist for a group named "nb", but no corresponding <references group="nb"/> tag was found