14.2. Numbers

| 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". Definitively:

(arithmetic value) + (arithmetic value) ... value
or: (arithmetic value) plus (arithmetic value) ... value

This phrase performs signed addition on the given values, whose kinds must agree, and produces the result. Examples: 200 + 1 = 201 10:04 AM + two minutes = 10:06 AM

(arithmetic value) - (arithmetic value) ... value
or: (arithmetic value) minus (arithmetic value) ... value

This phrase performs signed subtraction on the given values, whose kinds must agree, and produces the result. Examples: 200 - 1 = 199 10:04 AM - two minutes = 10:02 AM

(arithmetic value) * (arithmetic value) ... value
or: (arithmetic value) times (arithmetic value) ... value
or: (arithmetic value) multiplied by (arithmetic value) ... value

This phrase performs signed multiplication on the given values, whose kinds must be dimensionally compatible, and produces the result. Examples: 201 times 3 = 603 two minutes times 4 = eight minutes

(arithmetic value) / (arithmetic value) ... value
or: (arithmetic value) divided by (arithmetic value) ... value

This phrase performs signed division on the given values, whose kinds must be dimensionally compatible, and produces the result. Examples: 201 divided by 3 = 67 twenty minutes divided by 4 = five minutes twenty minutes divided by five minutes = 4 Division rounds down to the nearest whole number. It is mathematically impossible to divide by 0, so any attempt to do so will cause a run-time problem message.

remainder after dividing (arithmetic value) by (arithmetic value) ... value

This phrase performs signed division on the given values, whose kinds must be dimensionally compatible, and then produces the remainder. Examples: remainder after dividing 201 by 5 = 1 remainder after dividing twenty minutes by 7 = six minutes It is mathematically impossible to divide by 0, so any attempt to find the remainder after dividing by 0 will cause a run-time problem message.

The verbal and symbolic forms of these phrases 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.)

Arithmetic often produces fussily exact answers which seem inappropriate in a conversation. Nobody says "Steeple Barton is 7.655 miles down the road", but "Steeple Barton is eight miles down the road" sounds perfectly normal. In order to make that sort of report easier to make, Inform provides another arithmetic operation, one that's not found in most computer programming languages:

(arithmetic value) to the nearest (arithmetic value) ... value

This phrase rounds the given value off, rounding upward in boundary cases. Examples: 201 to the nearest 5 = 200 205 to the nearest 10 = 210 10:27 AM to the nearest five minutes = 10:25 AM

Inform has very few mathematical functions built in as phrases, because these aren't very often needed in story-telling. But it does provide these:

square root of (arithmetic value) ... value

This phrase produces an approximate square root, to the nearest integer, of the given value, which must be of a kind which has square roots. Example: square root of 16 = 4 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 root of (arithmetic value) ... value

This phrase produces an approximate cube root, to the nearest integer, of the given value, which must be of a kind which has cube roots. Example: cube root of 27 = 3 cube root of -27 = -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

Previous | Contents | Next