§15.2. Numbers and real numbers
Inform uses two different kinds of numerical quantity: "number" and "real number". Neither is better than the other: they're different approaches, each good for a different purpose.
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 the Z-machine, 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.
If we're using Glulx, Inform also has "real numbers" such as
2.1718, 4.0, -1633.9
which are not restricted to whole numbers, but which are stored only approximately: only about six to nine decimal digits can be relied on. For example,
real number: 1.23457
real number: 1.23457
because these two numbers are so close together that Inform can't tell them apart. But we do also get the ability to represent enormously large or small quantities, and to help with that, Inform can read and write "scientific notation". For example,
let Avogadro's number be 6.022141 x 10^23;
is equivalent to typing
let Avogadro's number be 602214100000000000000000.0;
The "x 10^23" part tells Inform that the decimal point belongs 23 places to the left of where it's written. (In scientific papers, the 23 would be printed as a superscript -- it's 10 to the power 23 -- but that's not convenient to type in to the source text, so we use the "^" symbol to indicate superscript.) The range we can hold is roughly:
1.18 x 10^−38 to 3.4 x 10^38
It's hard to convey just how enormously different these two numbers are: if we used them to measure widths in meters, one would be a hundred trillion trillion times smaller than an atom, the other a billion times larger than the entire visible universe. Scientific notation is the ultimate adjustable spanner.
Inform also allows the two most famous real numbers in mathematics to be given by their names:
which are close to 3.14159265 and 2.7182818 respectively. (Lower case letters must be used: these can't be written "Pi" or "E". Euler's constant gamma, always in the bronze medal position, will have to be written out longhand as 0.5772156649.)
Most computer programming languages traditionally write floating-point numbers using the E notation, like so:
Inform will follow suit if the use option "Use engineering notation." is set, but by default it isn't.
|Start of Chapter 15: Numbers and Equations|
|Back to §15.1. How do we measure things?|
|Onward to §15.3. Real number conversions|
Seven-digit telephone numbers are too long for Inform to handle when compiling to the Z-Machine, but they will work under Glulx. To have this example succeed, make sure that you have selected the Glulx option in your settings menu.
A telephone is a kind of thing. Understand "phone" or "telephone" as a telephone.
A phone number is a kind of value. 999-9999 specifies a phone number.
Now we borrow some techniques from the Understanding chapter to set up dialing actions:
Understand "dial [phone number] on [telephone]" as dialing it on. Understand "dial [phone number] on [something]" as dialing it on.
Understand the commands "phone" or "telephone" or "call" as "dial".
Understand "call [text]" or "phone [text]" or "dial [text]" or "telephone [text]" as a mistake ("That's not a number you know.").
Dialing it on is an action applying to one phone number and one thing.
Report dialing it on:
say "You dial [the phone number understood]."
This much is enough to let us dial telephone numbers and have Inform report that we've done so; it doesn't actually provide a telephone system such that we could reach and converse with other characters (but see the other telephone examples in the recipe book for more on how one might do that).
We'll set up a little political espionage scenario from which our player can make calls:
The Senator's Junior Suite is a room. "The Senator appears, unfortunately, to have very precise habits: little in the room has been moved from its usual place; the trash can is empty; the bed has been remade[if the blue paper is unexamined]. There may in fact be nothing to find here[end if]."
The bed is an enterable scenery supporter in the Junior Suite.
The player is wearing a housekeeping uniform and a brunette wig. The player carries a telephone called a Nokia.
Borrowing again from the chapter on Understanding, we might arrange things so that the player knows and can call a few standard numbers with such syntax as CALL HOME:
Understand "home" as 555-9200.
And what if we'd like to have the player learn some phone numbers during the game?
A thing can be examined or unexamined. Carry out examining something: now the noun is examined.
Understand "Stephen" as 555-2513 when the blue paper is examined.
This will understand CALL STEPHEN once the paper is examined; before that, the player will just get the "That's not a number you know" response that Inform uses for all attempts to call unknown names.
We'd better plant this paper for the player to find:
The blue paper is in the drawer. The description of the blue paper is "It reads: 'Call Stephen - 555-2513'."
The drawer is part of the dresser. It is closed and openable. The dresser is in The Senator's Junior Suite. The lamp is on the dresser. The description of the dresser is "The single drawer is [if the drawer is open]open[otherwise]shut[end if]."
Test me with "dial 555-9999 / call home on the telephone / phone the president / call stephen / open drawer / read paper / call stephen / put phone in drawer / close drawer / call stephen".