P-ary “Decimals”

Pub Details: Sinclair Timex User Group Newsletter v2 n2 (Volume 2, Issue 2)
Date: February 1983
Pages: 6

8 K ROM

Last month we introduced you to “binary decimals.” Here’s a short program to convert denary (base ten) fractions to binary.

10 LET P=2
20 INPUT F
30 PRINT "THE DECIMAL FRACTION ";F-INT F,"IN ";P;"-ARY IS APPROXIMATELY 0.";
40 LET F=P*(F-INT F)
50 PRINT INT F;
60 IF F>0 THEN GOTO 40

RUN the program. Enter 0.5 and note that the answer is 0.0111111111111111111111111111111110, which is only an approximation to the correct answer 0.1; this is due to round-off error in the floating point representation. Enter 1/2 and you’ll get the exact answer. Entering SQR 2 shows that 1.0110101 is an excellent binary approximation to the square root of two.

If you want P-ary, instead of binary, change the value of P in line 10. Try P equal to 7. RUN the program and enter the function PI. Be sure to review the difference between precision and accuracy before interpreting the results.

Transcription note: the listing is corrected so it gives the answers shown in the article. As printed, line 50 reads PRINT INT F with no semicolon, which prints each digit on a new line.

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