Home > Previous Page >  JA Manual Chapter 5 - Simple Arithmetic
Archive Search  
 

Contents
Introduction
Chapter 1
 SETTING UP THE ACE
Chapter 2
 SETTING UP THE ACE
Chapter 3
 LOADING PROGRAMS FROM TAPE
Chapter 4
 DEFINING NEW WORDS
Chapter 5
 SIMPLE ARITHMETIC
Chapter 6
 DEFINING NEW ARITHMETIC WORDS
Chapter 7
 ALTERING WORD DEFINITIONS
Chapter 8
 WORDS THAT ARE REALLY NUMBERS
Chapter 9
 MAKING DECISIONS
Chapter 10
 REPEATING
Chapter 11
 SOUND
Chapter 12
 THE CHARACTER SET
Chapter 13
 PLOTTING GRAPHS
Chapter 14
 SAVING PROGRAMS ON TAPE
Chapter 15
 FRACTIONS AND DECIMAL POINTS
Chapter 16
 READING THE KEYBOARD
Chapter 17
 OTHER WAYS OF COUNTING
Chapter 18
 BOOLEAN OPERATIONS
Chapter 19
 MORE ADVANCED ARITHMETIC
Chapter 20
 INSIDE THE DICTIONARY
Chapter 21
 STRINGS AND ARRAYS
Chapter 22
 VOCABULARIES
Chapter 23
 INSIDE COLON DEFINITIONS
Chapter 24
 HOW THE MEMORY IS LAID OUT
Chapter 25
 MACHINE CODE
Chapter 26
 EXTENDING THE ACE
Appendix A
 QUICK GUIDE FOR 'FORTH' ENTHUSIASTS
Appendix B
 ERRORS
Appendix C
 THE JUPITER ACE - FOR REFERENCE
Appendix D
 QUICK GUIDE FOR 'FORTH' ENTHUSIASTS
INDEX


Chapter 5


SIMPLE ARITHMETIC


Computers are famous for being able to do difficult sums very quickly, so let's try one on the Ace. Type in


2 2 + .

 ↑  ↑  ↑

Remember the spaces


When you press ENTER, the computer will write up '2 2 + . 4 OK', which combines the copied up record of your typing with the answer, 4 and OK -- it has (correctly) added 2 and 2 to get 4.
 Notice that you type in '2 2 +' instead of '2+2' - in other words, you're saying 'take 2 and 2 and add them together' rather than 'take 2 and add 2'. FORTH always works this way round; its rule is

First gather together the numbers you're interested in;

then do the calculations on them.

 This is like a recipe, with the list of ingredients at the top and then the instructions telling you what to do with them.
 + and . (call it 'dot') are just FORTH words — you can use VLIST to see them in the dictionary.

+ adds two numbers

. prints out a number on the television screen.

 2 isn't in the dictionary, but of course you and I and the computer know that it's a number. The computer remembers the numbers you type in until you tell it what to do with them.
 To remember the numbers, the computer uses a clever concept called the stack. It is actually done by electronics, but you can imagine a pile of cards with numbers written on them. To start off with, the stack is empty: no cards, no numbers. To remember the number 2, the computer takes a clean card, writes '2' on it, and puts it on top of the stack.



 When the computer has seen both 2s, there are two cards on the stack, each with '2' written on it:

 The FORTH word + says — take the top two cards off the stack and add together the numbers from them. Write this answer on a clean card, which goes on top of the stack. Throw away the two cards that you took off.

The FORTH word . says — take the top card off the stack and write its number on the television screen. Throw the card away.
 + uses the top two numbers on the stack regardless of how many there are underneath, and similarly . uses the top number ignoring any others. Thus both these words work on the numbers at the top of the stack rather than the bottom, and these are the numbers that were remembered most recently. This is true of all FORTH words. The numbers on top of the stack are the natural ones to use, and for a word to insist on going for the ones at the bottom would be unnatural, if not impossible.
 Metaphorically, the newest numbers are freshest in the computer's mind (the older ones are covered up(, and it's only when these are finally disposed of that it begins to remember the older numbers more clearly again.
 Suppose now you want to add three numbers together - say 10, 11 and 12. You'd first add 10 to 11, but you don't need to print out the answer — you can leave it on the stack to have 12 added to it, like this:


10 11 + 12 + .


10   


.               prints 33 on television



 One way of looking at this is to realise that '11 +' adds 11 to the top of the stack, '12 +' adds 12, and so on. The 10 starts the stack off. When you're tired of adding, try the other sorts of arithmetic:

 - is for subtraction. The minus sign - is got by using SYMBOL SHIFT with J. The underline character _ on 0 looks rather like it, so don't confuse them.
 * is for multiplication (SYMBOL SHIFT with B). The usual sign, x, looks too like the letter X for safety.

 / is for division (SYMBOL SHIFT with V).

 The numbers that these are used for on the Ace are integers, i.e. whole numbers. They can be negative, but not fractions, nor can they have decimal points. (Actually,

numbers with decimal points are allowed, but you need to use different FORTH words on them – for instance F+ instead of +, F. instead of .. These are dealt with in Chapter 15.)

 This lack of fractions is important to remember with /, because fractions aren't allowed in the answer either. For instance, try dividing 11 by 4:



