sicp每日一题[2.10]
Exercise 2.10
Ben Bitdiddle, an expert systems programmer, looks over Alyssa’s shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa's code to check for this condition and to signal an error if it occurs.
居然把第2.10题漏了,刚才准备发2.12才发现。。
这道题目也很简单,只要加一个区间内是否包含0的判断就行了
(define (div-interval x y)
(if (and (<= (lower-bound y) 0) (>= (upper-bound x) 0))
(error "被除区间包含0!")
(mul-interval
x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y))))))
(define r1 (make-interval 6.12 7.48))
(define r2 (make-interval 4.465 4.935))
(display-interval (div-interval r1 r2))
(newline)
(define a (make-interval 6.12 7.48))
(define b (make-interval -4.465 4.935))
(display-interval (div-interval a b))
执行结果