[top] [up] [next]

Arithmetic


It is possible to perform arithmetic computations in Prolog. All computations are done with 32 bit integers and 64 bit floating point numbers. Prolog is a language that processes symbols, to perform arithmetic the standard operator is/2 must be used.

Example
X is 1+2.. The result is X = 3.
X is ceiling( 2.1 ). The result is X = 3.
X is ceiling( -2.1). The result is X = -2.

What follows is an overview of all the available arithmetic predicates and operators, if the predicate/operator is defined in the ISO Prolog standard then (ISO) is placed behind the explanation.

Name/arity Explanation
abs / 1 absolute value (ISO)
+ / 2 addition (ISO)
acos / 1 arc cosines
asin / 1 arc sinus
atan / 1 arc tangent (ISO)
/\ / 2 bit and (ISO)
\ / 2 bit complement (ISO)
<< / 2  bit shift left (ISO)
\/ /2 bit or (ISO)
>> / 2 bit shift right (ISO)
ceiling / 1 smallest integer not smaller than (ISO)
cos / 1 cosine (ISO)
cosh / 1 hyperbolic cosines
e / 0 value of e
exp / 1 natural algorithm (ISO)
** / 2 exponentiation (ISO)
float / 1 conversion to float (ISO)
float_fractional_part / 1 float fractional part (ISO)
float_integer_part / 1 float integer part (ISO)
/ / 2 floating-point division (ISO)
index / 3 locate substring in string
// / 2 integer division (ISO)
irand / 1 generate random value
floor / 1 largest integer not greater than (ISO)
length / 1 length of string
log / 1 natural logarithm (ISO)
log10 / 1 base 10 logarithm
mod / 2 modulo (ISO)
* / 2 multiplication (ISO)
pi / 0 value of pi
rem / 2 integer remainder (ISO)
round / 1 integer nearest to (ISO)
sign / 1 sign of (ISO)
- / 1 sign reversal (ISO)
sin / 1 sinus (ISO)
sinh / 1 hyperbolic sinus
sqrt / 1 square root (ISO)
- / 2 subtraction (ISO)
tan / 1 tangent
tanh / 1 hyperbolic tangent
truncate / 1 integer equal to the integer part of (ISO)
value / 1 evaluates an arithmetic expression enclosed in quotes

More arithmetic operators which force Prolog to evaluate an expression as an arithmetic expression, like the is operator are:  

Operator / arity Explanation
< / 2 smaller than (ISO)
> / 2 greater than (ISO)
=< / 2 smaller than or equal to (ISO)
>= / 2 greater than or equal to (ISO)
=\= / 2 not equal to (ISO)
=:= / 2 equal to (ISO)

A string can also be used in arithmetic expressions, the value of a string is the ASCII value of the first character of the string. For instance the arithmetic value of "first" is 102.
Just like a string a list can also be used in arithmetic expressions, the first element of the list is considered the arithmetic value of the list, for instance the arithmetic value of the list [23,5] is 23.

[top] [up] [next]

 

info@trinc-prolog.com