Author(s): Daniel Cabeza, Manuel Hermenegildo.
Version: 1.11#222 (2004/5/24, 13:8:7 CEST)
Version of last change: 1.11#168 (2004/2/3, 21:28:52 CET)
Arithmetic is performed by built-in predicates which take as arguments arithmetic expressions (see
arithexpression/1
) and evaluate them. Terms representing arithmetic expressions can be created dynamically, but at the time of evaluation, each variable in an arithmetic expression must be bound to a non-variable expression (the term must be ground). For example, given the code in the first line a possible shell interaction follows:
evaluate(Expression, Answer) :- Answer is Expression. ?- _X=24*9, evaluate(_X+6, Ans). Ans = 222 ? yes
arithmetic
)arithmetic
)
Val is Exp
* ISO *
The arithmetic expression Exp
is evaluated and the result is unified with Val
General properties:
basic_props:native/1
)
Val is Exp
is side-effect free
.
(basic_props:sideff/2
)
Usage: X is Exp
Exp
is an arithmetic expression.
(arithmetic:arithexpression/1
)
X
is a number.
(basic_props:num/1
)
basic_props:eval(X is Exp)
(basic_props:eval/1
)
Exp1 < Exp2
* ISO *
The numeric value of Exp1
is less than the numeric value of Exp2
when both are evaluated as arithmetic expressions.
General properties:
basic_props:native/1
)
Exp1<Exp2
is side-effect free
.
(basic_props:sideff/2
)
Usage:
Exp1
is an arithmetic expression.
(arithmetic:arithexpression/1
)
Exp2
is an arithmetic expression.
(arithmetic:arithexpression/1
)
basic_props:eval(Exp1<Exp2)
(basic_props:eval/1
)
Exp1 =< Exp2
* ISO *
The numeric value of Exp1
is less than or equal to the numeric value of Exp2
when both are evaluated as arithmetic expressions.
General properties:
basic_props:native/1
)
Exp1=<Exp2
is side-effect free
.
(basic_props:sideff/2
)
Usage:
Exp1
is an arithmetic expression.
(arithmetic:arithexpression/1
)
Exp2
is an arithmetic expression.
(arithmetic:arithexpression/1
)
basic_props:eval(Exp1=<Exp2)
(basic_props:eval/1
)
Exp1 > Exp2
* ISO *
The numeric value of Exp1
is greater than the numeric value of Exp2
when both are evaluated as arithmetic expressions.
General properties:
basic_props:native/1
)
Exp1>Exp2
is side-effect free
.
(basic_props:sideff/2
)
Usage:
Exp1
is an arithmetic expression.
(arithmetic:arithexpression/1
)
Exp2
is an arithmetic expression.
(arithmetic:arithexpression/1
)
basic_props:eval(Exp1>Exp2)
(basic_props:eval/1
)
Exp1 >= Exp2
* ISO *
The numeric value of Exp1
is greater than or equal to the numeric value of Exp2
when both are evaluated as arithmetic expressions.
General properties:
basic_props:native/1
)
Exp1>=Exp2
is side-effect free
.
(basic_props:sideff/2
)
Usage:
Exp1
is an arithmetic expression.
(arithmetic:arithexpression/1
)
Exp2
is an arithmetic expression.
(arithmetic:arithexpression/1
)
basic_props:eval(Exp1>=Exp2)
(basic_props:eval/1
)
Exp1 =:= Exp2
* ISO *
The numeric values of Exp1
and Exp2
are equal when both are evaluated as arithmetic expressions.
General properties:
basic_props:native/1
)
Exp1=:=Exp2
is side-effect free
.
(basic_props:sideff/2
)
Usage:
Exp1
is an arithmetic expression.
(arithmetic:arithexpression/1
)
Exp2
is an arithmetic expression.
(arithmetic:arithexpression/1
)
basic_props:eval(Exp1=:=Exp2)
(basic_props:eval/1
)
Exp1 =\= Exp2
* ISO *
The numeric values of Exp1
and Exp2
are not equal when both are evaluated as arithmetic expressions.
General properties:
basic_props:native/1
)
Exp1=\=Exp2
is side-effect free
.
(basic_props:sideff/2
)
Usage:
Exp1
is an arithmetic expression.
(arithmetic:arithexpression/1
)
Exp2
is an arithmetic expression.
(arithmetic:arithexpression/1
)
basic_props:eval(Exp1=\=Exp2)
(basic_props:eval/1
)
An arithmetic expression is a term built from numbers and
evaluable functors that represent arithmetic functions. An arithmetic expression evaluates to a number, which may be an integer (
int/1
) or a float (
flt/1
). The evaluable functors allowed in an arithmetic expression are listed below, together with an indication of the functions they represent. All evaluable functors defined in
ISO-Prolog are implemented, as well as some other useful or traditional. Unless stated otherwise, an expression evaluates to a float if any of its arguments is a float, otherwise to an integer.
- /1
: sign reversal. * ISO *
+ /1
: identity.
-- /1
: decrement by one.
++ /1
: increment by one.
+ /2
: addition. * ISO *
- /2
: subtraction. * ISO *
* /2
: multiplication. * ISO *
// /2
: integer division. Float arguments are truncated to integers, result always integer. * ISO *
/ /2
: division. Result always float. * ISO *
rem/2
: integer remainder. The result is always an integer, its sign is the sign of the first argument. * ISO *
mod/2
: modulo. The result is always a positive integer. * ISO *
abs/1
: absolute value. * ISO *
sign/1
: sign of. * ISO *
float_integer_part/1
: float integer part. Result always float. * ISO *
float_fractional_part/1
: float fractional part. Result always float. * ISO *
truncate/1
: The result is the integer equal to the integer part of the argument. * ISO *
integer/1
: same as
truncate/1
.
float/1
: conversion to float. * ISO *
floor/1
: largest integer not greater than. * ISO *
round/1
: integer nearest to. * ISO *
ceiling/1
: smallest integer not smaller than. * ISO *
** /2
: exponentiation. Result always float. * ISO *
>> /2
: integer bitwise right shift. * ISO *
<< /2
: integer bitwise left shift. * ISO *
/\ /2
: integer bitwise and. * ISO *
\/ /2
: integer bitwise or. * ISO *
\ /1
: integer bitwise complement. * ISO *
# /2
: integer bitwise exclusive or (xor).
exp/1
: exponential (e to the power of). Result always float. * ISO *
log/1
: natural logarithm (base e). Result always float. * ISO *
sqrt/1
: square root. Result always float. * ISO *
sin/1
: sine. Result always float. * ISO *
cos/1
: cosine. Result always float. * ISO *
atan/1
: arc tangent. Result always float. * ISO *
gcd/2
: Greatest common divisor. Arguments must evaluate to integers, result always integer.
In addition to these functors, a list of just a number evaluates to this number. Since a
quoted string is just a list of integers, this allows a quoted character to be used in place of its
ASCII code; e.g. "A"
behaves within arithmetic expressions as the integer 65. Note that this is not
ISO-compliant, and that can be achieved by using the ISO notation 0'A
.
Arithmetic expressions, as described above, are just data structures. If you want one evaluated you must pass it as an argument to one of the arithmetic predicates defined in this library.
General properties:
arithexpression(Arg1)
is side-effect free
.
(basic_props:sideff/2
)
Usage: arithexpression(E)
E
is an arithmetic expression.
Go to the first, previous, next, last section, table of contents.