ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 5.2 - Functions

Exercises

1. To get common logarithms (to base 10), which are what you'd look up in log tables, divide the natural logarithm by LN 10.
For instance, to find log 2,

PRINT LN 2/LN 10

which gives the answer 0.30103.

Try doing multiplication & division using logs, using the ZX81 as a set of log tables in this way (for antilogs, see chapter 2, exercise 3). Check the answer using * and / - which are easier, quicker, more accurate, & much to be preferred.

2. EXP & LN are mutually inverse functions in the same sense that if you apply one & then the other, you get back to your original number. For instance,

LN EXP 2 = EXP LN 2 = 2

The same also holds for SIN & ASN, for COS & ACS, & for TAN & ATN. You can use this to test how accurately the computer works out these functions.

3. radians are 180°, so to convert from degrees to radians you divide by 180 & multiply by : thus

PRINT TAN (45/180*PI)

gives tan 45° (1). To get from radians to degrees, you divide by & multiply by 180.

4. Try

PRINT RND

a few times to see how the answer varies. Can you detect any pattern? (Unlikely.)

How would you use RND & INT to get a random whole number between 1 & 6, to represent the throw of a die? (Answer: INT (RND *6) +1.)

5. Test this rule:

Suppose you choose a number between 1 & 872 & type

RAND & then your number (& NEWLINE)

Then the next value of RND will be

(75 * (your number + 1) - 1)/65536

6. (For mathematicians only.)

Let p be a [large] prime, & let a be a primitive root modulo p.

Then if bi is the residue of ai modulo p (1 bi < p-1), the sequence

bi-1 / p - 1

is a cylindrical sequence of p-1 distinct numbers in the range 0 to 1 (excluding 1). By choosing a suitably, these can be made to look fairly random.

65537 is a Mersenne prime, 216-1. Use this & Gauss' law of quadratic reciprocity, to show that 75 is a primitive root modulo 65537.

The ZX81 uses p=65537 & a=75, & stores some bi-1 in memory. The function RND involves replacing bi-1 in memory by bi+1-1, & yielding the result (bi+1-1)/(p-1). RAND n (with 1 n 65535) makes bi equal to n+1.

7. INT always rounds down. To round to the nearest integer, add 0.5 first. For instance,

INT (2.9+0.5) = 3
INT (2.4+0.5) = 2
INT (-2.9+0.5) = -3
INT (-2.4+0.5) = -2

Compare these with the answers you get when you don't add 0.5

8. Try

PRINT PI, PI -3, PI -3.1, PI -3.14, PI -3.141

This shows how accurately the computer stores .

Sinclair ZX Spectrum

  Previous Page Back Next Page