Skip to content
Abhishek Sathiabalan edited this page Dec 23, 2018 · 3 revisions

Types of notation

There are three distinct types of mathematical notation: infix, prefix, and postfix.

Type Notation
Infix 2 + 2
Prefix + 2 2
Postfix 2 2 +

Why not always use infix notation?

Infix notation has a major flaw when you are looking to create a calculator, order of operations and parentheses. When you write an equation such as (3 + 2) * 3 the computer has to recognize the fact any data inside the parentheses must be evaluated before the rest of the expression. In this specific example order of operations dictates that we add 3 and 2 before we multiply by 3.

Parsing the expression correctly is very error prone and this is where postfix or prefix help us. In postfix and prefix the data given to us has already been converted into a notation that we do not have to worry about order of operations and parentheses. Let us look at our example below.

Type Notation
Infix (3 + 2) * 3
Prefix * + 3 2 3
Postfix 3 2 + 3 *

How do we convert from infix to postfix ?

Edsger W. Dijkstra created the shunting yard algorithm that converts infix to postfix or prefix notation. Using this algorithm alongside some custom rules, we have the basis of a semi-modern calculator.

Resources

Infix on Wikipedia

Prefix on Wikipedia

Postfix on Wikipedia

Edsger W. Dijkstra on Wikipedia

Shunting Yard Algorithm on Wikipedia