Chapter 7: Expressions

BASin

Chapter 7: Expressions

Summary

Operations: +, -, *, /
Expressions, scientific notation, variable names

You have already seen some of the ways in which BASin can calculate with numbers. It can perform the four arithmetic operations +, -, * and / (remember that * is used for multiplication, and / is used for division), and it can find the value of a variable, given its name.

The example:

LET tax=sum*15/100

gives just a hint of the very important fact that these calculations can be combined. Such a combination, like sum*15/100, is called an expression: so an expression is just a short-hand way of telling the computer to do several calculations, one after the other. In our example, the expression sum*15/100 means 'look up the value of the variable called "sum", multiply it by 15, and divide by 100'.

Here is a summary of the order in which BASin evaluates mathematical expressions (sometimes called operator precedence).

Multiplications and divisions are done first. They have higher priority than addition and subtraction. Relative to each other, multiplication and division have the same priority, which means that the multiplications and divisions are done in order from left to right. When they are dealt with, the additions and subtractions come next - these again have the same priority as each other, so we do them in order from left to right.

Although all you really need to know is whether one operation has a higher or lower priority than another, the computer does this by having a number between 1 and 16 to represent the priority of each operation: * and / have priority 8, and + and - have priority 6.

This order of calculation is absolutely rigid, but you can circumvent it by using brackets: anything in brackets is evaluated first and then treated as a single number.

Expressions are useful because, whenever the computer is expecting a number from you, you can give it an expression instead and it will work out the answer. The exceptions to this rule are so few that they will be stated explicitly in every case.

You can add together as many strings (or string variables) as you like in a single expression, and if you want, you can even use brackets.

We really ought to tell you what you can and cannot use as the names of variables. As we have already said, the name of a string variable has to be a single letter followed by $; and the name of the control variable of a FOR . . . NEXT loop must be a single letter; but the names of ordinary numeric variables are much freer. They can use any letters or digits as long as the first one is a letter. You can put spaces in as well to make it easier to read, but they won't count as part of the name. Also, it doesn't make any difference to the name whether you type it in capitals or lower case letters.

Here are some examples of the names of variables that are allowed:

x

t42
this name is so long that I shall never be able to type it out again without making a mistake
now we are six
nOWWeaReSiX

(These last two names are considered the same, and refer to the same variable.)

These are not allowed to be the names of variables:

2001 [it begins with a digit]

3 bears [begins with a digit]
M*A*S*H [* is not a letter or a digit]
Fotherington-Thomas [- is not a letter or a digit]

Numerical expressions can be represented by a number and exponent. Try the following to prove the point:

PRINT 2.34e0

PRINT 2.34e1
PRINT 2.34e2

and so on up to

PRINT 2.34e15

You will see that after a while the computer also starts using scientific notation. This is because no more than fourteen characters can be used to write a number. Similarly, try

PRINT 2.34e-1

PRINT 2.34e-2

and so on.

PRINT gives only eight significant digits of a number. Try

PRINT 4294967295, 4294967295-429e7

This proves that the computer can hold the digits of 4294967295, even though it is not prepared to display them all at once.

BASin uses floating point arithmetic, which means that it keeps separate the digits of a number (its mantissa) and the position of the point (the exponent. This is not always exact, even for whole numbers. Type

PRINT 1e10+1-1e10,1e10-1e10+1

Numbers are held to about nine and a half digits' accuracy, so 1e10 is too big to be held exactly right. The inaccuracy (actually about 2) is more than 1, so the numbers 1e10 and 1e10+1 appear to the computer to be equal. For an even more peculiar example, type

PRINT 5e9+1-5e9

Here the inaccuracy in 5e9 is only about 1, and the 1 to be added on in fact gets rounded up to 2. The numbers 5e9+1 and 5e9+2 appear to the computer to be equal.

The largest integer (whole number) that can be held completely accurately is 1 less than 32 2's multiplied together (or 4,294,967,295).

The string "" with no characters at all is called the empty or null string. Remember that spaces are significant and an empty string is not the same as one containing nothing but spaces.

Try:

PRINT "Have you finished "Finnegans Wake" yet?"

When you press Enter, you will get the flashing red cursor that shows there is a mistake somewhere in the line. When the computer finds the double quotes at the beginning of "Finnegans Wake", it imagines that these mark the end of the string "Have you finished", and it then can't work out what 'Finnegans Wake' means.

There is a special device to get over this: whenever you want to write a string quote symbol in the middle of a string, you must write it twice, like this:

PRINT "Have you finished ""Finnegans Wake"" yet?"

As you can see from what is printed on the screen, each double quote is only really there once; you just have to type it twice to get it recognized.

Chapter 8

Chapter 6