Home Contents Index Summary Previous Next

3.23 Arithmetic Functions

Arithmetic functions are terms which are evaluated by the arithmetic predicates described above. SWI-Prolog tries to hide the difference between integer arithmetic and floating point arithmetic from the Prolog user. Arithmetic is done as integer arithmetic as long as possible and converted to floating point arithmetic whenever one of the arguments or the combination of them requires it. If a function returns a floating point value which is whole it is automatically transformed into an integer. There are three types of arguments to functions:

Expr Arbitrary expression, returning either a floating point value or an integer.
IntExpr Arbitrary expression that should evaluate into an integer.
Int An integer.

In case integer addition, subtraction and multiplication would lead to an integer overflow the operands are automatically converted to floating point numbers. The floating point functions (sin/1, exp/1, etc.) form a direct interface to the corresponding C library functions used to compile SWI-Prolog. Please refer to the C library documentation for details on precision, error handling, etc.

- +Expr
Result = -Expr

+Expr1 + +Expr2
Result = Expr1 + Expr2

+Expr1 - +Expr2
Result = Expr1 - Expr2

+Expr1 * +Expr2
Result = Expr1 × Expr2

+Expr1 / +Expr2
Result = Expr1/Expr2

+IntExpr1 mod +IntExpr2
Result = Expr1 mod Expr2 (remainder of division).

+IntExpr1 rem +IntExpr2
Result = Expr1 rem Expr2 (remainder of division).

+IntExpr1 // +IntExpr2
Result = Expr1 divExpr2 (integer division).

abs(+Expr)
Evaluate Expr and return the absolute value of it.

sign(+Expr)
Evaluate to -1 if Expr < 0, 1 if Expr > 0 and 0 if Expr = 0.

max(+Expr1, +Expr2)
Evaluates to the largest of both Expr1 and Expr2.

min(+Expr1, +Expr2)
Evaluates to the smallest of both Expr1 and Expr2.

.(+Int,)
A list of one element evaluates to the element. This implies "a" evaluates to the ASCII value of the letter `a' (97). This option is available for compatibility only. It will not work if `style_check(+string)' is active as "a" will then be transformed into a string object. The recommended way to specify the ASCII value of the letter `a' is 0'a.

random(+Int)
Evaluates to a random integer i for which 0 =< i < Int. The seed of this random generator is determined by the system clock when SWI-Prolog was started.

round(+Expr)
Evaluates Expr and rounds the result to the nearest integer.

integer(+Expr)
Same as round/1 (backward compatibility).

float(+Expr)
Translate the result to a floating point number. Normally, Prolog will use integers whenever possible. When used around the 2nd argument of is/2, the result will be returned as a floating point number. In other contexts, the operation has no effect.

float_fractional_part(+Expr)
Fractional part of a floating-point number. Negative if Expr is negative, 0 if Expr is integer.

float_integer_part(+Expr)
Integer part of floating-point number. Negative if Expr is negative, Expr if Expr is integer.

truncate(+Expr)
Truncate Expr to an integer. Same as float_integer_part/1.

floor(+Expr)
Evaluates Expr and returns the largest integer smaller or equal to the result of the evaluation.

ceiling(+Expr)
Evaluates Expr and returns the smallest integer larger or equal to the result of the evaluation.

ceil(+Expr)
Same as ceiling/1 (backward compatibility).

+IntExpr >> +IntExpr
Bitwise shift IntExpr1 by IntExpr2 bits to the right.

+IntExpr << +IntExpr
Bitwise shift IntExpr1 by IntExpr2 bits to the left.

+IntExpr \/ +IntExpr
Bitwise `or' IntExpr1 and IntExpr2.

+IntExpr /\ +IntExpr
Bitwise `and' IntExpr1 and IntExpr2.

+IntExpr xor +IntExpr
Bitwise `exclusive or' IntExpr1 and IntExpr2.

\ +IntExpr
Bitwise negation.

sqrt(+Expr)
Result = sqrt(Expr)

sin(+Expr)
Result = sin(Expr). Expr is the angle in radians.

cos(+Expr)
Result = cos(Expr). Expr is the angle in radians.

tan(+Expr)
Result = tan(Expr). Expr is the angle in radians.

asin(+Expr)
Result = arcsin(Expr). Result is the angle in radians.

acos(+Expr)
Result = arccos(Expr). Result is the angle in radians.

atan(+Expr)
Result = arctan(Expr). Result is the angle in radians.

atan(+YExpr, +XExpr)
Result = arctan(YExpr/XExpr). Result is the angle in radians. The return value is in the range [- pi ... pi ]. Used to convert between rectangular and polar coordinate system.

log(+Expr)
Result = ln(Expr)

log10(+Expr)
Result = log10(Expr)

exp(+Expr)
Result = e **Expr

+Expr1 ** +Expr2
Result = Expr1**Expr2

+Expr1 ^ +Expr2
Same as **/2. (backward compatibility).

pi
Evaluates to the mathematical constant pi (3.141593).

e
Evaluates to the mathematical constant e (2.718282).

cputime
Evaluates to a floating point number expressing the CPU time (in seconds) used by Prolog up till now. See also statistics/2 and time/1.