next up previous
Next: Adding Arithmetic Functions Up: Built-In Predicates Previous: Arithmetic

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:

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 ( sin/1, exp/1, 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 percision, error handling, etc.

- +Expr
 

+Expr1 + +Expr2
 

+Expr1 - +Expr2
 

+Expr1 * +Expr2
 

+Expr1 / +Expr2
 
+IntExpr1 mod +IntExpr2

  (remainder of division).

+IntExpr1 // +IntExpr2
  (integer division).
abs( +Expr)

  Evaluate Expr and return the absolute value of it.
max( +Expr1, +Expr2)

  Evaluates to the largest of both Expr1 and Expr2.
min( +Expr1, +Expr2)

  Evaluates to the smallest of both Expr1 and Expr2.
.( +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 tranformed into a string object. The recommended way to specify the ASCII value of the letter `a' is 0'a .

random( +Int)

  Evaluates to a random integer i for which . The seed of this random generator is determined by the system clock when SWI-Prolog was started.
integer( +Expr)

  Evaluates Expr and rounds the result to the nearest integer.
floor( +Expr)

  Evaluates Expr and returns the largest integer smaller or equal to the result of the evaluation.
ceil( +Expr)

  Evaluates Expr and returns the smallest integer larger or equal to the result of the evaluation.

+IntExpr >> +IntExpr
  Bitwise shift IntExpr1 by IntExpr2 bits to the right. Note that integers are only 27 bits.

+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.
+IntExpr xor +IntExpr

  Bitwise `exclusive or' IntExpr1 and IntExpr2.

\ +IntExpr
  Bitwise negation.
sqrt( +Expr)

 
sin( +Expr)

  . Expr is the angle in radials.
cos( +Expr)

  . Expr is the angle in radials.
tan( +Expr)

  . Expr is the angle in radials.
asin( +Expr)

  . Result is the angle in radials.
acos( +Expr)

  . Result is the angle in radials.
atan( +Expr)

  . Result is the angle in radials.
atan( +YExpr, +XExpr)

  . Result is the angle in radials. The return value is in the range . Used to convert between rectangular and polar coordinate system.
log( +Expr)

 
log10( +Expr)

 
exp( +Expr)

 

+Expr1 ^ +Expr2
 
pi

  Evaluates to the mathematical constant (3.141593).
e

  Evaluates to the mathematical constant e (2.718282).
cputime

  Evaluates to a floating point number expressing the cpu time (in seconds) used by Prolog up till now. See also statistics/2 and time/1.



next up previous
Next: Adding Arithmetic Functions Up: Built-In Predicates Previous: Arithmetic



Passani Luca
Tue Nov 14 08:58:33 MET 1995