Re: Integer residue-classes William D Clinger (22 Feb 2006 22:44 UTC)
Re: Integer residue-classes Michael Sperber (26 Feb 2006 14:57 UTC)
Re: Integer residue-classes John Cowan (26 Feb 2006 15:52 UTC)
Re: Integer residue-classes Paul Schlie (26 Feb 2006 16:58 UTC)
Re: Integer residue-classes Paul Schlie (05 Mar 2006 18:52 UTC)
Re: Integer residue-classes Paul Schlie (12 Mar 2006 15:29 UTC)
miscellaneous request Paul Schlie (12 Mar 2006 15:47 UTC)
miscellaneous request (last one) Paul Schlie (12 Mar 2006 17:27 UTC)

Re: Integer residue-classes Paul Schlie 05 Mar 2006 18:51 UTC

Or possibly alternatively, being somewhat simpler:

; n/d :: (+ (div n d) (rem/d n d))       ; symmetric about 0
; n/d :: (+ (quo n d) (abs (mod/d n d))) ; asymmetric about 0

(define (/: n d) ; for NaN vs. div/0 error.
  (if (= d 0) +nan.0 (/ n d)))

;---

(define (div n d) ; symmetric about 0
  (truncate (/: n d)))

(define (rem n d) ; same sign as (div n d)
  ((if (< d 0) - +) (- n (* (div n d) d))))

(define (rem/d n d) ; remainder fraction
  (/: (rem n d) (abs d)))

; n/d :: (+ (div n d) (rem/d n d))

;---

(define (quo n d) ; asymmetric about 0
  (floor (/: n d)))

(define (mod n d) ; same sign as d
  (- n (* (quo n d) d)))

(define (mod/d n d) ; modular fraction
  (/: (mod n d) (abs d)))

; n/d :: (+ (quo n d) (abs (mod/d n d)))

Thereby:

(div x 0)   :: (quo x 0)   => NaN
(rem x 0)   :: (mod x 0)   => x
(rem/d x 0) :: (mod/d x 0) => NaN