§15.3. Real number conversions
This section notes down some technicalities about real numbers which need to be put down in writing somewhere, but won't affect most people most of the time.
Inform allows us to use numbers whenever real numbers are expected, and converts them automatically. For example,
cosine of 2
is read as if it were
cosine of 2.0
and produces -0.41615 either way. This conversion goes from exactness to approximation, so we may lose a little accuracy: real numbers measure to an accuracy of about 1 part in 16000000, so they'll have trouble telling the difference between 16000000 and 16000001. But this is unlikely to matter, since real numbers are used only for approximate calculations anyway.
The ordinary arithmetic operations work on both numbers and real numbers, so the meaning of "N plus M" depends on the kinds of N and M. In general the rule is that if either is a real number then the other one is automatically converted, and real arithmetic is used. So:
3 divided by 2 = 1
3 divided by 2.0 = 1.5
3.0 divided by 2 = 1.5
3.0 divided by 2.0 = 1.5
In general we can't do the reverse, that is, we can't silently use a real number where a number is expected. For example,
word number 1.6 in "The Great Wall of China"
makes no sense. But we can explicitly convert them:
(real number) to the nearest whole number ... number
This phrase performs signed addition on the given values, whose kinds must agree, and produces the result. Examples:
1.4 to the nearest whole number = 1
1.6 to the nearest whole number = 2
-1.6 to the nearest whole number = 2
We probably ought to bear in mind that the limited range of "number" means that the nearest whole number might not be all that near. For example:
6 x 10^23 to the nearest whole number = 2147483647
because 2147483647 is the highest value a "number" can have.
Finally, real number can also store two interesting not-really-number sorts of value. First, we have
plus infinity, minus infinity
which are used to keep track of what happens when we divide by really small quantities. It's mathematically impossible to divide by 0, but this can be hard to avoid when we're using real numbers, because they're only approximately stored - so it's not always possible to say whether they're exactly 0 or not. So in real number arithmetic,
showme 1.0 divided by 0.0;
doesn't throw a run-time problem the way that
showme 1 divided by 0;
does. Instead, it produces "plus infinity". Infinity behaves roughly the way we might expect - for example, "2 divided by plus infinity" produces 0 - but once it comes into a calculation the result probably lies on some extreme and won't be very useful. Amusingly, the following is correct Inform syntax:
plus infinity to the nearest whole number
and evaluates of course to 2147483647. We can use the adjectives "infinite" and "finite" to talk about these numbers: plus infinity and minus infinity are infinite, everything else is finite.
The same problem occurs for calculations like square roots. It's impossible to take the square root of a negative number, but we don't want to throw a run-time problem, because approximation means we can't always guarantee to stay the right side of 0. So for a few calculations like this, Inform generates what's called a "nonexistent" real number. We can use the adjectives nonexistent or existent to talk about this. Every number mentioned on this page so far is "existent", including the infinities. The only way to get a nonexistent number is to carry out an impossible mathematical operation such as
logarithm of -10
(The design of "real number" here follows well established trade-offs for scientific computing. Inform follows the IEEE-754 binary32 standard for floating-point arithmetic, so Inform's "real number" behaves very like the "float" type in C, C++, Java and similar programming languages. A "nonexistent" number is what's often called a NaN - a Not-a-Number.)