-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_2_009.scm
131 lines (108 loc) · 2.47 KB
/
ex_2_009.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(define tolerance 0.0001)
;aproximately equal numbers
(define (== a b)
(< (abs (- a b))
tolerance
)
)
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(define (make-interval a b)
(cons a b)
)
(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 (sub-interval x y)
(make-interval
(- (lower-bound x) (lower-bound y) )
(- (upper-bound x) (upper-bound y) )
)
)
(define (interval-width x)
(/ (- (upper-bound x)
(lower-bound x)
)
2
)
)
(define (print-interval x)
(newline)
(display "(")
(display (lower-bound x))
(display "; ")
(display (upper-bound x))
(display ")")
)
(define R1 (make-interval 6.12 7.48))
(define R2 (make-interval 2.58 2.97))
(define R1-R2 (sub-interval R1 R2))
(define R1+R2 (add-interval R1 R2))
(define R1*R2 (mul-interval R1 R2))
(define R1/R2 (div-interval R1 R2))
(newline)
(display "Intervals")
(newline)
(print-interval R1)
(print-interval R2)
(print-interval R1-R2)
(print-interval R1+R2)
(print-interval R1*R2)
(print-interval R1/R2)
(newline)
(display "Interval widths")
(newline)
(interval-width R1)
(interval-width R2)
(interval-width R1-R2)
(interval-width R1+R2)
(interval-width R1*R2)
(interval-width R1/R2)
(newline)
(display "IW R1 - IW R2 = IW R1-R2: ")
(newline)
(== (interval-width R1-R2)
(- (interval-width R1)
(interval-width R2)
)
)
(newline)
(display "IW R1 + IW R2 = IW R1+R2: ")
(== (interval-width R1+R2)
(+ (interval-width R1)
(interval-width R2)
)
)
(newline)
(display "IW R1 * IW R2 = IW R1*R2: ")
(== (interval-width R1*R2)
(* (interval-width R1)
(interval-width R2)
)
)
(newline)
(display "IW R1 / IW R2 = IW R1/R2: ")
(== (interval-width R1/R2)
(/ (interval-width R1)
(interval-width R2)
)
)