Real numbers
Keywords: pame

Subsections

- Exercises

#
Real numbers

Numbers which contain fractional parts, such as 3.5 or 0.0005623956, or numbers expressed in scientific notation, such as 1.9534 are values of mode `REAL`. Reals are denoted by digits and one at least of the decimal point (which is denoted by a full stop), or the letter `e`. The `e` means ×10some power. Just as with integers, there are no denotations for negative reals. When the exponent is preceded by a minus sign, this does not mean that the number is negative, but that the decimal point should be shifted leftwards. For example, in the following `REAL` denotations, the third denotation has the same value as the fourth (again, the denotations are separated by commas, but the commas are not part of the denotations):

       4.5,  .9,  0.000 000 003 4,  3.4e-9,  1e6

Although the second denotation is valid, it is advisable in such a case to precede the decimal point with a zero: `0.9`. This is better because a decimal point not preceded by an integer can be missed easily. Here are some identity declarations for values of mode `REAL`:

       REAL e = 2.718 281 828,
            electron charge = 1.602 10 e-19,
            monthly salary = 2574.43

The largest `REAL` which the compiler can handle is declared in the standard prelude as `max real`. Its value is

    1.79769313486231571e308

The value of π is declared in the standard prelude with the identifier `pi` and a value of

       REAL pi = 3.141592653589793238462643

It was mentioned above that in an identity declaration, any piece of program yielding a value of the required mode can be used as the value. Here, for example, is an identity declaration where the value has mode `INT`:

       REAL a = 3

However, the mode required is `REAL`. In certain circumstances, a value of one mode can be coerced into a value of another mode. These circumstances are known as contexts. There are five contexts defined in the language. Each context will be mentioned as it occurs. The right-hand side of an identity declaration has a strong context. In a strong context, a value and its mode can be changed according to six rules, known as coercions, defined in the language. Again, each coercion will be explained as it occurs. The coercion which replaces a value of mode `INT` with a value of mode `REAL` is known as widening. You will meet a different kind of widening in section 7.4.

You can even supply an identifier yielding the required mode on the right-hand side. Here are two identity declarations:

       REAL one = 1.0;
       REAL one again = one

You cannot combine these two declarations into one with a comma as in

       REAL one = 1.0, one again = one

because you cannot guarantee that the identity declaration for `one` will be elaborated before the declaration for `one again` (because the comma is not a go-on symbol).2.2

Values of modes `INT`, `REAL` and `CHAR` are known as plain values. We shall be meeting another mode having plain values in chapter 4, and modes in chapter 3 which are not plain. Complex numbers are dealt with in chapter 7.

## Exercises

1.10
  Is there anything wrong with the following identity declarations?

         REAL x = 5.,
              y = .5;
              z = 100

  Ans
1.11
  Given that light travels 2.997 925×108 metres per second in a vacuum, write an identity declaration for the identifier `light year` in terms of metres to an accuracy of 5 decimal places (use a calculator). Ans