Significant Figures

Developer(s): N. Handshaw Clark
Date: 1982
Type: Program
Platform(s): ZX81, TS 1000

The subroutine at line 1000 rounds the number in Z to SF significant figures. The lines in front of it are a short demonstration: RUN asks HOW MANY SIGNIFICANT FIQURES DO YOU WISH? (printed that way) and then for a number, and prints the rounded value.

Powers of ten are set with 1E1 to 1E13 in lines 1190-1310 because 10**I is not exact on the ZX81. In testing, 0.000098765432 to 6 figures gave .0000987654 and 123456.78 to 3 figures gave 123000. It runs in 2K; in a 1K ZX81 it did not run.

Published in Syntax September 1982 (Significant Figures Subroutine).

Related Articles

Display the significant figures of a number.

Image Gallery

Source Code

  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

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