Mathematically, a function is a rule for giving a number (the result) in exchange for another (the argument, or operand) and so is really a unary operation. The ZX81 has some of these built into it and their names are the words written under the keys. SQR, for instance, is the familiar square root function, and
PRINT SQR 9
gives 3, the square root of 9. (To get SQR, you first
press the FUNCTION key - shifted NEWLINE. This
changes the cursor to
. Now press the SQR key (H): SQR
appears on the screen and the cursor changes back to
. The same method works for
all the words that are written underneath the keys, almost all of
which are function names.)
Try
PRINT SQR 2
You can test the accuracy of the answer by
PRINT SQR 2*SQR 2
which ought to give 2. Note that both SQRs are worked out before the *, and in fact all functions (except one - NOT) are worked out before the five operations +, -, *, / and **. Again, you can circumvent this rule using brackets -
PRINT SQR (2*2)
gives 2.
Here are some functions (there is a complete list in appendix C). If your maths is not up to understanding some of these, it does not matter - you will still be able to use the computer.
| SGN | The sign function (sometimes called signum to avoid confusion with SIN). The result is -1, 0 or +1 according as the argument is gative, zero or positive. |
| ABS | The absolute value, or modulus. The
result is the argument made positive, so that
|
| SIN | * |
| COS | * |
| TAN | * |
| ASN | arcsin * |
| ACS | arccos * |
| ATN | arctan * |
| LN | natural logarithm (to base 2.718281828459045..., alias e) |
| EXP | exponential function |
| SQR | square root |
| INT | integer part. This always rounds down,
so INT 3.9 = 3 and INT -3.9 = -4. (An integer is a whole number, possibly negative.) |
| PI | PI = 3.1415265358979..., the
girth in cubits of a circle one cubit across. PI has no argument. (Only ten digits of this are actually stored in the computer, and only eight will be displayed.) |
| RND | Neither has RND an argument. It
yields a random number between 0 (which value it can take) and 1 (which it cannot). |
* The trigonometrical functions. These work in radians, not degrees.
Using the jargon of the last chapter, all these except PI and RND are unary operations with priority 11. (PI & RND are nullary operations, because they have no operands.)
The trigonometrical functions, and EXP, LN and SQR, are generally calculated to 8 digits accuracy.
RND and RAND: These are both on the same key, but whereas RND is a function, RAND is a keyword, like PRINT. RAND is used for controlling the randomness of RND.
RND is not truly random, but follows a fixed sequence of 65536 numbers that happen to be so jumbled up as to appear random (RND is pseudo-random). You can use RAND to start RND off at a definite place in this sequence by typing RAND, and then a number between 1 and 65535, and then NEWLINE. It's not so important to know where a given number starts RND off, as that the same number after RAND will always start RND off at the same place. For instance, type
RAND 1 (& NEWLINE)
and then
PRINT RND
and type both these in turn several times. (Remember to use FUNCTION to get RND.) The answer from RND will always be 0.0022735596, not a very random sequence.
RAND 0
(or you can miss out the 0) acts slightly differently: it judges where to srart RND off by how long the television has been on, and this should be genuinely random.
Note: In some dialects of BASIC you must always enclose the arguments of a function on brackets. This is not the case in ZX81 8K BASIC.
Statement: RAND
Functions: SGN. ABS, SIN, COS, TAN, ASN, ACS, ATN, LN, EXP, SQR, INT, PI, RND
FUNCTION
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 and 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, and much to be preferred.
2. EXP & LN are mutually inverse functions in the same sense that if you apply one and 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 and ASN, for COS and ACS, and for TAN and ATN. You can use this to test how accurately the computer works out these functions.
3. PI radians are 180°, so to convert from degrees to radians you divide by 180 andmultiply by PI: thus
PRINT TAN (45/180*PI)
gives tan 45° (1). To get from radians to degrees, you divide by PI and 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 and INT to get a random whole number between 1 and 6, to represent the throw of a die? (Answer: INT (RND *6) +1.)
5. Test this rule:
Suppose you choose a number between 1 and 872 and type
RAND and then your number (and 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, and 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, and 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 PI.