Quintus Prolog Manual
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.
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.
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
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.
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.
The following operation is included in order to allow integer arithmetic on character codes.
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
is/2 </2 >=/2 =:=/2 >//2 =\=/2 =</2
Additional arithmetic predicates can be found in library (math).
contact:
product support
sales information