Node:Arithmetics Tutorial, Next:, Previous:Stack Tutorial, Up:Tutorial



Arithmetics

The words +, -, *, /, and mod always operate on the top two stack items:

2 2 .s
+ .s
.
2 1 - .
7 3 mod .

The operands of -, /, and mod are in the same order as in the corresponding infix expression (this is generally the case in Forth).

Parentheses are superfluous (and not available), because the order of the words unambiguously determines the order of evaluation and the operands:

3 4 + 5 * .
3 4 5 * + .
Assignment:
What are the infix expressions corresponding to the Forth code above? Write 6-7*8+9 in Forth notation1.

To change the sign, use negate:

2 negate .
Assignment:
Convert -(-3)*4-5 to Forth.

/mod performs both / and mod.

7 3 /mod . .

Reference: Arithmetic.


Footnotes

  1. This notation is also known as Postfix or RPN (Reverse Polish Notation).