Previous topic  Top  Next topic  Print this Topic
 

Numbers, Comparisons and Arithmetic

 

Objects denoting numbers or strings are different from other objects because the usual comparison operators are defined for them, as well as several arithmetic functions. Within a query or a rule body, relations between numbers or strings may be tested with the comparison expressions. For example, the following query asks for all car owners younger than 22:

?- ?X:Car[owner->?P] AND ?P[age->?A] AND ?A < 22.

The arithmetic operations addition +, subtraction -, multiplication * and division / are also implemented. Arithmetic expressions may be constructed in the usual way. Even complex expressions, e.g. 3 + 5 + 2 or 3 + 2 * 3 are supported. By default, multiplication and division have a higher precedence than addition and subtraction. As usual, the evaluation order may be changed using parentheses, e.g. (3 + 2) * 3. The following example contains the query that computes the average age of Peter and Paul.

?-

peter[age->?P1] AND

paul[age->?P2] AND

(?A is (?P1+?P2)/2.0) .

Additionally the following mathematical functions are implemented:

sin,cos,tan,asin,acos,ceil,floor,exp,rint,sqrt,round,max,min,pow