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


Arithmetic Functions

Arithmetic functions are terms which are evaluated by the arithmetic predicates described above. SWI-Prolog tries to hide the difference between integer arithmetic and floating point arithmetic from the Prolog user. Arithmetic is done as integer arithmetic as long as possible and converted to floating point arithmetic whenever one of the arguments or the combination of them requires it. If a function returns a floating point value which is whole it is automatically transformed into an integer. There are three types of arguments to functions:

Expr Arbitrary expression, returning either a floating point value or an integer.
IntExpr Arbitrary expression that should evaluate into an integer.
Int An integer.

In case integer addition, subtraction and multiplication would lead to an integer overflow the operands are automatically converted to floating point numbers. The floating point functions (sin1, exp1, etc.) form a direct interface to the corresponding C library functions used to compile SWI-Prolog. Please refer to the C library documentation for details on precision, error handling, etc.

+Expr $\arg{Result} = -\arg{Expr}$ +Expr1+Expr2 $\arg{Result} = \arg{Expr1} + \arg{Expr2}$ +Expr1+Expr2 $\arg{Result} = \arg{Expr1} - \arg{Expr2}$ +Expr1+Expr2 $\arg{Result} = \arg{Expr1} \times \arg{Expr2}$ +Expr1+Expr2 $\arg{Result} = \frac{\arg{Expr1}}{\arg{Expr2}}$ mod+IntExpr1+IntExpr2 Modulo: Result = IntExpr1 - (IntExpr1 // IntExpr2) $\times$ IntExpr2 The function mod2 is implemented using the C % operator. It's behaviour with negtive values is illustrated in the table below.

2 = 17 mod 5
2 = 17 mod -5
-2 = -17 mod 5
-2 = -17 mod 5

rem+IntExpr1+IntExpr2 Remainder of division: Result = float_fractional_part(IntExpr1/IntExpr2) +IntExpr1+IntExpr2 Integer division: Result = truncate(Expr1/Expr2) abs1+Expr Evaluate Expr and return the absolute value of it. sign1+Expr Evaluate to -1 if $\arg{Expr} < 0$, 1 if $\arg{Expr} > 0$ and 0 if $\arg{Expr} = 0$. max2+Expr1, +Expr2 Evaluates to the largest of both Expr1 and Expr2. min2+Expr1, +Expr2 Evaluates to the smallest of both Expr1 and Expr2. 2+Int, [] A list of one element evaluates to the element. This implies "a" evaluates to the ASCII value of the letter `a' (97). This option is available for compatibility only. It will not work if `style_check(+string)' is active as "a" will then be transformed into a string object. The recommended way to specify the ASCII value of the letter `a' is 0'a. random1+Int Evaluates to a random integer i for which $0 \leq i < \arg{Int}$. The seed of this random generator is determined by the system clock when SWI-Prolog was started. round1+Expr Evaluates Expr and rounds the result to the nearest integer. integer1+Expr Same as round1 (backward compatibility). float1+Expr Translate the result to a floating point number. Normally, Prolog will use integers whenever possible. When used around the 2nd argument of is2, the result will be returned as a floating point number. In other contexts, the operation has no effect. float_fractional_part1+Expr Fractional part of a floating-point number. Negative if Expr is negative, 0 if Expr is integer. float_integer_part1+Expr Integer part of floating-point number. Negative if Expr is negative, Expr if Expr is integer. truncate1+Expr Truncate Expr to an integer. Same as float_integer_part1. floor1+Expr Evaluates Expr and returns the largest integer smaller or equal to the result of the evaluation. ceiling1+Expr Evaluates Expr and returns the smallest integer larger or equal to the result of the evaluation. ceil1+Expr Same as ceiling1 (backward compatibility). +IntExpr+IntExpr Bitwise shift IntExpr1 by IntExpr2 bits to the right. +IntExpr+IntExpr Bitwise shift IntExpr1 by IntExpr2 bits to the left. +IntExpr+IntExpr Bitwise `or' IntExpr1 and IntExpr2. +IntExpr+IntExpr Bitwise `and' IntExpr1 and IntExpr2. xor+IntExpr+IntExpr Bitwise `exclusive or' IntExpr1 and IntExpr2. +IntExpr Bitwise negation. sqrt1+Expr $\arg{Result} = \sqrt{\arg{Expr}}$ sin1+Expr $\arg{Result} = \sin{\arg{Expr}}$. Expr is the angle in radians. cos1+Expr $\arg{Result} = \cos{\arg{Expr}}$. Expr is the angle in radians. tan1+Expr $\arg{Result} = \tan{\arg{Expr}}$. Expr is the angle in radians. asin1+Expr $\arg{Result} = \arcsin{\arg{Expr}}$. Result is the angle in radians. acos1+Expr $\arg{Result} = \arccos{\arg{Expr}}$. Result is the angle in radians. atan1+Expr $\arg{Result} = \arctan{\arg{Expr}}$. Result is the angle in radians. atan2+YExpr, +XExpr $\arg{Result} = \arctan{\frac{\arg{YExpr}}{\arg{XExpr}}}$. Result is the angle in radians. The return value is in the range $[-\pi\ldots\pi]$. Used to convert between rectangular and polar coordinate system. log1+Expr $\arg{Result} = \ln{\arg{Expr}}$ log101+Expr $\arg{Result} = \lg{\arg{Expr}}$ exp1+Expr $\arg{Result} = \pow{e}{\arg{Expr}}$ +Expr1+Expr2 $\arg{Result} = \pow{\arg{Expr1}}{\arg{Expr2}}$ +Expr1+Expr2 Same as **/2. (backward compatibility). pi0 Evaluates to the mathematical constant $\pi$ (3.141593). e0 Evaluates to the mathematical constant $e$ (2.718282). cputime0 Evaluates to a floating point number expressing the CPU time (in seconds) used by Prolog up till now. See also statistics2 and time1.


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