Previous Up Next

9.3  Arithmetic Functions

9.3.1  Predefined Arithmetic Functions

The following predefined arithmetic functions are available. E, E1 and E2 stand for arbitrary arithmetic expressions.

FunctionDescriptionArgument TypesResult Type
+ Eunary plusnumbernumber
– Eunary minusnumbernumber
abs(E)absolute valuenumbernumber
sgn(E)sign valuenumberinteger
floor(E)round down to integral valuenumbernumber
ceiling(E)round up to integral valuenumbernumber
round(E)round to nearest integral valuenumbernumber
truncate(E)truncate to integral valuenumbernumber
E1 + E2additionnumber × numbernumber
E1 – E2subtractionnumber × numbernumber
E1 * E2multiplicationnumber × numbernumber
E1 / E2divisionnumber × numbersee below
E1 // E2integer division (truncate)integer × integerinteger
E1 rem E2integer remainderinteger × integerinteger
E1 div E2integer division (floor)integer × integerinteger
E1 mod E2integer modulusinteger × integerinteger
gcd(E1,E2)greatest common divisorinteger × integerinteger
lcm(E1,E2)least common multipleinteger × integerinteger
E1 ^ E2power operationnumber × numbernumber
min(E1,E2)minimum of 2 valuesnumber × numbernumber
max(E1,E2)maximum of 2 valuesnumber × numbernumber
copysign(E1,E2)combine value and signnumber × numbernumber
nexttoward(E1,E2)next representable numbernumber × numbernumber
\ Ebitwise complementintegerinteger
E1 /\ E2bitwise conjunctioninteger × integerinteger
E1 \/ E2bitwise disjunctioninteger × integerinteger
xor(E1,E2)bitwise exclusive disjunctioninteger × integerinteger
E1 >> E2shift E1 right by E2 bitsinteger × integerinteger
E1 << E2shift E1 left by E2 bitsinteger × integerinteger
sin(E)trigonometric functionnumberreal
cos(E)trigonometric functionnumberreal
tan(E)trigonometric functionnumberreal
asin(E)trigonometric functionnumberreal
acos(E)trigonometric functionnumberreal
atan(E)trigonometric functionnumberreal
atan(E1,E1)trigonometric functionnumber × numberreal
exp(E)exponential function ex numberreal
ln(E)natural logarithmnumberreal
sqrt(E)square rootnumberreal
pithe constant pi = 3.1415926...float
ethe constant e = 2.7182818...float
fix(E)convert to integer (truncate)numberinteger
integer(E)convert to integer (exact)numberinteger
float(E)convert to floatnumberfloat
breal(E)convert to bounded realnumberbreal
rational(E)convert to rationalnumberrational
rationalize(E)convert to rationalnumberrational
numerator(E)extract numerator of a rationalinteger or rational integer
denominator(E)extract denominator of a rationalinteger or rational integer
sum(Es)sum of elementsvectornumber
sum(Es*Es)scalar productvector*vectornumber
min(Es)minimum of list elementsvectornumber
max(Es)maximum of list elementsvectornumber
eval(E)evaluate runtime expressiontermnumber

Argument types other than specified yield a type error. As an argument type, number stands for integer, rational, float or breal with the type conversions as specified above. As a result type, number stands for the more general of the argument types, and real stands for float or breal. The division operator / yields either a rational or a float result, depending on the value of the global flag prefer_rationals. The same is true for the result of ^ if an integer is raised to a negative integral power.

The integer division // rounds the result towards zero (truncates), while the div division rounds towards negative infinity (floor). Each division function is paired with a corresponding remainder function: (rem computes the remainder corresponding to //, and mod computes the remainder corresponding to div 3. The remainder results differ only in the case where the two arguments have opposite signs. The relationship between them is as follows:

X =:= (X rem Y) + (X // Y) * Y
X =:= (X mod Y) + (X div Y) * Y

This table gives an overview:

      10 x 3   -10 x 3   10 x -3   -10 x -3

//       3        -3       -3          3
rem      1        -1        1         -1
div      3        -4       -4          3
mod      1         2       -2         -1

9.3.2  Evaluation Mechanism

An arithmetic expression is a Prolog term that is made up of variables, numbers, atoms and compound terms, e.g.,

3 * 1.5 + Y / sqrt(pi)

Compound terms are evaluated by first evaluating their arguments and then calling the corresponding evaluation predicate. The evaluation predicate associated with a compound term func(a_1,..,a_n) is the predicate func/(n+1). It receives a_1,..,a_n as its first n arguments and returns a numeric result as its last argument. This result is then used in the arithmetic computation. For instance, the expression above would be evaluated by the goal sequence

*(3,1.5,T1), sqrt(3.14159,T2), /(Y,T2,T3), +(T1,T3,T4)

where T1, T2 etc. are auxiliary variables created by the system to hold intermediate results.

Although this evaluation mechanism is usually transparent to the user, it becomes visible when errors occur, when subgoals are delayed, or when inline-expanded code is traced.

9.3.3  User Defined Arithmetic Functions

This evaluation mechanism outlined above is not restricted to the predefined arithmetic functions shown in the table. In fact it works for all atoms and compound terms. It is therefore possible to define a new arithmetic operation by just defining an evaluating predicate:

[eclipse 1]: [user].
 :- op(200, yf, !).             % let's have some syntaxtic sugar
 !(N, F) :- fac(N, 1, F).
 fac(0, F0, F) :- !, F=F0.
 fac(N, F0, F) :- N1 is N-1, F1 is F0*N, fac(N1, F1, F).
 user       compiled traceable 504 bytes in 0.00 seconds

[eclipse 2]: X is 23!.       % calls !/2

X = 25852016738884976640000

Note that this mechanism is not only useful for user-defined predicates, but can also be used to call ECLiPSe built-ins inside arithmetic expressions, e.g.,

T is cputime - T0.
L is string_length("abcde") - 1.

which call cputime/1 and string_length/2 respectively. Any predicate that returns a number as its last argument can be used in a similar manner.

However there is a difference compared to the evaluation of the predefined arithmetic functions (as listed in the table above): The arguments of the user-defined arithmetic expression are not evaluated but passed unchanged to the evaluating predicate. For example, the expression twice(3+4) is transformed into the goal twice(3+4, Result) rather than twice(7, Result). This makes sense because otherwise it would not be possible to pass any compound term to the predicate. If evaluation is wanted, the user-defined predicate can explicitly call is/2 or use eval/1.

9.3.4  Runtime Expressions

In order to enable efficient compilation of arithmetic expressions, ECLiPSe requires that variables in compiled arithmetic expressions must be bound to numbers at runtime, not symbolic expressions. For example, in the following code p/1 will only work when called with a numerical argument, else it will raise error 24:

p(Number) :- Res is 1 + Number, ...

To make it work even when the argument gets bound to a symbolic expression at runtime, use eval/1 as in the following example:

p(Expr) :- Res is 1 + eval(Expr), ...

If the expression is the only argument of is/2, the eval/1 may be omitted.

Caution: In ECLiPSe versions up to 5.8, mod was the remainder corresponding to //, i.e., behaved like rem

Previous Up Next