Re: Integer residue-classes [was: Questions about srfi-77 Generic Arithmetic] William D Clinger (21 Feb 2006 16:21 UTC)
Re: Integer residue-classes Michael Sperber (22 Feb 2006 11:10 UTC)

Re: Integer residue-classes [was: Questions about srfi-77 Generic Arithmetic] William D Clinger 21 Feb 2006 16:21 UTC

Oops again.  I made another mistake, of a more fundamental
nature, in my proposed definitions of div, mod, quo, and rem.
Here are the corrected definitions:

    (div q1 q2)                  ==> nd
    (mod q1 q2)                  ==> qm

    (quo q1 q2)                  ==> nq
    (rem q1 q2)                  ==> qr

where:

   1. q1 = nd * q2 + qm
   2. 0 <= qm < |q2|          if q2 <> 0
   3. q1 = nq * q2 + qr
   4. -|q2/2| <= qr < |q2/2|    if q2 <> 0

With this definition,

    (div 5 3)    ==>  1
    (div 5 -3)   ==>  -1

    (mod 5 3)    ==>  2
    (mod 5 -3)   ==>  2

    (quo 5 3)    ==>  2
    (quo 5 -3)   ==>  -2

    (rem 5 3)    ==>  -1
    (rem 5 -3)   ==>  -1

To make these procedures as similar as possible to the div
and mod of SRFI 77, let nd=nq=0 and qm=qr=q1 when q2=0.

What is interesting about these definitions is that the
absolute value signs can themselves be considered ad hoc.
Both div and quo are discontinuous in their second argument
at 0, and there's no getting around it.

Will