Manual: Arithmetic


Quintus Prolog Manual


(PREV) (NEXT)

G-8: Arithmetic

G-8-0: Overview

In Prolog, arithmetic is performed by certain built-in predicates which take arithmetic expressions as their arguments and evaluate them. Arithmetic expressions can evaluate to integers or floating-point numbers (floats).

With Release 3 Quintus Prolog has full 32 bit integer arithmetic and full 64 bit double precision floating point arithmetic. The range of integers is -2147483648 (-2^31) to 2147483647 (2^31-1) both inclusive. Arithmetic operations like integer addition and multiplication raise a representation error if there is an overflow.

The range of floating-point numbers is approximately 2.3E-308 to 1.7E+308. Floats are represented by 64 bits and they conform to the IEEE 754 standard. The behavior on floating-point overflow or underflow is machine-dependent.

Chapter summary: The arithmetic operations of evaluation and comparison are implemented in the predicates described in section G-8-1 and section G-8-2. All of them take arguments of the type "Expr", which is described in detail in section G-8-3.

G-8-1: Evaluating Arithmetic Expressions

The most common way to do arithmetic calculations in Prolog is to use the built-in predicate is/2.

Term must not contain any uninstantiated variables. Do not confuse is/2 with =/2.

G-8-2: Arithmetic Comparison

Each of the following predicates evaluates each of its arguments as an arithmetic expression, then compares the results. If one argument evaluates to an integer and the other to a float, the integer is coerced to a float before the comparison is made.

Note that two floating-point numbers are equal if and only if they have the same bit pattern. Because of rounding error, it is not normally useful to compare two floats for equality.

G-8-3: Arithmetic Expressions

Arithmetic evaluation and testing is performed by predicates that take arithmetic expressions as arguments. An arithmetic expression is a term built from numbers, variables, and functors that represent arithmetic functions. These expressions are evaluated to yield an arithmetic result which may be either an integer or a float; the type is determined by the rules described below.

At the time of evaluation, each variable in an arithmetic expression must be bound to a number or another arithmetic expression. If the expression is not sufficiently bound or if it is bound to terms of the wrong type then Prolog raises exceptions of the appropriate type (section G-19-3). Some arithmetic operations can also detect overflows. They also raise exceptions. e.g. Division by zero results in a domain error being raised.

Only certain functors are permitted in arithmetic expressions. These are listed below, together with a description of their arithmetic meanings. For the rest of the section, X and Y are considered to be arithmetic expressions.

G-8-3-1: Arithmetic calculations

X + Y
Evaluates to the sum of X and Y. If both operands are integers, the result is an integer; otherwise, the result is a float. If integer addition results in an overflow, a representation error is raised.
X - Y
Evaluates to the difference of X and Y. If both operands are integers, the result is an integer; otherwise, the result is a float. If integer subtraction results in an underflow, a representation error is raised.
X * Y
Evaluates to the product of X and Y. If both operands are integers, the result is an integer; otherwise, the result is a float. If integer multiplication results in an overflow, a representation error is raised.
- X
Evaluates to the negative of X. The type of the result, integer or float, is the same as the type of the operand.
abs(X)
Evaluates to X if X is a positive number, -X if it is a negative number.
X / Y
Evaluates to the quotient of X and Y. The result is always a float, regardless of the types of the operands X and Y. Attempt to divide by zero results in a domain error being raised.
X // Y
Evaluates to the integer quotient of X and Y. X and Y must both be integers. The result is truncated to the nearest integer that is between it and 0. Attempt to divide by zero results in a domain error being raised.
X div Y
Equivalent to '//'.
X mod Y
Evaluates to the remainder after the integer division of X by Y. X and Y must both be integers. The result, if non-zero, has the same sign as X. If Y evaluates to 0, a domain error is raised.
integer(X)
Evaluates to X if X is an integer. Otherwise (if X is a float) the result is the nearest integer that is between it and 0.
float(X)
Evaluates to X if X is a float. Otherwise (if X is an integer) the result is the floating-point equivalent of X.
min(X,Y)
Evaluates to the minimum of X and Y.
max(X,Y)
Evaluates to the maximum of X and Y.

G-8-3-2: Peeking into Memory

The following operations can be used to peek into memory. They can be used in conjunction with the foreign interface to peek into data structures within foreign code from Prolog. These operations take an integer argument and access the data stored at the address represented by the argument. Note that these operations can result in segmentation faults and bus errors if the argument you are trying to access is a bad address or if the address is not aligned properly for the data you are going to access from it. The only sure way of getting an integer in Prolog that represents an address that makes sense is by returning an address from a foreign function through the foreign language interface (see section I-3-9). For built-ins that poke ("store") values into memory, see the reference page for assign/2 in the reference section. For a more structured way of doing this, see the structs package.

integer_8_at(X)
Evaluates to the signed byte stored at address X.
unsigned_8_at(X)
Evaluates to the unsigned byte stored at address X.
integer_16_at(X)
Evaluates to the signed short stored at address X.
unsigned_16_at(X)
Evaluates to the unsigned short stored at address X.
integer_at(X)
Evaluates to the signed integer stored at address X.
address_at(X)
Evaluates to the address stored at address X.
single_at(X)
Evaluates to the single precision floating point number stored at address X.
double_at(X)
Evaluates to the double precision floating point number stored at address X.

G-8-3-3: Bit-vector Operations

The following bit-vector operations apply to integer arguments only. Supplying non-integer arguments results in an exception being raised. Note that the slant lines used in these operator names are produced with the forward and backward slash keys.

X / \ Y
Evaluates to the bitwise conjunction of X and Y.
X \ / Y
Evaluates to the bitwise disjunction of X and Y.
\(X,Y)
Evaluates to the bitwise xor of X and Y. Note that this is not an operator.
\ (X)
Evaluates to the complement of the bits in X.
X << Y
X is shifted left Y places. Equivalent to 'X << (Y /\ 2'11111)'.
X >> Y
X is shifted right Y places with sign extension.

G-8-3-4: Character Codes

The following operation is included in order to allow integer arithmetic on character codes.

[X]
Evaluates to X for numeric X. This is relevant because character strings in Prolog are lists of character codes, that is, integers. Thus, for those integers which correspond to character codes, the user can write a string of one character in place of that integer in an arithmetic expression. For example, the expression (A) is equivalent to (B), which in turn becomes (C) in which case X is unified with 2:
X is "c" - "a" (A)

                       X is [99] - [97]                       (B)

               

                       X is 99 - 97                           (C)

               
A cleaner way to do the same thing is

                       X is 0'c - 0'a

               

G-8-4: Predicate Summary

is/2 </2 >=/2 =:=/2 >//2 =\=/2 =</2

G-8-5: Library Support

Additional arithmetic predicates can be found in library (math).


Copyright (C) 1998 SICS
contact: product support sales information