Home > Previous Page >  Gil Filbey - Forth Series part 4
Archive Search  



Gil Filbey

A miscellany

  You notice that there are no rules about brackets i.e. working from the inside out although this is effectively what happens in both these examples. RPN takes care of it. What are the rules then? They are as follows:

1). Place number on stack
2). If there is a unary operator next, do it
3). Place next number on stack
4). Repeat 2
5). If there is a binary operator next, do it
6). Place next number on stack

This goes on to ternary operators and higher. (Try writing a flow chart for the above procedure). A simple example of a non-arithmetic ternary operator is the FORTH word ROT which rotates the top three numbers on the stack. To remind you:

1 2 3 ROT 2 3 1

An example of an arithmetic ternary operator is the FORTH word */

A B C */

evaluates A*B/C

It is different, in its effect, from

A B * C /

in that the product AB can be up to 31 bits whilst the simple AB*is a 16 bit number. You could even regard the operator:

Cos + * + * ;

as a quinary operator and so define the word:

: WHIZ COS + * + * ;

Then in the unlikely event of having to evaluate a large number of expressions of the form X you could write


Where A B C D E is any set of parameters.

Getting up to a few antics such as this really gives you the feel of FORTH. For fear you might be misled I must point out that 'cos' is not a Forth word and if you want to use such trigonometric functions you must define the appropriate words.

  A further illustration. To evaluate a polynomial

6x5+5 x 4+4 x3 + 3 x 2 + 2 x + 1

This is best written

((((6x + 5) x + 4) x + 3) x + 2) x + 1

If we had x, 5 times in succession on the top of the stack


We could proceed by

6 * 5 + * 4 + * 3 + * 2 + 1 +

Each successive'*'uses up an'x'

You could define the word POLY thus

6 * 5 + * 4 + 3 + 2 + * 1 + ;

To use it you write


Where 6 is a chosen value of x and 5 is the highest power of x. The LOOP gives 5 successive values 6 and the rest evaluates the polynomial. For an amusing example which none-the-less points the way to the use of FORTH in precision arithmetic we define the word AB/ thus,


  The words which require explaining are

CR (Carriage return).

MOD (Divide 2nd on stack by the top leaving remainder)

Reverse Polish Notation


t has been suggested by some readers may not be familiar with Reverse Polish Notation (RPN) as used by FORTH and incidentally by BURP (Basic Using Reverse Polish), which is a language used by the Wireless World Scientific computer,* and the whole range of Hewlett Packard calculators. To read a line of arithmetical calculation in RPN you run your eye along until you come to the first operator (such as +, *, cos) and then use it in a logical fashion on the numbers which precede it. Some operators are unary operators, that is to say they need only a single operand (number). Cos is such an operator. Some are binary in that they need two operands. Plus (+) is such and times (*) is such another.

2 Cos

means, 'evaluate the cosine of 2' whilst

2 Cos 1 +

means 'evaluate the cosine of 2 and add 1'.

It helps the clarity if a double space separates cos and 1,

2 cos 1 +

The power of RPN lies in the fact that no rules concerning brackets are necessary; neither is an 'equals' sign. Whilst it is an advantage to write down a complicated expression in order favourable to RPN it is

never necessary. Any expression can be tackled in the order in which it is written. You enter the first number (operand) and if there is an operation you can carry out on it, you do so. Otherwise you leave it there and pass on to the next number. It will wait patiently until numbers above it have been processed and each such process feeds it up. Consider an extreme case,

X = 6 (3 + 2 (4 + Cos 2))

Evaluated directly in RPN this is,

6 3 2 4 2 Cos + * + *

Five dangling operands before the first operator!

  You write down each number in succession and then list the operators reading backwards along the line. In FORTH nothing happens till you hit RETURN at the end. That is the way I would do it in FORTH if I were just using FORTH as a calculator

I couldn't do it like that, however, on my H.P 29C calculator since the stack isn't big enough. The initial 6 would drop out. It would be necessary to change the order to,

((Cos 2 + 4)2 + 3)6

Evaluated in RPN this is

