Mathematical Operators
    Il "Quarto" fuso ] The 4th Time Zone ]   


 

Introduction
Input/Output operations
Mathematical Operators
Stack operators
Other operators
Colon definition
Control structures
Conditional branching
Constants and variables
Other Forth words
Using the editor
Error messages


                                   

[ Index ][ Back ][ Next ]

+

This will add the top two numbers on the stack and leave the result as a single number.

e.g.      1 2 + . <CR>

will print the value of 1+2 = 3 on the screen. Note that the two top numbers are removed from the stack, being replaced by a single number. This is true of most Forth commands, in that they remove the values which they require to use from the stack and push the result onto the stack.

The stack in these examples:
N1 = top number on the stack (first to be removed)
N2 = second number on the stack (second to be removed)
N3 = third number on the stack (third to be removed)

To demostrate this, let us push three numbers onto the stack by typing

HEX 10FA 0019 1F47 <CR>

The stack will look like this:

1F47

TOS

(Top Of Stack)

0019

01fa

Note that this illustrate the property of the stack that is Last In First Out or LIFO, therefore we have:

N1 = 1F47
N2 =
0019
N3 =
01FA

So if we type:

CR . CR . CR . CR <CR>

we get:

1F47
0019
01FA

We will now resume our explanation of the mathematical operators

-

This will subtract the top number on the stack from the second number on the stack and leave the result as the top number. i.e.            N1 = N2 – N1.

e.g.            DECIMAL 7 11 - . <CR>

will print –4 since the stack would contain

N1

11

TOS (Top Of Stack)

N2

7

before the subtraction, and

N1

-4

TOS (Top Of Stack)

after the subtraction.

*

This will multiply the top two numbers on the stack and leave the result on the top of the stack. i.e. N1 = N1 * N2.

e.g.            DECIMAL 140 20 * . <CR>

will print 2800.

/

This will divide the second number on the stack with the first on the stack

e.g.            DECIMAL 1000 500 / . <CR>

will print 2.

MAX

This will leave the greater of the top two numbers on the stack.

e.g.      371 309 MAX . <CR>

will print 371.

MIN

This will leave the smaller of the top two numbers on the stack.

e.g.      371 309 MAX . <CR>

will print 309.

ABS

This will leave the absolute value of the top number on the stack as an unsigned number. i.e. N1 = abs(N1)

e.g.      47 ABS . <CR>

will print 47

            -47 ABS . <CR>     

will print 47.

MINUS or NEGATE

This will negate the top number on the stack. i.e. N1 = -N1

e.g.      418 MINUS  . <CR>

will print -418

            -418 MINUS  . <CR>

will print 418.

1+

Add 1 to the top number on the stack. i.e. N1 = N1 + 1

2+

Add 2 to the top number on the stack. i.e. N1 = N1 + 2

1-

Subtract 1 from the top number on the stack. i.e. N1 = N1 - 1

2-

Subtract 2 from the top number on the stack. i.e. N1 = N1 – 2

MOD

This will leave the remainder of N2 / N1 on the top of the stack with the same sign as N2.

e.g.      17 3 MOD . <CR>

will print 2 (17 / 3 = 5 remainder 2).

/MOD

This will leave the remainder and the quotient on the stack of N2/N1 such that the quotient becomes the top number on the stack an the remainder becomes the second.

e.g.      17 3 /MOD . CR . <CR>

will print  

5

(quotient)

2

(remainder)
 

[ Index ][ Back ][ Next ]


Ultimo aggiornamento: 10-12-2002. Copyright (c) 2000-2002 Matteo Vitturi. 
Per problemi o domande relativamente a questo sito contattare il webmaster
Last update: 12.10.2002. Copyright (c) 2000-2002 Matteo Vitturi.
For problems or questions related to this web please contact the webmaster.
1