Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Mathematical Predicates



Following are the mathematical predicates −

Predicates Description
random(L,H,X). Get random value between L and H
between(L,H,X). Get all values between L and H
succ(X,Y). Add 1 and assign it to X
abs(X). Get absolute value of X
max(X,Y). Get largest value between X and Y
min(X,Y). Get smallest value between X and Y
round(X). Round a value near to X
truncate(X). Convert float to integer, delete the fractional part
loor(X). Round down
ceiling(X). Round up
sqrt(X). Square root

Now let us see some of these functions in action using Prolog programs.

Example

| ?- random(0,10,X).

X = 0

yes
| ?- random(0,10,X).

X = 5

yes
| ?- random(0,10,X).

X = 1

yes
| ?- between(0,10,X).

X = 0 ? a

X = 1

X = 2

X = 3

X = 4

X = 5

X = 6

X = 7

X = 8

X = 9

X = 10

(31 ms) yes
| ?- 

Example

| ?- succ(2,X).

X = 3

yes
| ?- 

| ?- X is abs(-8).

X = 8

yes
| ?- X is max(10,5).

X = 10

yes
| ?- X is min(10,5).

X = 5

yes
| ?- X is round(10.56).

X = 11

yes
| ?- 

Example

| ?- X is truncate(10.56).

X = 10

yes
| ?- X is floor(10.56).

X = 10

yes
| ?- X is ceiling(10.56).

X = 11

yes
| ?- X is sqrt(144).

X = 12.0

yes
| ?- 
Advertisements