| 1 |
;;; |
| 2 |
;;;scheme.division - R7RS Tangerine |
| 3 |
;;; |
| 4 |
|
| 5 |
;; Originally SRFI-141 |
| 6 |
(define-module scheme.division |
| 7 |
(export ceiling/ ceiling-quotient ceiling-remainder |
| 8 |
round/ round-quotient round-remainder |
| 9 |
euclidean/ euclidean-quotient euclidean-remainder |
| 10 |
balanced/ balanced-quotient balanced-remainder |
| 11 |
|
| 12 |
;; These are in core (R7RS) |
| 13 |
floor/ floor-quotient floor-remainder |
| 14 |
truncate/ truncate-quotient truncate-remainder)) |
| 15 |
(select-module scheme.division) |
| 16 |
|
| 17 |
(define (ceiling/ n d) |
| 18 |
(receive (q r) (quotient&remainder n d) |
| 19 |
(if (>= n 0) |
| 20 |
(if (and (> d 0) (not (zero? r))) |
| 21 |
(values (+ q 1) (- r d)) |
| 22 |
(values q r)) |
| 23 |
(if (or (> d 0) (zero? r)) |
| 24 |
(values q r) |
| 25 |
(values (+ q 1) (- r d)))))) |
| 26 |
|
| 27 |
(define (ceiling-quotient n d) |
| 28 |
(values-ref (ceiling/ n d) 0)) |
| 29 |
|
| 30 |
(define (ceiling-remainder n d) |
| 31 |
(values-ref (ceiling/ n d) 1)) |
| 32 |
|
| 33 |
;; euclidean is same as R6RS div/mod, except checks for integers. |
| 34 |
|
| 35 |
(define (euclidean/ n d) |
| 36 |
(check-arg integer? n) |
| 37 |
(check-arg integer? d) |
| 38 |
(div-and-mod n d)) |
| 39 |
|
| 40 |
(define (euclidean-quotient n d) |
| 41 |
(check-arg integer? n) |
| 42 |
(check-arg integer? d) |
| 43 |
(div n d)) |
| 44 |
|
| 45 |
(define (euclidean-remainder n d) |
| 46 |
(check-arg integer? n) |
| 47 |
(check-arg integer? d) |
| 48 |
(mod n d)) |
| 49 |
|
| 50 |
;; balanced is same as R6RS div0/mod0, except checks for integers. |
| 51 |
|
| 52 |
(define (balanced/ n d) |
| 53 |
(check-arg integer? n) |
| 54 |
(check-arg integer? d) |
| 55 |
(div0-and-mod0 n d)) |
| 56 |
|
| 57 |
(define (balanced-quotient n d) |
| 58 |
(check-arg integer? n) |
| 59 |
(check-arg integer? d) |
| 60 |
(div0 n d)) |
| 61 |
|
| 62 |
(define (balanced-remainder n d) |
| 63 |
(check-arg integer? n) |
| 64 |
(check-arg integer? d) |
| 65 |
(mod0 n d)) |
| 66 |
|
| 67 |
;; round and balanced only differ when n/d exactly falls on the midpoint. |
| 68 |
;; in inexact case, we simply use 'round' for it's faster. we do need |
| 69 |
;; zero divisor check for that. |
| 70 |
|
| 71 |
(define (%exact-round/ n d) |
| 72 |
(receive (q r) (div0-and-mod0 n d) |
| 73 |
(if (and (odd? q) |
| 74 |
(even? d) |
| 75 |
(= (* (abs r) 2) (abs d))) |
| 76 |
(if (> d 0) |
| 77 |
(values (- q 1) (- r)) |
| 78 |
(values (+ q 1) (- r))) |
| 79 |
(values q r)))) |
| 80 |
|
| 81 |
(define (round/ n d) |
| 82 |
(check-arg integer? n) |
| 83 |
(check-arg integer? d) |
| 84 |
(when (zero? d) (error "Attempt to calculate a division by zero")) |
| 85 |
(if (and (exact? n) (exact? d)) |
| 86 |
(%exact-round/ n d) |
| 87 |
(let1 q (round (/ n d)) |
| 88 |
(values q (- n (* d q)))))) |
| 89 |
|
| 90 |
(define (round-quotient n d) |
| 91 |
(check-arg integer? n) |
| 92 |
(check-arg integer? d) |
| 93 |
(when (zero? d) (error "Attempt to calculate a division by zero")) |
| 94 |
(if (and (exact? n) (exact? d)) |
| 95 |
(values-ref (%exact-round/ n d) 0) |
| 96 |
(round (/ n d)))) |
| 97 |
|
| 98 |
(define (round-remainder n d) |
| 99 |
(check-arg integer? n) |
| 100 |
(check-arg integer? d) |
| 101 |
(when (zero? d) (error "Attempt to calculate a division by zero")) |
| 102 |
(if (and (exact? n) (exact? d)) |
| 103 |
(values-ref (%exact-round/ n d) 1) |
| 104 |
(- n (* d (round (/ n d)))))) |