Blame


1 665c255d 2023-08-04 jrmu (define (add-interval x y)
2 665c255d 2023-08-04 jrmu (make-interval (+ (lower-bound x) (lower-bound y))
3 665c255d 2023-08-04 jrmu (+ (upper-bound x) (upper-bound y))))
4 665c255d 2023-08-04 jrmu (define (mul-interval x y)
5 665c255d 2023-08-04 jrmu (let ((p1 (* (lower-bound x) (lower-bound y)))
6 665c255d 2023-08-04 jrmu (p2 (* (lower-bound x) (upper-bound y)))
7 665c255d 2023-08-04 jrmu (p3 (* (upper-bound x) (lower-bound y)))
8 665c255d 2023-08-04 jrmu (p4 (* (upper-bound x) (upper-bound y))))
9 665c255d 2023-08-04 jrmu (make-interval (min p1 p2 p3 p4)
10 665c255d 2023-08-04 jrmu (max p1 p2 p3 p4))))
11 665c255d 2023-08-04 jrmu
12 665c255d 2023-08-04 jrmu (define (div-interval x y)
13 665c255d 2023-08-04 jrmu (mul-interval x
14 665c255d 2023-08-04 jrmu (make-interval (/ 1.0 (upper-bound y))
15 665c255d 2023-08-04 jrmu (/ 1.0 (lower-bound y)))))
16 665c255d 2023-08-04 jrmu
17 665c255d 2023-08-04 jrmu (define (make-interval lower upper)
18 665c255d 2023-08-04 jrmu (cons lower upper))
19 665c255d 2023-08-04 jrmu (define (upper-bound interval)
20 665c255d 2023-08-04 jrmu (cdr interval))
21 665c255d 2023-08-04 jrmu (define (lower-bound interval)
22 665c255d 2023-08-04 jrmu (car interval))
23 665c255d 2023-08-04 jrmu
24 665c255d 2023-08-04 jrmu (define (sub-interval x y)
25 665c255d 2023-08-04 jrmu (make-interval (- (lower-bound x) (upper-bound y))
26 665c255d 2023-08-04 jrmu (- (upper-bound x) (lower-bound y))))
27 665c255d 2023-08-04 jrmu
28 665c255d 2023-08-04 jrmu ;; Exercise 2.9. The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainty of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (or difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division.
29 665c255d 2023-08-04 jrmu
30 665c255d 2023-08-04 jrmu ;; width of addition/subtraction is just the sum of the two widths
31 665c255d 2023-08-04 jrmu
32 665c255d 2023-08-04 jrmu (define i1 (make-interval -3 4))
33 665c255d 2023-08-04 jrmu (define i2 (make-interval -7 -3))
34 665c255d 2023-08-04 jrmu
35 665c255d 2023-08-04 jrmu ;; (width (mul-interval i1 i2)) is equal to 33/2; the two original widths were 7/2 and 4/2
36 665c255d 2023-08-04 jrmu
37 665c255d 2023-08-04 jrmu (define i3 (make-interval 3 4))
38 665c255d 2023-08-04 jrmu (define i4 (make-interval 15 20))
39 665c255d 2023-08-04 jrmu
40 665c255d 2023-08-04 jrmu ;; (width (mul-interval i3 i4)) is equal to 35/2; the two original widths were 1/2 and 5/2
41 665c255d 2023-08-04 jrmu
42 665c255d 2023-08-04 jrmu ;; even though the widths of i1 and i2 were originally greater, their resulting product's width is smaller than i3 and i4's product's width
43 665c255d 2023-08-04 jrmu