-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex2-9.scm
76 lines (56 loc) · 1.68 KB
/
ex2-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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#lang scheme
(define (make-interval a b)
(cons a b))
(define (upper-bound i)
(car i))
(define (lower-bound i)
(cdr i))
(define (add-interval x y)
(make-interval
(+ (upper-bound x) (upper-bound y))
(+ (lower-bound x) (lower-bound y))))
(define (subtract-interval x y)
(make-interval
(- (upper-bound x) (upper-bound y))
(- (lower-bound x) (lower-bound y))))
(define (mul-interval x y)
(let (
(p1 (* (upper-bound x) (lower-bound y)))
(p2 (* (upper-bound x) (upper-bound y)))
(p3 (* (lower-bound x) (lower-bound y)))
(p4 (* (lower-bound x) (upper-bound y))))
(make-interval
(max p1 p2 p3 p4)
(min p1 p2 p3 p4))))
(define (divide-interval x y)
(mul-interval x
(make-interval
(/ 1 (upper-bound y))
(/ 1 (lower-bound y)))))
;(mul-interval (make-interval 10 12) (make-interval 2 3))
(define (find-width x)
(/ (- (upper-bound x)
(lower-bound x))
2))
(define (check-addition-equivalance)
(lambda (x y)
(= (+ (find-width x)
(find-width y))
(find-width (add-interval x y)))))
(define (check-subtraction-equivalance)
(lambda (x y)
(= (- (find-width x)
(find-width y))
(find-width (subtract-interval x y)))))
; False
(define (check-mul-equivalance)
(lambda (x y)
(= (* (find-width x)
(find-width y))
(find-width (mul-interval x y)))))
; False
(define (check-division-equivalance)
(lambda (x y)
(= (/ (find-width x)
(find-width y))
(find-width (divide-interval x y)))))