2 Cos 4 + 2 * 3 + 6 *

and no operands are left dangling.

* Powertran, designed by J H Adams.

% (Prints without any space).

Use, N AB AB/

Result, it prints out the result of dividing A by B to N decimal places where N is unlimited!

Some other features of Appleforth.5.

  The HEX dump feature is interesting because it gives both the usual HEX listing of a program or definition together with the ASCII values by the side. As an example we list out the definition AB/just quoted. It was written into what is called SCREEN 4 which is part of a memory area set aside for definitions and editing, (There are 6 such screens each of size 1K):

  The ASCII characters enclosed in brackets are comments to remind the user of the way to use the definition. Anything enclosed between brackets is ignored by the compiler. You notice the word HEX at the beginning. This puts the system into HEX from DECIMAL and it will remain in HEX until you decide to change it. In the original version of FORTH the screens are sectors on a disc; but in the version I am using, and in the PROGRAMMA version for PET and APPLE etc, they are memory locations. When you are in one of these you have the ability to edit your text. In the 1.5 version this is done by means of control commands.

  • Control N moves the cursor up .
  • Control B moves the cursor down
  • Control L moves the line the cursor is
           on, and all text below it, down, leaving a
           blank line in which to insert text
  • Control Q deletes the cursor line and
  • closes the gap.
  • Control I enables text to be inserted at
            the point in the line at which the cursor
            is placed (Control A gets out of this
  • Control D deletes the character on which
            the cursor sits and closes the gap.


  • Left and right arrows allow cursor movement.

When you have finally edited the text to your satisfaction you then type

  • ;S followed by Control E which escapes from the editor mode and
  • Load compiles the defined words into dictionary

  Comments may be inserted anywhere even within a definition by means of the FORTH word " ("This, being a word, requires a space to be left before the first character of the comment e.g:


The PROGRAMMA INTERNATIONAL version uses a different method. When in a screen you use Line numbering (automatic if you want). The last line of the screen is then used as an edit line. Anything written in it may be inserted as a line into the existing text or used to replace a line of the text. Both of these are simple and powerful editing systems.

A Forth Programme

  To finish off this rather discursive article we ought to have another program. I choose a digital filter software implementation.

  A digital filter is a device into which an input stream of data is fed at fixed time intervals. The output at any instant is dependent on the state of the device and the value of the input. The state of the device however is dependent upon previous inputs and outputs. In some way we are all such devices! Our reaction to a stimulus depends upon our experience both of the effect upon us of the stimulus and of the results of our previous reactions. However a digital filter should be relied on not to change its state in the interpulse interval. A simple type of filter is one in which the output depends on the input and the immediately previous output in the following way

Xn  →▭→  Yn

Yn = K1 Xn + K2 Y(n-1)

This is called a first-order-lag filter.
  If K1 & K2 are less than one and

K1 + K2 = 1

we have a low-pass filter. Such a filter can show, for example the trend in the values of X whilst smoothing out short-term fluctuations. You might be tempted in this case to write

Yn = K1 (Xn-Yn-1) + Y(n-1)

but the truncation errors would be greater in integer arithmetic.

  We define variables A1 A2 B1 B2 in the usual way:


and a variable to denote a time internal. This latter is not necessary in the present case but is useful for scaling a graph (I shall discuss graphics in a later article):

0 Variable T


: INIT 0 T ! B2 ! B1 ! A2 ! A1 !;
: K1 A1 a A2 a */ ;
: K2 B1 a B2 a */ ;


To start we type N N N N INIT).

Which establishes the values K1 & K2. Eg:

2 13 11 13 INIT


: LAG K1 SWAP K2 DUP . ;




X2 LAG X3 LAG Etc.

X1 X2 X3 ... could be the readings of your electricity meter or any other set of data which requires smoothing. The smaller the value of K1 the greater the smoothing effect. Noisy signals can be cleaned up by storing the signals on tape or directly in memory and feeding them through the LAG programme at leisure. The precision of the arithmetic is generally adequate here but you might like to improve it along the lines of the AB/ example.