Previous Up Next

7.6  Arithmetic

7.6.1  Evaluation of an arithmetic expression

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
Result = eval(Expression)
SignatureISO
Variable
must be bound to a non-variable expression E.
The result is eval(E)
IF → IFY
integer number
this number
I → IY
floating point number
this number
F → FY
+ E
eval(E)
IF → IFN
- E
- eval(E)
IF → IFY
inc(E)
eval(E) + 1
IF → IFN
dec(E)
eval(E) - 1
IF → IFN
E1 + E2
eval(E1) + eval(E2)
IF, IF → IFY
E1 - E2
eval(E1) - eval(E2)
IF, IF → IFY
E1 * E2
eval(E1) * eval(E2)
IF, IF → IFY
E1 / E2
eval(E1) / eval(E2)
IF, IF → FY
E1 // E2
rnd(eval(E1) / eval(E2))
I, I → IY
E1 rem E2
eval(E1) - (rnd(eval(E1) / eval(E2))*eval(E2))
I, I → IY
E1 mod E2
eval(E1) - ( ⌊eval(E1) / eval(E2)⌋ *eval(E2))
I, I → IY
E1 /\ E2
eval(E1) bitwise_and eval(E2)
I, I → IY
E1 \/ E2
eval(E1) bitwise_or eval(E2)
I, I → IY
E1 ^ E2
eval(E1) bitwise_xor eval(E2)
I, I → IN
\ E
bitwise_not eval(E)
I → IY
E1 << E2
eval(E1) integer_shift_left eval(E2)
I, I → IY
E1 >> E2
eval(E1) integer_shift_right eval(E2)
I, I → IY
abs(E)
absolute value of eval(E)
IF → IFY
sign(E)
sign of eval(E) (-1 if < 0, 0 if = 0, +1 if > 0)
IF → IFY
min(E1,E2)
minimal value between eval(E1) and eval(E2)
IF, IF → ?N
max(E1,E2)
maximal value between eval(E1) and eval(E2)
IF, IF → ?N
E1 ** E2
eval(E1) raised to the power of eval(E2)
IF, IF → FY
sqrt(E)
square root of eval(E)
IF → FY
atan(E)
arc tangent of eval(E)
IF → FY
cos(E)
cosine of eval(E)
IF → FY
acos(E)
arc cosine of eval(E)
IF → FN
sin(E)
sine of eval(E)
IF → FY
asin(E)
arc sine of eval(E)
IF → FN
exp(E)
e raised to the power of eval(E)
IF → FY
log(E)
natural logarithms of eval(E)
IF → FY
float(E)
the floating point number equal to eval(E)
IF → FY
ceiling(E)
rounds eval(E) upward to the nearest integer
F → IY
floor(E)
rounds eval(E) downward to the nearest integer
F → IY
round(E)
rounds eval(E) to the nearest integer
F → IY
truncate(E)
the integer value of eval(E)
F → IY
float_fractional_part(E)
the float equal to the fractional part of eval(E)
F → FY
float_integer_part(E)
the float equal to the integer part of eval(E)
F → FY

The meaning of the signature field is as follows:

is, +, -, *, //, /, rem, and mod are predefined infix operators. + and - are predefined prefix operators (section 7.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 precise definition of the integer division (//)/2 and of the integer remainder (rem)/2. Rounding toward zero is the most common case. In any case 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 7.22.1).

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 3.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.

7.6.2  (is)/2 - evaluate expression

Templates

is(?term, +evaluable)

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 7.6.1).

is is a predefined infix operator (section 7.14.10).

Errors

Refer to the evaluation of an arithmetic expression for possible errors (section 7.6.1).

Portability

ISO predicate.

7.6.3  (=:=)/2 - arithmetic equal, (=\=)/2 - arithmetic not equal,
(<)/2 - arithmetic less than, (=<)/2 - arithmetic less than or equal to,
(>)/2 - arithmetic greater than, (>=)/2 - arithmetic greater than or equal to

Templates

=:=(+evaluable, +evaluable)
=\=(+evaluable, +evaluable)
<(+evaluable, +evaluable)
=<(+evaluable, +evaluable)
>(+evaluable, +evaluable)
>=(+evaluable, +evaluable)

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 7.6.1).

=:=, =\=, <, =<, > and >= are predefined infix operators (section 7.14.10).

Errors

Refer to the evaluation of an arithmetic expression for possible errors (section 7.6.1).

Portability

ISO predicates.


Copyright (C) 1999-2007 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright
Previous Up Next