A Polish notation calculator converts an arithmetic expression between infix, prefix (Polish) and postfix (reverse Polish) notation, and evaluates it. Type an expression such as 3 + 4 × (2 − 1), say how it is written, and you get the same expression in all three forms and its value. It works with numbers and letters, and everything is done in your browser.
How to use the Polish notation calculator
- Choose Infix, Prefix or Postfix for the way that you write your expression.
- Type the expression. In prefix and postfix, leave a space between the symbols.
- Press Convert.
- Read the three forms and the value.
What Polish notation is
The Wikipedia article on Polish notation describes it as a mathematical notation in which operators precede their operands, and it says that Jan Łukasiewicz invented it in 1924.[1] Adding 1 and 2 is written + 1 2 in Polish notation, rather than 1 + 2 in infix notation, and (5 − 6) × 7 becomes × − 5 6 7.[1] The page notes that Polish notation does not need any parentheses as long as each operator has a fixed number of operands.[1] That is its main advantage: the order of the operations is fixed by the order of the symbols.
Reverse Polish notation
In reverse Polish notation, or postfix, the operators follow their operands, as in 3 4 +.[2] The article says that Łukasiewicz's notation is its root, that the first computer to use postfix was Konrad Zuse's Z3 in 1941, and that Friedrich Bauer and Edsger Dijkstra reinvented it in the early 1960s.[2] Its examples include 3 − 4 + 5 as 3 4 − 5 + and (3 + 4) × (5 + 6) as 3 4 + 5 6 + ×.[2] Postfix is easy for a computer to evaluate with a stack, and some calculators have used it.
How the calculator converts infix
For infix input, the calculator uses the shunting-yard algorithm, which Edsger Dijkstra first published in November 1961 and named for its resemblance to a railroad shunting yard.[3] The page shows that 3 + 4 gives 3 4 + and that 3 + 4 × (2 − 1) gives 3 4 2 1 − × +.[3] Its precedence table gives ^ a precedence of 4 and right associativity, × and ÷ a precedence of 3, and + and − a precedence of 2, all left associative.[3] The page's longer example, 3 + 4 × 2 ÷ (1 − 5) ^ 2 ^ 3, gives 3 4 2 × 1 5 − 2 3 ^ ^ ÷ +, and you can enter it to check the calculator.[3]
Evaluating postfix and prefix
To evaluate postfix, read the symbols from the left. Put each number on a stack, and when you meet an operator, take the top two numbers off the stack, apply the operator, and put the result back. The RPN page's example is 3 4 −: push 3, push 4, and the subtraction removes the top two items, giving −1.[2] At the end, one number is left. For 3 4 + 5 6 + ×, the stack holds 7, then 7 and 11, and then the product 77. Prefix is evaluated in the same way from the right. The calculator builds a tree from your expression and works out the value from it.
Converting between notations by hand
To change infix to prefix by hand, draw the expression as a tree, with the operator at the top of each part, and read the tree with the operator first, then the left branch, then the right. Postfix reads the left branch, then the right branch, and then the operator. For the tree of (5 − 6) × 7, the root is ×, the left branch is the − with 5 and 6, and the right branch is 7. That gives × − 5 6 7 in prefix and 5 6 − 7 × in postfix. In the other direction, the calculator puts brackets in only where they are needed, so 3 4 − 5 + reads as 3 − 4 + 5.
Worked examples
Take the infix expression (3 + 4) × (5 + 6). In postfix it is 3 4 + 5 6 + ×, which is evaluated as follows: 3 and 4 are pushed, the + replaces them with 7; 5 and 6 are pushed and the + gives 11; the × multiplies 7 and 11 to give 77. In prefix the same expression is × + 3 4 + 5 6, which has no brackets either. Now take 8 − (3 − 2). Postfix is 8 3 2 − −, which evaluates to 8 − 1 = 7. The calculator writes the infix form with brackets only where they are needed, so a prefix expression like − 8 − 3 2 is shown as 8 − (3 − 2), while − − 8 3 2 is shown as 8 − 3 − 2.
Why compilers and calculators use postfix
A computer cannot easily read an infix expression from left to right, because it has to look ahead to see which operation comes first. Once an expression is in postfix, it can be evaluated in a single pass with a stack, with no brackets and no precedence rules. That is why compilers often convert expressions to postfix or to a tree before they generate code, and why some programming languages, such as Forth and PostScript, use postfix directly. Some early scientific calculators also used postfix, so that users pressed the numbers first and then the operator.
Letters and numbers
You can use single letters as variables, such as a + b × c. The calculator converts the form, giving a b c × + in postfix, but it cannot give a value, because the letters have no numbers. If you type numbers with decimals, such as 2.5 × 4, the value is worked out as usual. A number with a minus sign in front, like −3, is treated as one number. For a subtraction, put a space around the minus, as in 5 − 3.
Common mistakes
- Leaving out spaces in prefix and postfix. The symbols must be separated.
- Forgetting the order of the operands. In postfix, 5 6 − means 5 − 6, not 6 − 5.
- Treating ^ as left associative. 2 ^ 3 ^ 2 means 2 ^ (3 ^ 2) = 512.
- Using too few operands. Each operator needs exactly two.
What next
Use this often? Add Polish Notat to your Home screen.
Frequently asked questions
What is a Polish notation calculator?
A tool that converts an arithmetic expression between infix, prefix and postfix notation, and evaluates it.
What is Polish notation?
A notation in which each operator comes before its operands, so 1 + 2 is written + 1 2. It was invented by Jan Łukasiewicz in 1924.
What is reverse Polish notation?
A notation in which operators follow their operands, so 3 + 4 is written 3 4 +. It is easy to evaluate with a stack.
How do I convert infix to postfix?
Use the shunting-yard algorithm, which orders the operators by precedence. 3 + 4 × (2 − 1) becomes 3 4 2 1 − × +.
Why does postfix not need parentheses?
The order of the symbols fixes the order of the operations, as long as each operator has a fixed number of operands.
Sources
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.