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 (sin1, exp1, 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.
% operator. It's
behaviour with negtive values is illustrated in the table below.
| 2 | = | 17 | mod | 5 |
| 2 | = | 17 | mod | -5 |
| -2 | = | -17 | mod | 5 |
| -2 | = | -17 | mod | 5 |
rem+IntExpr1+IntExpr2
Remainder of division:
Result = float_fractional_part(IntExpr1/IntExpr2)
+IntExpr1+IntExpr2
Integer division:
Result = truncate(Expr1/Expr2)
abs1+Expr
Evaluate Expr and return the absolute value of it.
sign1+Expr
Evaluate to -1 if
, 1 if
and 0 if
.
max2+Expr1, +Expr2
Evaluates to the largest of both Expr1 and Expr2.
min2+Expr1, +Expr2
Evaluates to the smallest of both Expr1 and Expr2.
2+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.
random1+Int
Evaluates to a random integer i for which
.
The seed of this random generator is determined by the system clock when
SWI-Prolog was started.
round1+Expr
Evaluates Expr and rounds the result to the nearest integer.
integer1+Expr
Same as round1 (backward compatibility).
float1+Expr
Translate the result to a floating point number. Normally, Prolog will
use integers whenever possible. When used around the 2nd argument of
is2, the result will be returned as a floating point number. In other
contexts, the operation has no effect.
float_fractional_part1+Expr
Fractional part of a floating-point number. Negative if Expr is
negative, 0 if Expr is integer.
float_integer_part1+Expr
Integer part of floating-point number. Negative if Expr is
negative, Expr if Expr is integer.
truncate1+Expr
Truncate Expr to an integer. Same as float_integer_part1.
floor1+Expr
Evaluates Expr and returns the largest integer smaller or equal
to the result of the evaluation.
ceiling1+Expr
Evaluates Expr and returns the smallest integer larger or equal
to the result of the evaluation.
ceil1+Expr
Same as ceiling1 (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.
xor+IntExpr+IntExpr
Bitwise `exclusive or' IntExpr1 and IntExpr2.
+IntExpr
Bitwise negation.
sqrt1+Expr
sin1+Expr
. Expr is the angle in radians.
cos1+Expr
. Expr is the angle in radians.
tan1+Expr
. Expr is the angle in radians.
asin1+Expr
. Result is the angle in radians.
acos1+Expr
. Result is the angle in radians.
atan1+Expr
. Result is the angle in radians.
atan2+YExpr, +XExpr
. Result is the
angle in radians. The return value is in the range
.
Used to convert between rectangular and polar coordinate system.
log1+Expr
log101+Expr
exp1+Expr
+Expr1+Expr2
+Expr1+Expr2
Same as **/2. (backward compatibility).
pi0
Evaluates to the mathematical constant
(3.141593).
e0
Evaluates to the mathematical constant
(2.718282).
cputime0
Evaluates to a floating point number expressing the CPU time (in seconds)
used by Prolog up till now. See also statistics2 and time1.