ECLiPSe distinguishes three types of numbers: integers, rationals and floats. Floats are either single or double precision, depending on the value of the global flag float_precision. Numbers of different types do not unify. To help bug detection, the arithmetic predicates raise events when an attempt is made to unify numbers of different types. The system performs automatic type conversions in the direction integer -> rational -> single float -> double float These conversions are done (i) to make the types of two input arguments equal and (ii) to lift the type of an input argument to the one expected by the function. The result type is the lifted input type, unless otherwise specified.
A table of predefined arithmetic functions is given below. A predefined function is evaluated by first evaluating its arguments and then calling the corresponding evaluation predicate. The evaluation predicate belonging to a compound term func(a_1,..,a_n) is the predicate func/(n+1). It receives a_1,..,a_n as its first n arguments and returns an integer or real result as its last argument. This result is then used in the arithmetic computation.
This evaluation mechanism outlined above is not restricted to the predefined arithmetic functors shown in the table. In fact it works for all atoms and compound terms. It is therefore possible to define a new arithmetic operation by just defining an evaluation predicate. Similarly, many ECLiPSe built-ins return numbers in the last argument and can thus be used as evaluation predicates (e.g.cputime/1, random/1, string_length/2, ...). Note that recursive evaluation of arguments is only done for the predefined arithmetic functions, for the others the arguments are simply passed to the evaluation predicate.
Most arithmetic errors will not be reported in is/2, but in the evaluation predicate where it occured.
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 number number ceiling(E) round up number number round(E) round to nearest number number E1 + E2 addition number x number number E1 - E2 subtraction number x number number E1 * E2 multiplication number x number number E1 / E2 division number x number see below E1 // E2 integer division integer x integer integer E1 mod E2 modulus operation integer x integer integer E1 ^ E2 power operation number x number number min(E1,E2) minimum of 2 values number x number number max(E1,E2) maximum of 2 values number x number number \ E bitwise complement integer integer E1 /\ E2 bitwise conjunction integer x integer integer E1 \/ E2 bitwise disjunction integer x integer integer xor(E1,E2) bitwise exclusive or integer x integer integer E1 >> E2 shift E1 right by E2 bits integer x integer integer E1 << E2 shift E1 left by E2 bits integer x integer integer setbit(E1,E2) set bit E2 in E1 integer x integer integer clrbit(E1,E2) clear bit E2 in E1 integer x integer integer getbit(E1,E2) get of bit E2 in E1 integer x integer integer sin(E) trigonometric function number float cos(E) trigonometric function number float tan(E) trigonometric function number float asin(E) trigonometric function number float acos(E) trigonometric function number float atan(E) trigonometric function number float exp(E) exponential function ex number float ln(E) natural logarithm number float sqrt(E) square root number float pi the constant pi --- float e the constant e --- float fix(E) truncate to integer number integer float(E) convert to float number float rational(E) convert to rational number rational numerator(E) numerator of rational integer or rational integer denominator(E) denominator of rational integer or rational integer sum(E) sum of list elements list number eval(E) eval runtime expression term numberThe division operator / yields either a rational or a float result, depending on the value of the global flag prefer_rationals. The same is true for the result of ^ if an integer is raised to a negative integral power. The relation between integer division // and modulus operation mod is as follows:
X =:= (X mod Y) + (X // Y) * Y.
Success: 103 is 3 + 4 * 5 ^ 2. X is asin(sin(pi/4)). (gives X = 0.785398). Y is 2 * 3, X is 4 + Y. (gives X = 10, Y = 6). X is string_length("four") + 1. (gives X = 5). [eclipse]: [user]. myconst(4.56). user compiled 40 bytes in 0.02 seconds yes. [eclipse]: 5.56 is myconst + 1. yes. Fail: 3.14 is pi. % different values Error: X is _. (Error 4) atom is 4. (Error 5) 1 is 1.0. (Error 5) X is "s". (Error 24) [eclipse]: X is undef(1). calling an undefined procedure undef(1, _g63) in ... [eclipse]: X is 3 + Y. instantiation fault in +(3, _g45, _g53)