This is the first of several articles by Dr. Frank O’Hara on numerical errors in the ZX81’s 8K ROM floating point.–AZ
All floating-point systems require certain techniques of rounding, general approximation and slightly over- or under-stating numbers to make the arithmetic work. The problem is particularly acute in the ZX81 because 1) all numbers, even integers, are held in floating-point form; 2) of the almost total lack of instruction in floating-point techniques in the manual or the literature; and 3) the limitation of the ROM to 8K and the premium on every byte.
In doing arithmetic on the ZX81, it helps to know just which numbers are held exactly in its BASIC and which are not. This small program shows you exactly how any number is held on the ZX81:
Program 1
10 INPUT X
20 LET V=PEEK 16400+256*PEEK 16401
30 FOR I=V+1 TO V+5
40 PRINT PEEK I;" ";
50 NEXT I
60 PRINT
70 GOTO 10
INTEGER POWERS OF 2: To use this program, RUN it and first enter 0 to satisfy yourself that 0 is held as five zeros as the manual says. Then try some small integer powers of 2, like 1 (or 2⁰), 2, 4, 8. You’ll get 129 0 0 0 0; 130 0 0 0 0; 131 0 0 0 0; and 132 0 0 0 0. Before explaining this, let me give you a program to generate all integer powers of 2 from 1 to 126. First delete line 10 from Program 1 and replace line 70 with 70 RETURN. Then add these lines to give:
Program 2
(lines 30-70 from Program 1)
100 LET X=1
120 FOR J=1 TO 126
130 PRINT J;" ";
140 LET X=2*X
150 GOSUB 30
160 NEXT J
Finally, make line 20 be line 110 and delete the old line 20. To run, press GOTO 100 then NEWLINE (NL) or ENTER. The first 22 powers of 2 will appear, each in the form (129 plus n) 0 0 0 0 for the nth power. To see the other 5 screenfuls, continue in FAST at about 7 seconds per screenful. You should then be convinced the manual has got it right on this point. After seeing the 126th power (255 0 0 0 0), try PRINT 2*X. You’ll get report 6/0 for arithmetic overflow. The ZX81 cannot hold 2 to the 127.
To see the negative powers of 2, replace line 140 by LET X=X/2 and change 126 to 140 in line 120. RUN 100 and you get the powers from 2⁻¹ (128 0 0 0 0) to 2⁻¹²⁸ (1 0 0 0 0). From -129 to -140 the powers should all be held as zero (0 0 0 0 0), but here we meet our first quirk of the ROM. On division by 2, 2⁻¹²⁸ becomes 2⁻¹²⁹ and is promptly rounded up to 2⁻¹²⁸ again! So our innocuous little program has got us stuck here. Try LET X=X/4, then GOTO 30 (NOT RUN 30) and you will get 0 0 0 0 0 and of course report 7/70 (RETURN with no GOSUB). If you have the patience to run the program again with 128 in line 120, you can use this same method to get a zero result for 2⁻¹³⁰. If you want a zero result for 2⁻¹²⁹ you have to fall back on the the otherwise less accurate type of expression LET X=2**-129.
So we have a simple way of generating the exact representation of integral powers of 2 on the ZX81 (except for 2⁻¹²⁹). We see that, as we expect from the manual, 2ⁿ is given by (129 plus n) 0 0 0 0 where n equals -128 to 126 inclusive.
Now compare these exact results with the expression 2**N. To do this, produce Program 3 from Program 2: Make sure that line 120 contains 126 and change line 140 to LET X=2**J. RUN 100 and note the 19 inaccuracies at the powers 13, 26, 27, 31, 52-54, 61, 62, 104-108 and 121-125. Running the negative powers reveals similar inaccuracies at corresponding negative powers.
Note in particular that PRINT 2**13-8192 gives .000022888184. That extra 6 in the last byte is 3/2¹⁷, about .00002. The inaccuracy arises because the ROM evaluates 2**13 by approximate methods as EXP (13*LN2). Of course, 8192.00002 may be accurate enough for most purposes, but it may help you to know that it is not completely accurate and you can always get it exactly if you need it by 2*2**12. The same holds for any other inaccurately held powers. For example, the important number 2³¹ is given exactly by 2*2**30 or by 2**32/2. So Euler’s famous prime can be got quickly by 2**32/2-1.
Next month: Positive integral powers of 10.