CHAPTER 9    Arithmetic and Comparison

 

Visual Prolog's arithmetic and comparison capabilities are similar to those provided in programming languages such as BASIC, C, and Pascal. Visual Prolog includes a full range of arithmetic functions; you have already seen some simple examples of Visual Prolog's arithmetic capabilities.

In this chapter we summarize Visual Prolog's built-in predicates and functions for performing arithmetic and comparisons, as well as a two arity versions of a standard predicate used for random number generation. We'll also discuss comparison of strings and characters.

Arithmetic Expressions

Arithmetic expressions consist of operands (numbers and variables), operators (+, -, *, /, div, and mod), and parentheses. The symbols on the right side of the equal sign (which is the = predicate) in the following make up an arithmetic expression.

    A = 1 + 6 / (11 + 3) * Z

Leading "0x" or "0o" signify hexadecimal and octal numbers, respectively, e.g.

    0xFFF = 4095
        86 = 0o112 + 12

The value of an expression can only be calculated if all variables are bound at the time of evaluation. The calculation then occurs in a certain order, determined by the priority of the arithmetic operators; operators with the highest priority are evaluated first.

1. Operations

Visual Prolog can perform all four basic arithmetic operations (addition, subtraction, multiplication, and division) between integral and real values; the type of the result is determined according to Table 9.1.

Table 9.1 Arithmetic Operations

Operand 1

Operator

Operand 2

Result

integral

+, -, *

integral

integral

real

+, -, *

integral

real

integral

+, -, *

real

real

real

+, -, *

real

real

integral or real

/

integral or real

real

integral

div

integral

integral

integral

mod

integral

integral

In case of mixed integral arithmetic, involving both signed and unsigned quantities, the result is signed. The size of the result will be that of the larger of the two operands. Hence, if a ushort and a long are involved the result is long; if a ushort and a ulong are involved the result is ulong.

2. Order of Evaluation

Arithmetic expressions are evaluated in this order:

If the expression contains sub-expressions in parentheses, the sub-­expressions are evaluated first.

If the expression contains multiplication (*) or division (/, div or mod), these operations are carried out next, working from left to right through the expression.

Finally, addition (+) and subtraction (-) are carried out, again working from left to right.

Hence, these are the operator precedence:

Table 9.2 Operator Precedence

Operator

Priority

+ -

1

* / mod div

2

- + (unary

3

In the expression A = 1 + 6/(11+3)*Z, assume that Z has the value 4, since variables must be bound before evaluation.

(11 + 3) is the first sub-expression evaluated, because it's in parentheses; it evaluates to 14.

Then 6/14 is evaluated, because / and * are evaluated left to right; this gives 0.428571.

Next, 0.428571 * 4 gives 1.714285.

Finally, evaluating 1 + 1.714285 gives the value of the expression as 2.714285.

A will then be bound to 2.714285 which makes it a real value.

However, you should exercise some care when handling floating point (real) quantities. In most cases they're not represented accurately and small errors can accumulate, giving unpredictable results. An example follows later in the chapter.