-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.7-2.9.scm
54 lines (47 loc) · 1.86 KB
/
2.7-2.9.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
(define (make-interval a b) (cons a b))
(define (upper-bound interval) (cdr interval))
(define (lower-bound interval) (car interval))
(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 (zero? (upper-bound y)) (zero? (lower-bound y)))
(display "Error: second interval has zero value\n")
(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) (upper-bound y))
;; (- (upper-bound x) (lower-bound x))))
(define (sub-interval x y)
(add-interval x (make-interval (- (upper-bound y)) (- (lower-bound y)))))
(define (width-interval x)
(/ (- (upper-bound x) (lower-bound x)) 2))
(define (compare-width x y operation)
(let ((width-x (width-interval x))
(width-y (width-interval y))
(width-result (width-interval (operation x y))))
(cond ((equal? operation add-interval)
(display (+ width-x width-y))
(display "=?")
(display width-result)
(newline))
((equal? operation sub-interval)
(display (abs (- width-x width-y)))
(display "=?")
(display width-result)
(newline))
((equal? operation mul-interval)
(display (* width-x width-y))
(display "=?")
(display width-result)
(newline))
(else (display "fail")))
))