/MOD leaves the quotient, 2, on top of the stack, so that is what . prints first. The remainder comes next.
 Here's a list of many of the words that do arithmetic. It's not a complete definition, but it gives you some idea of the variety available even before you start defining your own words.


WARNING These words don't work properly if the numbers are too big – see Exercise 2.

+, -, *, /, MOD and /MOD you've seen already.

NEGATE changes the sign of the top number on the stack - i.e. it multiplies the number by -1.

 1+, 1-, 2+, and 2- are specially defined words that do the same as 1 +, 1 - and so on (i.e. with spaces) but more quickly. For instance, 1+ adds one to the top number on the stack.

*/ uses three numbers at the top of the stack, and leaves one. It takes off the top three, multiplies together the two that were second and third from the top, and then divides the product by the number that was on the top. The quotient (answer to the division) is left on the stack. For instance,

6 5 2 */ .


works like


6 5 * 2 / .


to give an answer of (6*5) ÷ 2=15.  */MOD is like */, but with a /MOD operation instead of /. It takes three numbers off the stack and puts back two, the remainder and quotient.

 MAX and MIN take two numbers off the stack, and leave the larger or smaller (maximum or minimum) of the two.

 ABS takes one number off the stack, and leaves its absolute value i.e. the same number but with its sign ignored, so that it is left zero or positive.

 Try these out to see them working.

Summary
The stack
  FORTH words +, -, *, /, MOD, /MOD, NEGATE, 1+, 1-, 2+ ,2-, */, */MOD, MAX, MIN,   ABS


Exercises
1. Any problem in arithmetic can be turned into an exercise for the Ace — for instance:  The Jupiter Ace cost £89.95. How much of this goes to the Government as VAT (15%)? (The £89.95 includes VAT.)

 Answer We work in pence, to avoid fractions.
 The price+VAT is 115% of the price without VAT (the price itself=100%, VAT=15%, so the total price is 115%), which means that to get from the price without VAT to the price with, you multiply by 115/100. We, however, are given the price with VAT (8995p), so we want to do the reverse operation, i.e. multiply by 100/115.

So: price without VAT=8995p * 100/115 The answer we actually want, the VAT, is 15% of this, which is 8995p * 100/115 * 15/100 = 8995p * 15/115. Now do


8995   15   115   */   .



The answer, the VAT you paid on your Ace, is 1173p, or £11.73.

2. There is a limit to the size of numbers that the Ace can normally handle, but it doesn't tell you if you reach that limit. The largest number is 32767, and the smallest is -32768. In fact they wrap round to meet up with each other, so if you do


32767   1+   .


you get -32768.

Numbers outside this range won't even be read in properly — try

32768 .


(you get -32768).
This causes most problems with *, because it's easy to multiply two numbers that

are themselves quite small enough, but give a product that is too big. For instance,


256 256 * .


gives 0. (The real answer is 65536.)



3. Try these two:



256 256 * 256 / .


and


256 256 256 */ .


 The first one goes wrong, as explained in Exercise 2, and you might expect the second one to do the same. However, it unexpectedly gives the right answer.
 When doing a multiplication followed by a division, there is a good chance that the multiplication will produce a big number, only for the division to bring it back down to a small one. So with this in mind, */ is specially written to look after large products properly.

4. Execute . repeatedly until there's nothing left on the stack to print. The computer will print out a nonsense number, and then ERROR 2. This means 'stack underflow' or 'there seem to be fewer than no numbers left on the stack'.
 Stack underflow isn't always detected immediately, because the Ace only checks for it at certain times. Between these times it might well have dipped below the bottom of the stack without realising it, but this doesn't matter because there are some nonsense numbers under the stack for the computer to play with.
 For instance, suppose you have an empty stack and type


1   +


 The computer will add 1 to one of the nonsense numbers. Since the net effect of + is always to take one number off the stack, the computer imagines that it has taken the 1 off the stack to leave an exactly empty stack. An exactly empty stack hasn't yet quite underflowed, so there is no ERROR 2. However, if you execute + again the net effect of + (after adding together two nonsense numbers) is to take a number off an already empty stack, and this does underflow. Note that ERROR always empties the stack.


5. Try



1   0   /   .



 You will be surprised to find that the answer seems to be -1. This is wrong, of course; the fact is that you're not supposed to divide by 0. If you do you'll get nonsensical results on the Ace.


[Top] | [Back] | [Next]