Debian Codegraph @

%3a141/srfi-141-impl.scm

raw symbols SCIP
1
2 ;;; -*- Mode: Scheme -*-
3
4 ;;;; Integer Division Operators
5
6 ;;; Given a QUOTIENT and REMAINDER defined for nonnegative numerators
7 ;;; and positive denominators implementing the truncated, floored, or
8 ;;; Euclidean integer division, this implements a number of other
9 ;;; integer division operators.
10
11 ;;; Copyright (c) 2010--2011 Taylor R. Campbell
12 ;;; All rights reserved.
13 ;;;
14 ;;; Redistribution and use in source and binary forms, with or without
15 ;;; modification, are permitted provided that the following conditions
16 ;;; are met:
17 ;;; 1. Redistributions of source code must retain the above copyright
18 ;;; notice, this list of conditions and the following disclaimer.
19 ;;; 2. Redistributions in binary form must reproduce the above copyright
20 ;;; notice, this list of conditions and the following disclaimer in the
21 ;;; documentation and/or other materials provided with the distribution.
22 ;;;
23 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 ;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 ;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 ;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 ;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 ;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 ;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 ;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 ;;; SUCH DAMAGE.
34
35 ;;;; Shims
36 ;;; SRFI-8
37 (define-syntax receive
38 (syntax-rules ()
39 ((receive formals expression body ...)
40 (call-with-values (lambda () expression)
41 (lambda formals body ...)))))
42
43 ;;; exact-integer?
44 (define (exact-integer? x) (and (integer? x) (exact? x)))
45
46 ;;;; Integer Division
47
48 ;;;; Ceiling
49
50 (define (ceiling/ n d)
51 (if (and (exact-integer? n) (exact-integer? d))
52 (cond ((and (negative? n) (negative? d))
53 (ceiling-/- n d))
54 ((negative? n)
55 (let ((n (- 0 n)))
56 (values (- 0 (quotient n d)) (- 0 (remainder n d)))))
57 ((negative? d)
58 (let ((d (- 0 d)))
59 (values (- 0 (quotient n d)) (remainder n d))))
60 (else
61 (ceiling+/+ n d)))
62 (let ((q (ceiling (/ n d))))
63 (values q (- n (* d q))))))
64
65 (define (ceiling-/- n d)
66 (let ((n (- 0 n)) (d (- 0 d)))
67 (let ((q (quotient n d)) (r (remainder n d)))
68 (if (zero? r)
69 (values q r)
70 (values (+ q 1) (- d r))))))
71
72 (define (ceiling+/+ n d)
73 (let ((q (quotient n d)) (r (remainder n d)))
74 (if (zero? r)
75 (values q r)
76 (values (+ q 1) (- r d)))))
77
78 (define (ceiling-quotient n d)
79 (if (and (exact-integer? n) (exact-integer? d))
80 (cond ((and (negative? n) (negative? d))
81 (receive (q r) (ceiling-/- n d) r q))
82 ((negative? n) (- 0 (quotient (- 0 n) d)))
83 ((negative? d) (- 0 (quotient n (- 0 d))))
84 (else (receive (q r) (ceiling+/+ n d) r q)))
85 (ceiling (/ n d))))
86
87 (define (ceiling-remainder n d)
88 (if (and (exact-integer? n) (exact-integer? d))
89 (cond ((and (negative? n) (negative? d))
90 (receive (q r) (ceiling-/- n d) q r))
91 ((negative? n) (- 0 (remainder (- 0 n) d)))
92 ((negative? d) (remainder n (- 0 d)))
93 (else (receive (q r) (ceiling+/+ n d) q r)))
94 (- n (* d (ceiling (/ n d))))))
95
96 ;;;; Euclidean Division
97
98 ;;; 0 <= r < |d|
99
100 (define (euclidean/ n d)
101 (if (and (exact-integer? n) (exact-integer? d))
102 (cond ((and (negative? n) (negative? d)) (ceiling-/- n d))
103 ((negative? n) (floor-/+ n d))
104 ((negative? d)
105 (let ((d (- 0 d)))
106 (values (- 0 (quotient n d)) (remainder n d))))
107 (else (values (quotient n d) (remainder n d))))
108 (let ((q (if (negative? d) (ceiling (/ n d)) (floor (/ n d)))))
109 (values q (- n (* d q))))))
110
111 (define (euclidean-quotient n d)
112 (if (and (exact-integer? n) (exact-integer? d))
113 (cond ((and (negative? n) (negative? d))
114 (receive (q r) (ceiling-/- n d) r q))
115 ((negative? n) (receive (q r) (floor-/+ n d) r q))
116 ((negative? d) (- 0 (quotient n (- 0 d))))
117 (else (quotient n d)))
118 (if (negative? d) (ceiling (/ n d)) (floor (/ n d)))))
119
120 (define (euclidean-remainder n d)
121 (if (and (exact-integer? n) (exact-integer? d))
122 (cond ((and (negative? n) (negative? d))
123 (receive (q r) (ceiling-/- n d) q r))
124 ((negative? n) (receive (q r) (floor-/+ n d) q r))
125 ((negative? d) (remainder n (- 0 d)))
126 (else (remainder n d)))
127 (- n (* d (if (negative? d) (ceiling (/ n d)) (floor (/ n d)))))))
128
129 ;;;; Floor
130
131 (define (floor/ n d)
132 (if (and (exact-integer? n) (exact-integer? d))
133 (cond ((and (negative? n) (negative? d))
134 (let ((n (- 0 n)) (d (- 0 d)))
135 (values (quotient n d) (- 0 (remainder n d)))))
136 ((negative? n) (floor-/+ n d))
137 ((negative? d) (floor+/- n d))
138 (else (values (quotient n d) (remainder n d))))
139 (let ((q (floor (/ n d))))
140 (values q (- n (* d q))))))
141
142 (define (floor-/+ n d)
143 (let ((n (- 0 n)))
144 (let ((q (quotient n d)) (r (remainder n d)))
145 (if (zero? r)
146 (values (- 0 q) r)
147 (values (- (- 0 q) 1) (- d r))))))
148
149 (define (floor+/- n d)
150 (let ((d (- 0 d)))
151 (let ((q (quotient n d)) (r (remainder n d)))
152 (if (zero? r)
153 (values (- 0 q) r)
154 (values (- (- 0 q) 1) (- r d))))))
155
156 (define (floor-quotient n d)
157 (if (and (exact-integer? n) (exact-integer? d))
158 (cond ((and (negative? n) (negative? d)) (quotient (- 0 n) (- 0 d)))
159 ((negative? n) (receive (q r) (floor-/+ n d) r q))
160 ((negative? d) (receive (q r) (floor+/- n d) r q))
161 (else (quotient n d)))
162 (floor (/ n d))))
163
164 (define (floor-remainder n d)
165 (if (and (exact-integer? n) (exact-integer? d))
166 (cond ((and (negative? n) (negative? d))
167 (- 0 (remainder (- 0 n) (- 0 d))))
168 ((negative? n) (receive (q r) (floor-/+ n d) q r))
169 ((negative? d) (receive (q r) (floor+/- n d) q r))
170 (else (remainder n d)))
171 (- n (* d (floor (/ n d))))))
172
173 ;;;; Round Ties to Even
174
175 (define (round/ n d)
176 (define (divide n d adjust leave)
177 (let ((q (quotient n d)) (r (remainder n d)))
178 (if (and (not (zero? r))
179 (or (and (odd? q) (even? d) (divisible? n (quotient d 2)))
180 (< d (* 2 r))))
181 (adjust (+ q 1) (- r d))
182 (leave q r))))
183 (if (and (exact-integer? n) (exact-integer? d))
184 (cond ((and (negative? n) (negative? d))
185 (divide (- 0 n) (- 0 d)
186 (lambda (q r) (values q (- 0 r)))
187 (lambda (q r) (values q (- 0 r)))))
188 ((negative? n)
189 (divide (- 0 n) d
190 (lambda (q r) (values (- 0 q) (- 0 r)))
191 (lambda (q r) (values (- 0 q) (- 0 r)))))
192 ((negative? d)
193 (divide n (- 0 d)
194 (lambda (q r) (values (- 0 q) r))
195 (lambda (q r) (values (- 0 q) r))))
196 (else
197 (let ((return (lambda (q r) (values q r))))
198 (divide n d return return))))
199 (let ((q (round (/ n d))))
200 (values q (- n (* d q))))))
201
202 (define (divisible? n d)
203 ;; This operation admits a faster implementation than the one given
204 ;; here.
205 (zero? (remainder n d)))
206
207 (define (round-quotient n d)
208 (if (and (exact-integer? n) (exact-integer? d))
209 (receive (q r) (round/ n d)
210 r ;ignore
211 q)
212 (round (/ n d))))
213
214 (define (round-remainder n d)
215 (if (and (exact-integer? n) (exact-integer? d))
216 (receive (q r) (round/ n d)
217 q ;ignore
218 r)
219 (- n (* d (round (/ n d))))))
220
221 ;;;; Truncate
222
223 (define (truncate/ n d)
224 (if (and (exact-integer? n) (exact-integer? d))
225 (cond ((and (negative? n) (negative? d))
226 (let ((n (- 0 n)) (d (- 0 d)))
227 (values (quotient n d) (- 0 (remainder n d)))))
228 ((negative? n)
229 (let ((n (- 0 n)))
230 (values (- 0 (quotient n d)) (- 0 (remainder n d)))))
231 ((negative? d)
232 (let ((d (- 0 d)))
233 (values (- 0 (quotient n d)) (remainder n d))))
234 (else
235 (values (quotient n d) (remainder n d))))
236 (let ((q (truncate (/ n d))))
237 (values q (- n (* d q))))))
238
239 (define (truncate-quotient n d)
240 (if (and (exact-integer? n) (exact-integer? d))
241 (cond ((and (negative? n) (negative? d)) (quotient (- 0 n) (- 0 d)))
242 ((negative? n) (- 0 (quotient (- 0 n) d)))
243 ((negative? d) (- 0 (quotient n (- 0 d))))
244 (else (quotient n d)))
245 (truncate (/ n d))))
246
247 (define (truncate-remainder n d)
248 (if (and (exact-integer? n) (exact-integer? d))
249 (cond ((and (negative? n) (negative? d))
250 (- 0 (remainder (- 0 n) (- 0 d))))
251 ((negative? n) (- 0 (remainder (- 0 n) d)))
252 ((negative? d) (remainder n (- 0 d)))
253 (else (remainder n d)))
254 (- n (* d (truncate (/ n d))))))
255
256 ;;; Copyright 2015 William D Clinger.
257 ;;;
258 ;;; Permission to copy this software, in whole or in part, to use this
259 ;;; software for any lawful purpose, and to redistribute this software
260 ;;; is granted subject to the restriction that all copies made of this
261 ;;; software must include this copyright and permission notice in full.
262 ;;;
263 ;;; I also request that you send me a copy of any improvements that you
264 ;;; make to this software so that they may be incorporated within it to
265 ;;; the benefit of the Scheme community.
266 ;;;
267 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268
269 (define (balanced/ x y)
270 (call-with-values
271 (lambda () (euclidean/ x y))
272 (lambda (q r)
273 (cond ((< r (abs (/ y 2)))
274 (values q r))
275 ((> y 0)
276 (values (+ q 1) (- x (* (+ q 1) y))))
277 (else
278 (values (- q 1) (- x (* (- q 1) y))))))))
279
280 (define (balanced-quotient x y)
281 (call-with-values
282 (lambda () (balanced/ x y))
283 (lambda (q r) q)))
284
285 (define (balanced-remainder x y)
286 (call-with-values
287 (lambda () (balanced/ x y))
288 (lambda (q r) r)))