Charles Moore at the time
Forth is a computer language invented by Charles Moore and built on the principle Keep it simple!  There are plenty free Forth-systems to download for several operating systems, for example GForth and SP-Forth, and also very good tutorials like Starting Forth by Leo Brodie. Also for free.

This blog is about computational mathematics, how to create mathematical routines in Forth. I would be surprised if my code turned out to be innovative or optimal. Of course I try to do my best but my ambition is not to present very efficient routines. The blog is just a growing collection of words connected with mathematics.

I use ANS Forth, plus the most common notation of local variables to make the code more easy to write and read. A wonderful thing with Forth is that you are working with an interactive interpreter and create milieus for your purposes. You write something, press enter and the computer respond with an answer followed by ok or present an error message. If you write some numbers

123 314 465425633

separated by a space the system store the numbers in the parameterstack when you press enter

123 314 465425633  ok

The last number is stored at the top of the stack and so on. If you write +, the routine for addition, and press enter the two top numbers on the stack are added and being replace with their sum

+  ok

To see the sum you can print a dot and press enter

.  465425947  ok

Now only the number 123 is left on the stack since the dot routine like the adding routine popped the stack. This is described by the stackdiagrams:

+  ( m n -- m+n )

.  ( n -- )

In Forth routines are called words and their names could be a sequence of any ascii signs except space, which is the sign for separating different words. To program you use the colon word : followed by a space and the name of the routine and the words that define the action of the word

: hello_world  ." Hello world" ;

Note the space between the word ." and the string. The ending " is not a word but a signal to the word .".  The last word ; denotes the end of the routine. Also the control structures are words defined by : and ;

: if  ( at compile-time: -- orig | at run-time: f -- )
  postpone ?branch >mark ; immediate

The brackets are comments, in this case stack diagrams. The word immediate signals that if should be executed while the word that contain it is compiled and not when it is running. Then, at compile time (the time between you press enter and the interpreter write 'ok') the word ?branch is compiled in the main routine and some signals to other control structure words as else and then are made for syntax control. This is described in Leo Brodies book above.

The words if, then and else works a bit different in Forth, as all Forth words in post fix notation style by acting on the numbers on the stack.

: test  ( n — )
  if .” the number is not zero”
  else .” the number is zero"
  then ;

The word if read the top value on the stack, pop it and direct the control flow. After the test the program continues after the word then.

To manipulate the stack there are several words and the most important are:

dup ( a -- a a )
drop ( a -- )
swap ( a b -- b a )
over ( a b -- a b a )
rot ( a b c -- b c a )
-rot ( a b c -- c a b )
tuck ( a b -- b a b )
nip ( a b -- b )

2dup ( a b -- a b a b )
2drop ( a b -- )
2swap ( a b c d -- c d a b )
2over ( a b c d -- a b c d a b )
2rot ( a b c d e f -- c d e f a b )
2tuck ( a b c d -- c d a b c d )

All comparison words pop the stack:

< ( a b -- flag ) and similar for > and =.
0= ( a -- flag ) and similar for 0< and 0>.

Keep it simple!


  1. Starting Forth is the book from,which I learned Forth for artificial intelligence.

    1. I will download Win32Forth and try it. It didn't work with GForth or SP-Forth. What does 'Mind' do?

  2. From Spain , this very interesting blog. I hope you can continue to make mathematical solutions .

    1. Thanks! I will continue the blog also for my own sake: to get it all well documented in a safe place. After cell size mathematics I will continue with mathematical routines for unsigned integers of any bit-length.