### 8.2 Arithmetic

Arithmetic is performed by built-in predicates, which take as arguments arithmetic expressions and evaluate them. An arithmetic expression is a term built from numbers, variables, and functors that represent arithmetic functions. At the time of evaluation, each variable in an arithmetic expression must be bound to a non-variable expression. An expression evaluates to a number, which may be an integer or a float.

The range of integers is `[-2^2147483616, 2^2147483616)`. Thus for all practical purposes, the range of integers can be considered infinite.

The range of floats is the one provided by the C `double` type, typically `[4.9e-324, 1.8e+308]` (plus or minus). In case of overflow or division by zero, `iso` execution mode will raise an evaluation error exception. In `sicstus` execution mode no exceptions will be raised, instead appropriate infinity values, as defined by the IEEE standard, will be used.

Only certain functors are permitted in an arithmetic expression. These are listed below, together with an indication of the functions they represent. X and Y are assumed to be arithmetic expressions. Unless stated otherwise, the arguments of an expression may be any numbers and its value is a float if any of its arguments is a float; otherwise, the value is an integer. Any implicit coercions are performed with the `integer/1` and `float/1` functions.

The arithmetic functors are annotated with [ISO], [ISO only], or [SICStus only], with the same meaning as for the built-in predicates; see ISO Compliance.

`+(`X`)`
The value is X.
`-`X
The value is the negative of X. ISO
X`+`Y
The value is the sum of X and Y. ISO
X`-`Y
The value is the difference of X and Y. ISO
X`*`Y
The value is the product of X and Y. ISO
X`/`Y
The value is the float quotient of X and Y. ISO
X`//`Y ISO
The value is the integer quotient of X and Y. The result is always truncated towards zero. In `iso` execution mode X and Y have to be integers.
X` rem `Y ISO
The value is the integer remainder after dividing X by Y, i.e. `integer(`X`)-integer(`Y`)*(`X`//`Y`)`. The sign of a nonzero remainder will thus be the same as that of the dividend. In `iso` execution mode X and Y have to be integers.
X` mod `Y ISO only
The value is X modulo Y, i.e. `integer(`X`)-integer(`Y`)*floor(`X`/`Y`)`. The sign of a nonzero remainder will thus be the same as that of the divisor. X and Y have to be integers.
X` mod `Y SICStus only
The value is the same as that of X rem Y.
`integer(`X`)`
The value is the closest integer between X and 0, if X is a float; otherwise, X itself.
`float_integer_part(`X`) `ISO
The same as `float(integer(`X`))`. In `iso` execution mode, X has to be a float.
`float_fractional_part(`X`) `ISO
The value is the fractional part of X, i.e. X``` - float_integer_part(```X`)`. In `iso` execution mode, X has to be a float.
`float(`X`) `ISO
The value is the float equivalent of X, if X is an integer; otherwise, X itself.
X`/\`Y ISO
The value is the bitwise conjunction of the integers X and Y. In `iso` execution mode X and Y have to be integers.
X`\/`Y ISO
The value is the bitwise disjunction of the integers X and Y. In `iso` execution mode X and Y have to be integers.
X`#`Y
The value is the bitwise exclusive or of the integers X and Y.
`\(`X`) `ISO
The value is the bitwise negation of the integer X. In `iso` execution mode X has to be an integer.
X`<<`Y ISO
The value is the integer X shifted left by Y places. In `iso` execution mode X and Y have to be integers.
X`>>`Y ISO
The value is the integer X shifted right by Y places. In `iso` execution mode X and Y have to be integers.
`[`X`]`
A list of just one number X evaluates to X. Since a quoted string is just a list of integers, this allows a quoted character to be used in place of its character code; e.g. `"A"` behaves within arithmetic expressions as the integer 65.

SICStus Prolog also includes an extra set of functions listed below. These may not be supported by other Prologs. All trigonometric and transcendental functions take float arguments and deliver float values. The trigonometric functions take arguments or deliver values in radians.

