An arithmetic expression is a Prolog term built from numbers, variables, and functors (or operators) that represent arithmetic functions. When an expression is evaluated each variable must be bound to a non-variable expression. An expression evaluates to a number, which may be an integer or a floating point number. The following table details the components of an arithmetic expression, how they are evaluated, the types expected/returned and if they are ISO or an extension:
Expression Signature ISO a variable IF → IF Y an integer number I Y a floating point number F Y pi F Y e F N epsilon F N + E IF → IF Y - E IF → IF Y inc(E) IF → IF N dec(E) IF → IF N E1 + E2 IF, IF → IF Y E1 - E2 IF, IF → IF Y E1 * E2 IF, IF → IF Y E1 / E2 IF, IF → F Y E1 // E2 I, I → I Y E1 rem E2 I, I → I Y E1 div E2 I, I → I Y E1 mod E2 I, I → I Y E1 /\ E2 I, I → I Y E1 \/ E2 I, I → I Y xor(E1,E2) I, I → I Y \ E I → I Y E1 << E2 I, I → I Y E1 >> E2 I, I → I Y lsb(E) I → I N msb(E) I → I N popcount(E) I → I N abs(E) IF → IF Y sign(E) IF → IF Y min(E1,E2) IF, IF → ? Y max(E1,E2) IF, IF → ? Y gcd(E1,E2) I, I → I N E1 ^ E2 IF, IF → IF Y E1 ** E2 IF, IF → F Y sqrt(E) IF → F Y tan(E) IF → F Y atan(E) IF → F Y atan2(Y,X) IF → F Y cos(E) IF → F Y acos(E) IF, IF → F Y sin(E) IF → F Y asin(E) IF → F Y tanh(E) IF → F N atanh(E) IF → F N cosh(E) IF → F N acosh(E) IF, IF → F N sinh(E) IF → F N asinh(E) IF → F N exp(E) IF → F Y log(E) IF → F Y log10(E) IF → F N log(R, E) F, IF → F N float(E) IF → F Y ceiling(E) F → I Y floor(E) F → I Y round(E) F → I Y truncate(E) F → I Y float_fractional_part(E) F → F Y float_integer_part(E) F → F Y 
The meaning of the signature field is as follows:
is, +, -, *, /, //, div, rem, mod, /\, \/, <<, >>, ** and ^ are predefined infix operators. +, - and \, are predefined prefix operators (section 8.14.10).
Integer division rounding function: the integer division rounding function rnd(X) rounds the floating point number X to an integer. There are two possible definitions (depending on the target machine) for this function which differ on negative numbers:
The definition of this function determines the definition of the integer division and remainder ((//)/2 and (rem)/2). It is possible to test the value (toward_zero or down) of the integer_rounding_function Prolog flag to determine which function being used (section 8.22.1). Since rounding toward zero is the most common case, two additional evaluable functors ((div)/2 and (mod)/2) are available which consider rounding toward −∞.
Fast mathematical mode: in order to speed-up integer computations, the GNU Prolog compiler can generate faster code when invoked with the --fast-math option (section 4.4.3). In this mode only integer operations are allowed and a variable in an expression must be bound at evaluation time to an integer. No type checking is done.
Errors
| a sub-expression E is a variable | instantiation_error | |
| a sub-expression E is neither a number nor an evaluable functor | type_error(evaluable, E) | |
| a sub-expression E is a floating point number while an integer is expected | type_error(integer, E) | |
| a sub-expression E is an integer while a floating point number is expected | type_error(float, E) | |
| a division by zero occurs | evaluation_error(zero_divisor) | |
Portability
Refer to the above table to determine which evaluable functors are ISO and which are GNU Prolog extensions. For efficiency reasons, GNU Prolog does not detect the following ISO arithmetic errors: float_overflow, int_overflow, int_underflow, and undefined.
Templates
Description
Result is Expression succeeds if Result can be unified with eval(Expression). Refer to the evaluation of an arithmetic expression for the definition of the eval function (section 8.6.1).
is is a predefined infix operator (section 8.14.10).
Errors
Refer to the evaluation of an arithmetic expression for possible errors (section 8.6.1).
Portability
ISO predicate.
Templates
Description
Expr1 =:= Expr2 succeeds if eval(Expr1) = eval(Expr2).
Expr1 =\= Expr2 succeeds if eval(Expr1) ≠ eval(Expr2).
Expr1 < Expr2 succeeds if eval(Expr1) < eval(Expr2).
Expr1 =< Expr2 succeeds if eval(Expr1) ≤ eval(Expr2).
Expr1 > Expr2 succeeds if eval(Expr1) > eval(Expr2).
Expr1 >= Expr2 succeeds if eval(Expr1) ≥ eval(Expr2).
Refer to the evaluation of an arithmetic expression for the definition of the eval function (section 8.6.1).
=:=, =\=, <, =<, > and >= are predefined infix operators (section 8.14.10).
Errors
Refer to the evaluation of an arithmetic expression for possible errors (section 8.6.1).
Portability
ISO predicates.
Templates
Description
succ(X, Y) is true iff Y is the successor of the non-negative integer X.
Errors
| X and Y are both variables | instantiation_error | |
| X is neither a variable nor an integer | type_error(integer, X) | |
| Y is neither a variable nor an integer | type_error(integer, Y) | |
| X is an integer < 0 | domain_error(not_less_than_zero, X) | |
| Y is an integer < 0 | domain_error(not_less_than_zero, Y) | |
Portability
GNU Prolog predicate.