When Zero Isn’t Zero

See all articles from Update April 1995
Author(s): Robert Hartung
Publication: Update Magazine
Pub Details: Update April 1995
Date: April 1995
Pages: 4

The problem posed by Basil Wentworth (January UPDATE, p. 22) seems to point to a glitch in the floating point algorithm used by Sir Clive — shades of Intel Pentium! I first became aware of it shortly after the TS2068 came out, but I presume it also existed in the ZX81 ROM I installed in my ZX80, as it does in the Spectrum and QL ROMs. I found that a program branch which depended upon a zero difference between a calculated mixed decimal value and its integer was erratic. It only worked when .00001 was added to the calculated value before the <=0 branching test was applied.

In the listing to the right, this compensation is done in line 150 of Basil’s demo. The two right columns of the printout show this produces the desired result. The left column printout shows why the calculations of the variable pg in lines 150 and 250 do not produce a true zero as they should at every tenth result of 0.1 being summed ten times. In fact, repetition of the error and round-down cause greater divergency each time a calculation is based on the one preceding it.

Compensating with +.00001 also produces a divergency, but since it is positive instead of negative, truncation of the calculated values to two decimal places will produce an expected zero result enough times for most practical purposes. STR$ and VAL (missing on the QL) may be used for this on the TS2068:

LET d$=STR$ n1
IF LEN d$<4 THEN LET d$=d$+".00"
LET n2=VAL d$( TO 4)

QL coercion allows this method:

d$=n1
IF LEN(d$)<4 THEN d$=d$ & ".00"
n2=d$(1 TO 4)

A similar error also is produced by the floating point algorithm used by Intel processors preceding the Pentium, but since the divergency is positive rather than negative, an approximation of zero at the expected places is produced similar to the one demonstrated below. This can be shown in both the GW-Basic and QBasic programs included with Microsoft DOS. Interestingly, spreadsheets such as Abacus for the QL and Quattro Pro 5.0 for DOS handle repeated floating point-integer calculations correctly.

TS2068 DEMO LISTING
10 PRINT "Left column shows non-zero sums of pg+.1-INT pg. Right columns show compensation of page+.00001"''
100 LET page=1: LET pg=1
110 FOR f=1 TO 60
130 REM .00001 added to page in line 150
150 LET page=page+.1+.00001: LET pg=pg+.1
160 LET panel=page-INT page
165 LET panel=.1*INT (10*panel)
250 PRINT pg-INT pg;TAB 16;INT page+panel;TAB 24;panel
1000 NEXT f

Regarding a “glitch” of another sort, during the past year my responsibilities as chaplain for a county jail and sheriff department, plus trying to pretend I was a hard-hat construction worker while helping my brother build a Quonset-type machinery storage barn, greatly curtailed the time available for computer-surfing. I’m not complaining but, in fact, am glad that at +70 (non-floating point!) years I could still climb a scaffold and tighten about 8000 bolts. However, during that time I discovered that both disk drives for my TS2068 as well as the two for my QL had become ill and were corrupting disks whenever a write was attempted.

Fortunately, I can now access my backups, thanks to Dan Elliott having replacement drives, although I still need time to restore everything. The moral is backup, backup, backup! By the way, if your floppy drives are repairable, ISR in Leander, TX, does an excellent job at a reasonable cost, phone (800) 458-6778.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.