# Rounding Operations

We supply a number of binary and unary operations that deal with rounding. We find different rounding modes and we also find different forms of division. The result type of the evaluable function integer/1 is always an integer. On the other hand the result type of the evaluable functions truncate/1, floor/1, ceiling/1 and round/1 is the same as the argument type:

integer, truncate:        towards zero
floor:        towards lower infinity
ceiling:        towards upper infinity
round:        towards nearest neighbour or then away from zero
Examples:
`floor(-3)         --> -3floor(-3.14)      --> -4floor(-0d3.1415)  --> -4`

The division is based on a hypothetical /F operation. This operation is approximate for float arguments and exact for integer and decimal arguments. The result type of the evaluable functions (//)/2 and (div)/2 is always an integer. On the other hand the result type of the eval-uable functions (rem)/2 and (mod)/2 is the same as the argument types:

X // Y = integer(X /F Y).
X div Y = integer(floor(X /F Y)).
X rem Y = X – (X // Y) * Y.
X mod Y = X – (X div Y) * Y.
Examples:
`5 // 2            --> 25.0 // 2.0        --> 20d5.00 // 2       --> 2(-5) // 2         --> -2(-5) div 2        --> -35 rem 2           --> 15.0 rem 2.0       --> 1.00d5.00 rem 2      --> 0d1.00(-5) rem 2        --> -1(-5) mod 2        --> 1`

If the arguments of the binary operations have different types the same widening as already defined for the basic operations is applied. This means the widening is done towards the bigger domain of the two arguments.

The following rounding operations are recognized in evaluations:

integer(X):
If X is a number the returns the integer of X.
truncate(X): [ISO 9.1.7]
If X is a number then returns the rounding of X towards zero.
floor(X): [ISO 9.1.7]
If X is a number then returns the rounding of X towards negative infinity.
ceiling(X): [ISO 9.1.7]
If X is a number then returns the rounding of X towards positive infinity.
round(X): [ISO 9.1.7]
If X is a number then returns the rounding of X towards the nearest integer. If the absolute fraction of the number is 0.5 then returns the rounding away from zero
X // Y: [ISO 9.1.7]
If X and Y are both numbers then the function returns the truncated division of X by Y.
X rem Y: [ISO 9.1.7]
If X and Y are both numbers then the function returns the remainder of X by Y.
X div Y: [TC2 9.1.3]
If X and Y are both numbers then the function returns the floored division of X by Y.
X mod Y: [ISO 9.1.7]
If X and Y are both numbers then the function returns the modulus of X by Y.

The following Prolog flags for rounding operations are provided:

integer_rounding_function: [ISO 7.11.1.4]
Legal values are down [ISO] and toward_zero [ISO]. The flag indicates how the (//)/2 and rem/2 are performed. Default value is toward_zero. The value cannot be changed.