-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex2.12-interval-arithmetic-make-center-percent.scm
58 lines (40 loc) · 1.37 KB
/
ex2.12-interval-arithmetic-make-center-percent.scm
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (make-interval a b) (cons a b))
(define (upper-bound i) (max (car i) (cdr i)))
(define (lower-bound i) (min (car i) (cdr i)))
(define (sub-interval x y)
(let ((new-lower-bound (- (lower-bound x) (upper-bound y)))
(new-upper-bound (- (upper-bound x) (lower-bound y))))
(make-interval new-lower-bound new-upper-bound)))
(define (width-interval i)
(/ (- (upper-bound i) (lower-bound i)) 2))
(define (make-center-width c w)
(make-interval (- c w) (+ c w)))
(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)
(/ (- (upper-bound i) (lower-bound i)) 2))
(define (make-center-percent c p)
(let ((w (* c p)))
(let ((lb (- c w))
(ub (+ c w)))
(lambda (m) (m c p w lb ub)))))
(define (upper-bound i)
(i (lambda (c p w lb ub) ub)))
(define (lower-bound i)
(i (lambda (c p w lb ub) lb)))
(define (width i)
(i (lambda (c p w lb ub) w)))
(define (center i)
(i (lambda (c p w lb ub) c)))
(define (percent i)
(i (lambda (c p w lb ub) p)))