next up previous contents index
Next: Arithmetic Functions Up: Built-in predicates Previous: Character Conversion   Contents   Index


Arithmetic

Arithmetic can be divided into some special purpose integer predicates and a series of general predicates for floating point and integer arithmetic as appropriate. The integer predicates are as ``logical'' as possible. Their usage is recommended whenever applicable, resulting in faster and more ``logical'' programs.

The general arithmetic predicates are optionally compiled now (see set_prolog_flag2 and the -O command line option). Compiled arithmetic reduces global stack requirements and improves performance. Unfortunately compiled arithmetic cannot be traced, which is why it is optional.

The general arithmetic predicates all handle expressions. An expression is either a simple number or a function. The arguments of a function are expressions. The functions are described in functions.

between3+Low, +High, ?Value Low and High are integers, $\arg{High} \geq \arg{Low}$. If Value is an integer, $\arg{Low} \leq \arg{Value} \leq \arg{High}$. When Value is a variable it is successively bound to all integers between Low and High. succ2?Int1, ?Int2 Succeeds if $\arg{Int2} = \arg{Int1} + 1$. At least one of the arguments must be instantiated to an integer. plus3?Int1, ?Int2, ?Int3 Succeeds if $\arg{Int3} = \arg{Int1} + \arg{Int2}$. At least two of the three arguments must be instantiated to integers. +Expr1+Expr2 Succeeds when expression Expr1 evaluates to a larger number than Expr2. +Expr1+Expr2 Succeeds when expression Expr1 evaluates to a smaller number than Expr2. +Expr1+Expr2 Succeeds when expression Expr1 evaluates to a smaller or equal number to Expr2. +Expr1+Expr2 Succeeds when expression Expr1 evaluates to a larger or equal number to Expr2. +Expr1+Expr2 Succeeds when expression Expr1 evaluates to a number non-equal to Expr2. +Expr1+Expr2 Succeeds when expression Expr1 evaluates to a number equal to Expr2. is-Number+Expr Succeeds when Number has successfully been unified with the number Expr evaluates to. If Expr evaluates to a float that can be represented using an integer (i.e. the value is integer and within the range that can be described by Prolog's integer representation), Expr is unified with the integer value.

Note that normally, is2 will be used with unbound left operand. If equality is to be tested, =:=/2 should be used. For example:

?- 1.0 is sin(pi/2). Fails!. sin(pi/2) evaluates to 1.0, but is2 will represent this as the integer 1, after which unify will fail.
?- 1.0 is float(sin(pi/2)). Succeeds, as the float1 function forces the result to be float.
?- 1.0 =:= sin(pi/2). Succeeds as expected.


next up previous contents index
Next: Arithmetic Functions Up: Built-in predicates Previous: Character Conversion   Contents   Index
Dr. Richard Botting 2001-12-12