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, Prolog raises exceptions of the appropriate type (see ref-ere-hex). 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. 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. All trigonometric and
transcendental functions take float arguments and deliver float
values. The trigonometric functions take arguments or deliver
values in radians.
The arithmetic functors are annotated with [ISO], with the same meaning as for the built-in predicates; see ISO Compliance.
+(
X)
-
X+
Y-
Y*
Y/
Y//
Y ISO rem
Y ISOinteger(
X)-integer(
Y)*(
X//
Y)
. The sign of
a nonzero remainder will thus be the same as that of the dividend.
X and Y have to be integers.
mod
Y ISOinteger(
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.
integer(
X)
float_integer_part(
X)
ISOfloat(integer(
X))
.
X has to be a float.
float_fractional_part(
X)
ISO -
float_integer_part(
X)
.
X has to be a float.
float(
X)
ISO/\
Y ISO\/
Y ISO\
Y\(
X)
ISO<<
Y ISO>>
Y ISO[
X]
"A"
behaves within arithmetic expressions as the integer 65.
abs(
X)
ISOsign(
X)
ISOgcd(
X,
Y)
min(
X,
Y)
max(
X,
Y)
msb(
X)
integer(log(2,X))
.
X must be greater than zero, and
X has to be an integer.
round(
X)
ISOtruncate(
X)
ISOfloor(
X)
ISOceiling(
X)
ISOsin(
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)
The following operation is included in order to allow integer arithmetic on character codes.
[
X]
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