-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex2-07-interval-arithmetic.scm
71 lines (45 loc) · 1.21 KB
/
ex2-07-interval-arithmetic.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;; Ex. 2.7 Extended Exercise Interval arithmetic
;; Rp = (/ 1 (+ (/ 1 R1) (/ 1 R2)))
;;
;; Given information, a.k.a what Alyssa did
;;
(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 (div-interval x y)
(mul-interval
x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(define (make-interval a b) (cons a b))
;;
;; Ex. 2.7 Define lower-bound and upper-bound
;;
(define (lower-bound i) (car i))
(define (upper-bound i) (cdr i))
;;
;; Results
;;
(define R1 (make-interval 0.5 1.0))
;Value: r1
R1
;Value 14: (.5 . 1.)
(define R2 (make-interval 0.7 1.2))
;Value: r2
(lower-bound R1)
;Value: .5
(upper-bound r2)
;Value: 1.2
(add-interval r1 r2)
;Value 16: (1.2 . 2.2)
(mul-interval r1 r2)
;Value 17: (.35 . 1.2)
(div-interval r1 r2)
;Value 21: (.4166666666666667 . 1.4285714285714286)