8 Others Elements of Prolog
For the arithmetic operators, prolog has already a list of predefined predicates. These are :
In Prolog, the calculations don't write like we have learned.They are written as a binary tree.That is to say that :
y*5+10*x are written in Prolog : +(*(y,5),*(10,x)).
Because we don't have learned to write like this, Prolog accept our way of writing calculations.Nevertheless, we have to say to him the rules of priority on the operators.For instance, we have to say to Prolog that * is prior on +.We have also to indicate to Prolog the prior calculations with '()'.Prolog allows the programmer to define his own operators with the relations next :
Where P is the priority of the operators(between 1 and 1200), xfy indicates if the operator is infix(xfx,xfy,yfx) or postfix(fx,fy).The name is, of course, the name of the operator.Note that the prior operator has the lowest priority.
Prolog has already these predefined operators :
Prolog use the infix operator 'is' to give the result of an arithmetic operation to a variable.is
X is 3 + 4.
X = 7
When the goal operator 'is' is used the variables on the right must have been unified to numbers before.
Prolog is not oriented calculations, so, after a certain point, he approximates
the calculations and he don't answer the exact number but his approximation
Fo example :
?- X is 1000 + .0001
X = 1000