After considerable work, Alyssa P. Hacker delivers her finished system. Several years later, after she has forgotten all about it, she gets a frenzied call from an irate user, Lem E. Tweakit. It seems that Lem has noticed that the formula for parallel resistors can be written in two algebraically equivalent ways:
and
He has written the following two programs, each of which computes the parallel-resistors formula differently:
(define (par1 r1 r2)
(div-interval (mul-interval r1 r2)
(add-interval r1 r2)))
(define (par2 r1 r2)
(let ((one (make-interval 1 1)))
(div-interval one
(add-interval (div-interval one r1)
(div-interval one r2)))))
Lem complains that Alyssa's program gives different answers for the two ways of computing. This is a serious complaint.
Demonstrate that Lem is right. Investigate the behavior of the system on a variety of arithmetic expressions. Make some intervals
Recall
We demonstrate the same holds for
Where
This is confirmed by display-interval
below:
(load "2.interval.scm")
;; (define (div-interval x y)
;; (mul-interval x
;; (make-interval (/ 1.0 (upper-bound y))
;; (/ 1.0 (lower-bound y)))))
(define a (make-center-percent 1000 0.1))
(define b (make-center-percent 2000 0.05))
(define aa (div-interval a a))
(define ab (div-interval a b))
(display-interval aa) ; center: 1., percent: .2
(display-interval ab) ; center: .5, percent: .15
The issue is that div-interval
treats
This is why Lem's procedures produce different answers: par1
takes two draws of r1
and r2
while par2
only uses one draw of r1
and r2
. The latter is correct.