next up previous contents
Next: Additional facilities for Up: CHAPTER.5: NUMERICAL AND Previous: Types of numbers

Forming numerical expressions

Any of the above forms of numerical expression can be used where Pop-11 requires an expression, e.g. to assign values to variables, or to give arguments to procedures requiring numerical arguments. Mathematical procedures provided by Pop-11 are listed below, including the standard arithmetical operators (+, -, *, /), trigonometrical functions (sin, cos, tan) and others.

Starting from expressions denoting numbers we can create more complicated expressions denoting numbers. E.g.

    33 + 44     denotes the number 7

    sqrt(9)     denotes the number 3.0

    max(4, 77)  denotes the number 77.
Arithmetic expressions can be embedded in others. For example the expression

        tan(23 + 22)
contains sub-expressions 23, 22, and 23 + 22.

Finally

    max(min(66, 33), min(999, 9))
denotes the number 33, as does this:

    max( min(66-5, 33), min(999, 9+9) ) =>
    ** 33
Exercise: How many different expressions does the last example contain?

Using a radix other than 10 to represent numbers on input

Pop-11 will read in binary numbers if they are preceded by `2:'

    2:111 =>
    ** 7
Similarly octal numbers may be preceded by `8:'

    8:111 =>
    ** 73
This sort of prefix can be used for non-integer numbers too:

    8:11.11 =>
    ** 9.140625

    10:55.55 =>
    ** 55.55
The integer base used for reading in non-decimal numbers must be in the range 2-36. If the base is greater than 10, the letters A-Z (uppercase only) can be used in the number to represent digit values from 10 to 35, e.g. 16:1FFA represents 8186 as a hexadecimal number.

Note: the same internal representation is used, no matter how the number is read in. So how the number prints out is not affected. E.g.

    2:111, 7 =>
    ** 7   7

    2:111 == 7 =>
    ** <true>

Using pop_pr_radix to control radix used in printing

How numbers are printed is controlled using pop_pr_radix, which defaults to 10. By making it 16 numbers can be printed in hexadecimal, for instance:

    16 -> pop_pr_radix;
    16 =>
    ** 10
    15 =>
    ** F
You can define a procedure to print numbers in binary form thus:

    define pr_binary(num);
        dlocal pop_pr_radix = 2;
        pr(num)
    enddefine;

    vars x;
    for x from 1 to 15 do pr_binary(x); pr(space) endfor;
    1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

pop_pr_radix, pop_pr_places and pop_pr_exponent

The printing of decimal (floating point) numbers is also controlled by pop_pr_radix.

    16 -> pop_pr_radix;
    15.55 =>
    ** F.8CCCCD
Compare

    pr_binary(15.55);
    1111.100011
The number of decimal places shown is controlled by pop_pr_places, which defaults to 6.

    10 -> pop_pr_radix;
    sqrt(2) =>
    ** 1.41421

    2 -> pop_pr_places;
    sqrt(2) =>
    ** 1.41

    6 -> pop_pr_places;
    sqrt(2) =>
    ** 1.41421
A value of 0 causes decimal numbers to be printed as integers.

If pop_pr_exponent is made true (it defaults to false) then exponent form is used for printing decimals and ddecimals:

    true -> pop_pr_exponent;
    sqrt(20000) =>
    ** 1.41421e+2
    false -> pop_pr_exponent;
    sqrt(20000) =>
    ** 141.421
As explained above, the exponent notation can also be used for reading in decimals:

    1.41421e+2 =>
    ** 141.421


next up previous contents
Next: Additional facilities for Up: CHAPTER.5: NUMERICAL AND Previous: Types of numbers



Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998