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

Re: Integer residue-classes

This page is part of the web mail archives of SRFI 77 from before July 7th, 2015. The new archives for SRFI 77 contain all messages, not just those from before July 7th, 2015.



Personally, I find the following definitions more generally useful:

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

(define (quo q d) ; asymmetric about 0
  (if (= q 0) 0 (floor (/ q d))))

(define (mod q d) ; having the same sign as d
  (- q (* (if (= d 0) 0 (quo q d)) d)))

(define (div q d) ; symmetric about 0
  (if (= q 0) 0 (truncate (/ q d))))

(define (rem q d) ; having the same sign as (div q d)
  (* (if (= d 0) 1 (/ d (abs d))) (- q (* (if (= d 0) 0 (div q d)) d))))