Module "round"

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: integer towards zero        
truncate: towards zero        
floor: towards lower infinity
ceiling: towards upper infinity
round: towards nearest neighbour or then away from zero
Examples:
floor(-3)         --> -3
floor(-3.14)     --> -4.0
floor(-0d3.1415) --> -0d4

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 / Y).
X div Y = integer(floor(X / Y)).
X rem Y = X – (X // Y) * Y.
X mod Y = X – (X div Y) * Y.
Examples:
5 // 2            --> 2
5.0 // 2.0       --> 2
0d5.00 // 2      --> 2
(-5) // 2         --> -2
(-5) div 2        --> -3
5 rem 2         --> 1
5.0 rem 2.0       --> 1.0
0d5.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.
divmod(X, Y, Z, T):
The predicate succeeds in Z with the division of X by Y, and in T with the modulo 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 integer (//)/2 and rem/2 are performed. Default value is toward_zero. The value cannot be changed.
float_rounding_function:
Legal values are down, up, toward_zero, away_zero, half_toward, half_away and half_even. The flag indicates how float number parsing and float evaluable functions are performed. Default value is half_even. The value cannot be changed.

Comments