Postfix Expression Calculator
Evaluate a postfix expression to a number and watch the value stack change token by token. Infix and prefix work too — the notation is detected for you.
Value stack, step by step
| # | Token | Action | Value stack |
|---|
How postfix evaluation works
Evaluating postfix is the simplest algorithm on this site. One pass, one stack, no precedence rules, no brackets, no lookahead.
- Read the expression left to right. One token at a time.
- Operand: push its value. Numbers go straight onto the stack.
- Operator: pop two values. The first popped is the right operand, the second is the left.
-
Apply and push back.
Compute
left operator rightand push the single result. - One value remains. That is the answer. Anything else means the expression was malformed.
Worked example: 3 4 2 * 1 5 - 2 ^ / +
token action value stack
----- --------------------------------- -----------------
3 push 3 3
4 push 4 3 4
2 push 2 3 4 2
* pop 2, pop 4 -> 4 * 2 = 8 3 8
1 push 1 3 8 1
5 push 5 3 8 1 5
- pop 5, pop 1 -> 1 - 5 = -4 3 8 -4
2 push 2 3 8 -4 2
^ pop 2, pop -4 -> -4 ^ 2 = 16 3 8 16
/ pop 16, pop 8 -> 8 / 16 = 0.5 3 0.5
+ pop 0.5, pop 3 -> 3 + 0.5 = 3.5 3.5
result 3.5
That is the postfix form of 3 + 4 * 2 / (1 - 5) ^ 2. Notice that the
brackets and precedence were dealt with during the conversion —
by the time the calculator sees it, there is nothing left to decide.
Which value is the left operand
The second one popped. A stack hands back the most recent push first, and the right operand is written second in postfix, so it is pushed second and popped first.
| Postfix | Correct | Operands swapped |
|---|---|---|
| 8 2 - | 8 - 2 = 6 | 2 - 8 = -6 |
| 8 2 / | 8 / 2 = 4 | 2 / 8 = 0.25 |
| 2 3 ^ | 2 ^ 3 = 8 | 3 ^ 2 = 9 |
| 8 2 + | 10 | 10 — hides the bug |
Evaluating an infix expression
You do not evaluate infix directly — you convert it first. This calculator detects
that you typed infix, runs the
shunting-yard algorithm to get the
postfix form, then evaluates that with the stack above. Both stages are shown, so
typing 3 + 4 * 2 gives you the postfix 3 4 2 * + and the
result 11 — not 14, because multiplication binds tighter.
Convert once, evaluate many times. The conversion resolves every precedence and bracket question, so the evaluation stage never has to think about them again.
Evaluating a prefix expression
Same stack, two changes: scan right to left, and treat the first
value popped as the left operand instead of the right. Paste
+ 2 * 3 4 above and the calculator will recognise it as Polish notation
and handle the reversal for you.
Why calculators use postfix
- One pass, no backtracking. Infix evaluation needs to find the highest-precedence operation repeatedly; postfix just runs forwards.
- Constant memory per operation. Two pops and a push, regardless of how deeply the original expression was nested.
- No bracket matching at evaluation time. Brackets were resolved during conversion and no longer exist.
- It is what stack machines do natively. The Java Virtual Machine, the CPython interpreter and PostScript all execute stack-based bytecode, which is postfix in a different costume.
Frequently asked questions
How do you evaluate a postfix expression?
Scan it left to right with a stack of values. Push every operand. When you meet an operator, pop two values — the first popped is the right operand, the second is the left — apply the operator, and push the result back. One value is left at the end and that is the answer.
Why is postfix easier for a computer to evaluate than infix?
Postfix has no brackets and no precedence rules, so no lookahead or backtracking is needed. One left-to-right pass and one stack is enough. Evaluating infix directly means repeatedly deciding which operation comes next, which is why compilers and calculators convert to postfix first.
Which value is the left operand when you pop?
The second one popped. A stack returns the most recently pushed value first, and the right operand was pushed last. Swapping them leaves addition and multiplication looking correct while silently inverting subtraction, division and exponentiation.
Can this calculator evaluate infix expressions too?
Yes. Type an ordinary expression like 3 + 4 * 2 and it detects the notation automatically, converts it to postfix, then evaluates that. The postfix form and the value stack are both shown, so you can see exactly what it did.
How do you evaluate a prefix expression?
Scan right to left instead of left to right, and treat the first value popped as the left operand rather than the right. Everything else is the same. This calculator accepts prefix input and handles the reversal for you.
What happens with division by zero?
The calculator stops and says which division failed rather than returning Infinity or NaN. The same applies to modulo by zero.
Does it handle decimals and negative numbers?
Yes. Decimals like 3.5 work anywhere. A negative literal must be written against its digits with no space — -3 is the number, while - 3 is the subtraction operator, which matters in postfix and prefix where both are legal in the same position.