Arithmetic is performed by built-in predicates, which take as arguments arithmetic expressions and evaluate them. An arithmetic expression is a term built from numbers, variables, and functors that represent arithmetic functions. At the time of evaluation, each variable in an arithmetic expression must be bound to a non-variable expression. An expression evaluates to a number, which may be an integer or a float.
The range of integers is [-2^2147483616, 2^2147483616)
. Thus for
all practical purposes, the range of integers can be considered
infinite.
The range of floats is the one provided by the C double
type,
typically [4.9e-324, 1.8e+308]
(plus or minus). In case of
overflow or division by zero, iso
execution mode will raise an
evaluation error exception. In sicstus
execution mode no
exceptions will be raised, instead appropriate infinity values, as
defined by the IEEE standard, will be used.
Only certain functors are permitted in an arithmetic expression.
These are listed below, together with an indication of the functions
they represent. X and Y are assumed to be arithmetic
expressions. Unless stated otherwise, the arguments of an
expression may be any numbers and its value is a float if any of its
arguments is a float; otherwise, the value is an integer. Any
implicit coercions are performed with the integer/1
and
float/1
functions.
The arithmetic functors are annotated with [ISO], [ISO only], or [SICStus only], with the same meaning as for the built-in predicates; see ISO Compliance.
+(
X)
-
X+
Y-
Y*
Y/
Y//
Y ISOiso
execution mode X and Y have to be integers.
rem
Y ISOinteger(
X)-integer(
Y)*(
X//
Y)
. The sign of
a nonzero remainder will thus be the same as that of the dividend.
In iso
execution mode X and Y have to be integers.
mod
Y ISO onlyinteger(
X)-integer(
Y)*floor(
X/
Y)
.
The sign of
a nonzero remainder will thus be the same as that of the divisor.
X and Y have to be integers.
mod
Y SICStus onlyinteger(
X)
float_integer_part(
X)
ISOfloat(integer(
X))
. In iso
execution mode,
X has to be a float.
float_fractional_part(
X)
ISO -
float_integer_part(
X)
. In iso
execution mode, X has
to be a float.
float(
X)
ISO/\
Y ISOiso
execution mode X and Y have to be integers.
\/
Y ISOiso
execution mode X and Y have to be integers.
#
Y\(
X)
ISOiso
execution mode X has to be an integer.
<<
Y ISOiso
execution mode X and Y have to be integers.
>>
Y ISOiso
execution mode X and Y have to be integers.
[
X]
"A"
behaves within arithmetic expressions as the integer 65.
SICStus Prolog also includes an extra set of functions listed below. These may not be supported by other Prologs. All trigonometric and transcendental functions take float arguments and deliver float values. The trigonometric functions take arguments or deliver values in radians.
abs(
X)
ISOsign(
X)
ISOgcd(
X,
Y)
iso
execution mode X and Y have to be integers.
min(
X,
Y)
max(
X,
Y)
msb(
X)
integer(log(2,X))
.
X must be greater than zero, and
in iso
execution mode, X has to be an integer.
round(
X)
ISO onlyround(
X)
SICStus onlytruncate(
X)
ISO onlytruncate(
X)
SICStus onlyfloor(
X)
ISO onlyfloor(
X)
SICStus onlyceiling(
X)
ISO onlyceiling(
X)
SICStus onlysin(
X)
ISOcos(
X)
ISOtan(
X)
cot(
X)
sinh(
X)
cosh(
X)
tanh(
X)
coth(
X)
asin(
X)
acos(
X)
atan(
X)
ISOatan2(
X,
Y)
acot(
X)
acot2(
X,
Y)
asinh(
X)
acosh(
X)
atanh(
X)
acoth(
X)
sqrt(
X)
ISOlog(
X)
ISOlog(
Base,
X)
exp(
X)
ISO **
Y ISOexp(
X,
Y)
inf
SICStus onlynan
SICStus onlyVariables in an arithmetic expression to be evaluated may be bound to other arithmetic expressions rather than just numbers, e.g.:
evaluate(Expression, Answer) :- Answer is Expression. | ?- evaluate(24*9, Ans). Ans = 216
Arithmetic expressions, as described above, are just data structures.
If you want one evaluated you must pass it as an argument to one
of the built-in predicates listed below. Note that is/2
only evaluates one of its arguments, whereas all the comparison
predicates evaluate both of theirs. In the following, X and
Y stand for arithmetic expressions, and Z for some
term.
is
X ISO =:=
Y ISO =\=
Y ISO <
Y ISO >
Y ISO =<
Y ISO >=
Y ISO