乐正

Actions speak louder than words.

Sicp-ex2-10

问题

Ben Bitdiddle是个专业程序员,他看了Alyssa工作后评论说,除以一个跨过横跨0的区间的 意义不清楚。请修改Alyssa的代码,检查这种情况并在出现这一情况时报错。

解答

1
2
3
4
5
6
7
8
(define (div-interval x y)
  (if (or (<= (lower-bound y) 0)
    (>= (upper-bound y) 0))
      (display "Can not include 0")
      (mul-interval x
        (make-interval (/ 1.0 (upper-bound y))
           (/ 1.0 (lower-bound y))))))
;Value: div-interval

测试

1
2
3
4
(div-interval (make-interval 3 4)
        (make-interval -1 3))
Can not include 0
;Unspecified return value

draft

« sicp-ex2-9 sicp-ex2-11 »

Comments