-
Notifications
You must be signed in to change notification settings - Fork 0
Home
There are three distinct types of mathematical notation: infix, prefix, and postfix.
Type | Notation |
---|---|
Infix | 2 + 2 |
Prefix | + 2 2 |
Postfix | 2 2 + |
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 * |
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.