Significant Figures Subroutine

Author(s): N. Handshaw Clark
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n9 (Volume 3, Issue 9)
Date: September 1982
Pages: 16-17

In scientific calculations the accuracy of a computed value must not exceed that of the least accurately known number used in the computation. You can use the following subroutine (at 1000 in this program) in any program containing the proper input, i.e., SF and Z, to specify the number of significant figures in any expression.

Because the ZX81 holds 10**1, 10**2, etc., slightly inaccurately (see part 2 of Dr. O’Hara‘s article, Apr.82), using QE=10**I might give erroneous results. For example, if SF=6 and Z=0.000098765432, using QE=10**10 would produce 0.000098765399. In subroutine 1180, 1E1 through 1E13, which are held exactly, are substituted for 10**1 through 10**13. For Z=0.000098765432 and SF=6, this gives the correct value, 0.0000987654, with the desired number of significant figures. (Note that the ZX81 will not print the last significant figure if it is a zero to the right of the decimal point. It will, however, be counted as a significant figure by the subroutine.)

Use this subroutine as often as necessary by repeating the GOSUB command after assigning each new value to Z. The ZX81 can handle up to eight significant figures.

  10 REM "ANY PROGRAM"
20 PRINT "HOW MANY SIGNIFICANT FIQURES DO YOU WISH?"
30 INPUT SF
40 CLS
50 PRINT "INPUT NUMBER:"
60 INPUT Z
70 CLS
80 GOSUB 1000
90 PRINT Z
100 STOP
1000 REM "SIGNIFICANT FIQURES"
1010 FAST
1020 LET I=SF
1030 GOSUB 1180
1040 LET ESF=QE
1050 IF ABS Z>=(ESF) THEN GOTO 1120
1060 FOR I=0 TO 30
1070 GOSUB 1180
1080 LET W=INT ((ABS Z+(0.5/QE))*QE)
1090 IF W<(ESF/1E1) THEN NEXT I
1100 LET Z=(Z/(ABS Z))*(W/QE)
1110 RETURN
1120 FOR I=0 TO 30
1130 GOSUB 1180
1140 LET W=INT ((ABS Z+(0.5*QE))/QE)
1150 IF W>=(ESF) THEN NEXT I
1160 LET Z=(Z/(ABS Z))*W*QE
1170 RETURN
1180 LET QE=10**I
1190 IF I=1 THEN LET QE=1E1
1200 IF I=2 THEN LET QE=1E2
1210 IF I=3 THEN LET QE=1E3
1220 IF I=4 THEN LET QE=1E4
1230 IF I=5 THEN LET QE=1E5
1240 IF I=6 THEN LET QE=1E6
1250 IF I=7 THEN LET QE=1E7
1260 IF I=8 THEN LET QE=1E8
1270 IF I=9 THEN LET QE=1E9
1280 IF I=10 THEN LET QE=1E10
1290 IF I=11 THEN LET QE=1E11
1300 IF I=12 THEN LET QE=1E12
1310 IF I=13 THEN LET QE=1E13
1320 RETURN
SYNTACTIC SUM: 60191, 8K

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