- Predefined Arithmetic Functions
- Evaluation Mechanism
- User Defined Arithmetic Functions
- Runtime Expressions

Function | Description | Argument Types | Result Type | |

+ E | unary plus | number | number | |

– E | unary minus | number | number | |

abs(E) | absolute value | number | number | |

sgn(E) | sign value | number | integer | |

floor(E) | round down to integral value | number | number | |

ceiling(E) | round up to integral value | number | number | |

round(E) | round to nearest integral value | number | number | |

truncate(E) | truncate to integral value | number | number | |

E1 + E2 | addition | number × number | number | |

E1 – E2 | subtraction | number × number | number | |

E1 * E2 | multiplication | number × number | number | |

E1 / E2 | division | number × number | see below | |

E1 // E2 | integer division (truncate) | integer × integer | integer | |

E1 rem E2 | integer remainder | integer × integer | integer | |

E1 div E2 | integer division (floor) | integer × integer | integer | |

E1 mod E2 | integer modulus | integer × integer | integer | |

gcd(E1,E2) | greatest common divisor | integer × integer | integer | |

lcm(E1,E2) | least common multiple | integer × integer | integer | |

E1 ^ E2 | power operation | number × number | number | |

min(E1,E2) | minimum of 2 values | number × number | number | |

max(E1,E2) | maximum of 2 values | number × number | number | |

`\` E |
bitwise complement | integer | integer | |

E1 `/\` E2 |
bitwise conjunction | integer × integer | integer | |

E1 `\/` E2 |
bitwise disjunction | integer × integer | integer | |

xor(E1,E2) | bitwise exclusive disjunction | integer × integer | integer | |

E1 >> E2 | shift E1 right by E2 bits | integer × integer | integer | |

E1 << E2 | shift E1 left by E2 bits | integer × integer | integer | |

sin(E) | trigonometric function | number | real | |

cos(E) | trigonometric function | number | real | |

tan(E) | trigonometric function | number | real | |

asin(E) | trigonometric function | number | real | |

acos(E) | trigonometric function | number | real | |

atan(E) | trigonometric function | number | real | |

atan(E1,E1) | trigonometric function | number × number | real | |

exp(E) | exponential function e^{x} |
number | real | |

ln(E) | natural logarithm | number | real | |

sqrt(E) | square root | number | real | |

pi | the constant pi = 3.1415926... | — | float | |

e | the constant e = 2.7182818... | — | float | |

fix(E) | convert to integer (truncate) | number | integer | |

integer(E) | convert to integer (exact) | number | integer | |

float(E) | convert to float | number | float | |

breal(E) | convert to bounded real | number | breal | |

rational(E) | convert to rational | number | rational | |

rationalize(E) | convert to rational | number | rational | |

numerator(E) | extract numerator of a rational | integer or rational | integer | |

denominator(E) | extract denominator of a rational | integer or rational | integer | |

sum(L) | sum of list elements | list | number | |

min(L) | minimum of list elements | list | number | |

max(L) | maximum of list elements | list | number | |

eval(E) | evaluate runtime expression | term | number |

Argument types other than specified yield a type error. As an argument type,

The integer division

This table gives an overview:X =:= (X rem Y) + (X // Y) * Y X =:= (X mod Y) + (X div Y) * Y

10 x 3 -10 x 3 10 x -3 -10 x -3 // 3 -3 -3 3 rem 1 -1 1 -1 div 3 -4 -4 3 mod 1 2 -2 -1

Compound terms are evaluated by first evaluating their arguments and then calling the corresponding evaluation predicate. The evaluation predicate associated with a compound term3 * 1.5 + Y / sqrt(pi)

where T*(3,1.5,T1), sqrt(3.14159,T2), /(Y,T2,T3), +(T1,T3,T4)

Although this evaluation mechanism is usually transparent to the user, it becomes visible when errors occur, when subgoals are delayed, or when inline-expanded code is traced.

Note that this mechanism is not only useful for user-defined predicates, but can also be used to call ECL[eclipse 1]: [user]. :- op(200, yf, !). % let's have some syntaxtic sugar !(N, F) :- fac(N, 1, F). fac(0, F0, F) :- !, F=F0. fac(N, F0, F) :- N1 is N-1, F1 is F0*N, fac(N1, F1, F). user compiled traceable 504 bytes in 0.00 seconds yes. [eclipse 2]: X is 23!. % calls !/2 X = 25852016738884976640000 yes.

which callT is cputime - T0. L is string_length("abcde") - 1.

However there is a difference compared to the evaluation of the predefined arithmetic functions (as listed in the table above): The arguments of the user-defined arithmetic expression are

To make it work even when the argument gets bound to a symbolic expression at runtime, use eval/1 as in the following example:p(Number) :- Res is 1 + Number, ...

If the expression is the only argument of is/2, the eval/1 may be omitted.p(Expr) :- Res is 1 + eval(Expr), ...