-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_2_010.scm
81 lines (68 loc) · 1.69 KB
/
ex_2_010.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
(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)
(if
(or (= (upper-bound y) 0)
(= (lower-bound y) 0))
((display "Division by 0!"))
(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 0 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)