chickadee » scheme » modulo

(quotient n[1] n[2])procedure
(remainder n[1] n[2])procedure
(modulo n[1] n[2])procedure

These procedures implement number-theoretic (integer) division. n[2] should be non-zero. All three procedures return integers. If n[1]/n[2] is an integer:

   (quotient n[1] n[2])           ===> n[1]/n[2]
   (remainder n[1] n[2])          ===> 0
   (modulo n[1] n[2])             ===> 0

If n[1]/n[2] is not an integer:

   (quotient n[1] n[2])           ===> n[q]
   (remainder n[1] n[2])          ===> n[r]
   (modulo n[1] n[2])             ===> n[m]

where n[q] is n[1]/n[2] rounded towards zero, 0 < |n[r]| < |n[2]|, 0 < |n[m]| < |n[2]|, n[r] and n[m] differ from n[1] by a multiple of n[2], n[r] has the same sign as n[1], and n[m] has the same sign as n[2].

From this we can conclude that for integers n[1] and n[2] with n[2] not equal to 0,

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

provided all numbers involved in that computation are exact.

(modulo 13 4)                   ===>  1
(remainder 13 4)                ===>  1

(modulo -13 4)                  ===>  3
(remainder -13 4)               ===>  -1

(modulo 13 -4)                  ===>  -3
(remainder 13 -4)               ===>  1

(modulo -13 -4)                 ===>  -1
(remainder -13 -4)              ===>  -1

(remainder -13 -4.0)            ===>  -1.0  ; inexact