chickadee » scheme » base » truncate-remainder

(floor/ n[1] n[2])procedure
(floor-quotient n[1] n[2])procedure
(floor-remainder n[1] n[2])procedure
(truncate/ n[1] n[2])procedure
(truncate-quotient n[1] n[2])procedure
(truncate-remainder n[1] n[2])procedure

These procedures implement number-theoretic (integer) division. It is an error if n[2] is zero. The procedures ending in / return two integers; the other procedures return an integer. All the procedures compute a quotient n[q] and remainder n[r] such that n[1] = n[2] * n[q] + n[r]. For each of the division operators, there are three procedures defined as follows:

(<operator>/ n[1] n[2]) ==> n[q] n[r]
(<operator>-quotient n[1] n[2]) ==> n[q]
(<operator>-remainder n[1] n[2]) ==> n[r]

The remainder n[r] is determined by the choice of integer n[q]: n[r] = n[1] − n[2] * n[q]. Each set of operators uses a different choice of n[q]:

floor    n[q] = ⌊n[1] / n[2]⌋
truncate n[q] = runcate(n[1] / n[2])

For any of the operators, and for integers n[1] and n[2] with n[2] not equal to 0,

(= n[1] (+ (* n[2] (<operator>-quotient n[1] n[2]))
        (<operator>-remainder n[1] n[2])))
        ==> #t

provided all numbers involved in that computation are exact.

Examples:

(floor/ 5 2)          ==> 2 1
(floor/ -5 2)         ==> -3 1
(floor/ 5 -2)         ==> -3 -1
(floor/ -5 -2)        ==> 2 -1
(truncate/ 5 2)       ==> 2 1
(truncate/ -5 2)      ==> -2 -1
(truncate/ 5 -2)      ==> -2 1
(truncate/ -5 -2)     ==> 2 -1
(truncate/ -5.0 -2)   ==> 2.0 -1.0