-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29-divide-two-integers.rkt
More file actions
35 lines (29 loc) · 897 Bytes
/
29-divide-two-integers.rkt
File metadata and controls
35 lines (29 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#lang racket
(define MIN -2147483648)
(define MAX 2147483647)
(define << arithmetic-shift)
(define (>>1 n) (<< n -1))
(define (<<1 n) (<< n 1))
(define (align x y)
(let ([xl (integer-length x)]
[yl (integer-length y)])
(<< y (max 0 (- xl yl)))))
(define (divide-nat dividend divisor)
(let loop ([x dividend]
[y (align dividend divisor)]
[r 0])
(cond [(= y divisor)
(if (< x y) r (+ r 1))]
[(< x y) (loop x (>>1 y) (<<1 r))]
[else (loop (- x y) y (+ r 1))])))
(define (divide dividend divisor)
(if (and (= dividend MIN)
(= divisor -1))
MAX
((if (xor (< dividend 0)
(< divisor 0))
- +)
;; oops, overflow here!
;; should convert both to negative,
;; but I'm too lazy to pretend that we have 32bit integers
(divide-nat (abs dividend) (abs divisor)))))