Chapter 14: Numbers and Equations
14.2. Numbers

What Inform calls a "number" is a whole number, positive, negative or zero. The range of numbers we can hold is not unlimited - if the format Setting for a project is one of the Z-machine choices, then we have:

-32768, -32767, ..., -3, -2, -1, 0, 1, 2, 3, ..., 32767

and if it is set to Glulx, then we have:

-2147483648, -2147483647, ..., -3, -2, -1, 0, 1, 2, 3, ..., 2147483647

Numbers from zero to twelve may be written out, but larger ones must be written as numerals. So "twelve" or "12", but "13" only.

We are allowed to perform about the same operations on numbers as are provided by a simple office calculator, starting with addition, subtraction, multiplication and division. We can use the traditional typewriter symbols for these, +, -, * and /, or can spell them out in words as "plus", "minus", "times" (or "multiplied by"), and "divided by". For example, these are equivalent:

the score + 10
the score plus 10

It's probably better style to spell them out in full when writing text, and keep the symbols for writing equations, as we'll see later on in the chapter. (If we do use the symbols, then spaces around them are obligatory: to Inform, they are words which just happen to be spelt with symbols instead of letters.) Some more examples:

2 minus 5
10 times 21
10 multiplied by 21
144 divided by 12
the square root of 144
the cube root of 27
1232 to the nearest 10

With addition and subtraction, what we often want is to add or subtract some amount from a named variable, and a convenient abbreviation is provided for this:

increase the target by 10;
decrease the target by 5;

Division rounds down to the nearest integer. If we try dividing 26 by 3, we will get the answer 8: but 3 does not go into 26 exactly. We can obtain the remainder, if we want to, like so:

remainder after dividing 26 by 3

the value of which is 2, because 3 goes into 26 eight times, with remainder 2. It is mathematically impossible to divide by 0, so any attempt to do so will cause a run-time problem message.

Extracting square and cube roots is also approximate, of course. Inform says that "the square root of 10" is 3 - the true figure is a little more than this, but it can't be represented as a whole number. Trying to take the square root of a negative number will cause a run-time problem, because then we can't even nearly solve it. Cube roots can always be taken - for instance, "the cube root of -27" is -3.

We can compare numbers using either the traditional computer-programming symbols, or using words:

if the score is less than 10
if the score < 10

and similarly for "greater than", "at least" and "at most", with the symbols ">", ">=" and "<=". But we are not allowed the equals sign: for that we need only use "is" -

if the score is 10


PreviousContentsNext