`abs(`X`) `ISO
The value is the absolute value of X.
`sign(`X`) `ISO
The value is the sign of X, i.e. -1, if X is negative, 0, if X is zero, and 1, if X is positive, coerced into the same type as X (i.e. the result is an integer, iff X is an integer).
`gcd(`X`,`Y`)`
The value is the greatest common divisor of the two integers X and Y. In `iso` execution mode X and Y have to be integers.
`min(`X`,`Y`)`
The value is the lesser value of X and Y.
`max(`X`,`Y`)`
The value is the greater value of X and Y.
`msb(`X`)`
The value is the position of the most significant nonzero bit of the integer X, counting bit positions from zero. It is equivalent to, but more efficient than, `integer(log(2,X))`. X must be greater than zero, and in `iso` execution mode, X has to be an integer.
`round(`X`) `ISO only
The value is the closest integer to X. X has to be a float. If X is exactly half-way between two integers, it is rounded up (i.e. the value is the least integer greater than X).
`round(`X`) `SICStus only
The value is the float that is the closest integral value to X. If X is exactly half-way between two integers, it is rounded to the closest even integral value.
`truncate(`X`) `ISO only
The value is the closest integer between X and 0. X has to be a float.
`truncate(`X`) `SICStus only
The value is the float that is the closest integer between X and 0.
`floor(`X`) `ISO only
The value is the greatest integer less or equal to X. X has to be a float.
`floor(`X`) `SICStus only
The value is the float that is the greatest integral value less or equal to X.
`ceiling(`X`) `ISO only
The value is the least integer greater or equal to X. X has to be a float.
`ceiling(`X`) `SICStus only
The value is the float that is the least integral value greater or equal to X.
`sin(`X`) `ISO
The value is the sine of X.
`cos(`X`) `ISO
The value is the cosine of X.
`tan(`X`)`
The value is the tangent of X.
`cot(`X`)`
The value is the cotangent of X.
`sinh(`X`)`
The value is the hyperbolic sine of X.
`cosh(`X`)`
The value is the hyperbolic cosine of X.
`tanh(`X`)`
The value is the hyperbolic tangent of X.
`coth(`X`)`
The value is the hyperbolic cotangent of X.
`asin(`X`)`
The value is the arc sine of X.
`acos(`X`)`
The value is the arc cosine of X.
`atan(`X`) `ISO
The value is the arc tangent of X.
`atan2(`X`,`Y`)`
The value is the four-quadrant arc tangent of X and Y.
`acot(`X`)`
The value is the arc cotangent of X.
`acot2(`X`,`Y`)`
The value is the four-quadrant arc cotangent of X and Y.
`asinh(`X`)`
The value is the hyperbolic arc sine of X.
`acosh(`X`)`
The value is the hyperbolic arc cosine of X.
`atanh(`X`)`
The value is the hyperbolic arc tangent of X.
`acoth(`X`)`
The value is the hyperbolic arc cotangent of X.
`sqrt(`X`) `ISO
The value is the square root of X.
`log(`X`) `ISO
The value is the natural logarithm of X.
`log(`Base`,`X`)`
The value is the logarithm of X in the base Base.
`exp(`X`) `ISO
The value is the natural exponent of X.
X` ** `Y ISO
`exp(`X`,`Y`)`
The value is X raised to the power of Y.
`inf `SICStus only
The value is infinity as defined in the IEEE standard.
`nan `SICStus only
The value is not-a-number as defined in the IEEE standard.

Variables in an arithmetic expression to be evaluated may be bound to other arithmetic expressions rather than just numbers, e.g.:

```     evaluate(Expression, Answer) :- Answer is Expression.

| ?- evaluate(24*9, Ans).
Ans = 216
```

Arithmetic expressions, as described above, are just data structures. If you want one evaluated you must pass it as an argument to one of the built-in predicates listed below. Note that `is/2` only evaluates one of its arguments, whereas all the comparison predicates evaluate both of theirs. In the following, X and Y stand for arithmetic expressions, and Z for some term.

Z` is `X ISO
X, which must be an arithmetic expression, is evaluated and the result is unified with Z.
X` =:= `Y ISO
The numeric values of X and Y are equal.
X` =\= `Y ISO
The numeric values of X and Y are not equal.
X` < `Y ISO
The numeric value of X is less than the numeric value of Y.
X` > `Y ISO
The numeric value of X is greater than the numeric value of Y.
X` =< `Y ISO
The numeric value of X is less than or equal to the numeric value of Y.
X` >= `Y ISO
The numeric value of X is greater than or equal to the numeric value of Y.