Arithmetic

To allow for normal math functions, Prolog supports arithmetic evaluation of certain built-in predicates.

Arithmetic evaluation means that a structure, such as 5 + 3, is not treated as Prolog normally would, that is as '+'(5, 3) which is no different from foo(5, 3). Instead, in this case, it is evaluated arithmetically.

This section documents a number of built-in predicates that all perform arithmetic evaluations on structures. If a structure is not amenable to being evaluated arithmetically, then it fails. This allows you to check for this type of error condition.

The most commonly used predicate for arithmetic evaluation is is/2. It evaluates a structure on its right and unifies the result with the left. For example

The behavior of is/2 is very different from normal Prolog unification, in which

There is no assignment in Prolog, so you need to use more than one variable to express the normal programming construct of incrementing a variable. Here, for example, is a simple loop that writes something N times.

Note that the various arithmetic operators are only evaluated when used by a predicate that evaluates arithmetic expressions. So, for example, the trigonometric functions might look like predicates, but they are actually simply evaulatable expressions. In an arithmetic expression, sin(X) is replaced with the value of sin(X). Used anywhere else it is a meaningless functor and argument.

X is Y

Succeeds if X can be unified with the value of Y evaluated as an arithmetic expression.

Arithmetic Comparisons

X >= Y

Greater or equal.

X =< Y

Less than or equal.

X > Y

Greater than.

X < Y

Less than.

X =:= Y

X =:= Y succeeds if X evaluated is arithmetically equal to Y evaluated; else it fails.

X =\= Y

X =\= Y succeeds if X evaluated is not arithmetically equal to Y; else it fails.

Note that in evaluating arithmetic comparisons, X and Y are first evaluated and the appropriate test then succeeds or fails according to the inequalities' being true or false.

It is a bad Prolog practice to test for numerical (in)equality by using =, \=, == or \==.

Arithmetic Operators

X + Y

Sum of values of X and Y.

X - Y

Value of X minus value of Y.

X * Y

Value of X multiplied by value of Y.

X / Y

Value of X divided by value of Y. Always returns a double precision floating point value.

X // Y

Integer division of X by Y-truncates result to the absolute integer. That is, 11 // 4 =:= 2 and -11 // 4 =:= -2.

X divs Y

Integer division such that the remainder is >= -Y/2 and < Y/2.

X divu Y

Integer division such that the remainder is positive.

X mod Y

The remainder after dividing the value of X by the value of Y.

X mods Y

The remainder, constrained so that the result is >= -Y/2 and < Y/2.

X modu Y

The remainder, constrained so that the result is positive.

For the following bitwise operators the arguments must be 16 bit integers.

X /\ Y

Bitwise "and" of value of X and value of Y.

X \/ Y

Bitwise "or" of value of X and value of Y.

X << Y

Evaluates to X bit-shifted left by Y places. Note this is an arithmetic shift (does not include the sign bit). So 0 is 1 << 16.

X >> Y

Evaluates to X bit-shifted right by Y places. Again watch the sign bit e.g. -4 >> 1 =:= -2 .

X ** Y

Evaluates to X raised to the Y power.

\ X

Evaluates to the bitwise complement of X (i.e., all those bits which were 1 become 0 and vice versa).

- X

Evaluates to the negative of X evaluated.

Arithmetic Functions

acos(X)

acos evaluates to the angle (in radians) whose arccosine is X.

asin(X)

asin evaluates to the angle (in radians) whose arcsine is X.

atan(X)

atan evaluates to the angle (in radians) whose arctangent is X.

cos(X)

cos evaluates to the cosine of X (in radians)

exp(X)

exp evaluates to e raised to the power of X evaluated.

float(X)

float converts X to a double precision floating point number.

integer(X)

integer converts X to an integer (truncating any fractional part).

ln(X)

ln evaluates to the natural log (loge()) of X evaluated.

seed_random(I)

seed_random/1 is a predicate that seeds the random number generator. It takes an integer argument.

sin(X)

sin evaluates to the sine of X (in radians).

sqrt(X)

sqrt evaluates to the square root of X.

tan(X)

tan evaluates to the tangent of X (in radians).

Built-in Atoms

There are a number of built-in atoms, which have predetermined values that can be used in arithmetic expression.

cputime
A floating point number with the number of CPU seconds expired. It is useful for timing functions, for example:
?- T1 is cputime, dothing, T is T1 - cputime, write(time:T).
e
The value "e" (2.718282..)
pi
The value "pi" (3.14159 ..)
random
A random floating point number >= 0.0 and < 1.0

Copyright ©1987-2000 Amzi! inc. All Rights Reserved.