[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Integer residue-classes



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