Interval that spans zero

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.

There are no comments yet.

Authentication required

You must log in to post a comment.

(define (make-interval a b)
  (cons a b))

(define (lower-bound interval)
  (car interval))

(define (upper-bound interval)
  (cdr interval))

(define (mul-interval x y)
  (let ((p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y))))
    (make-interval (min p1 p2 p3 p4)
                   (max p1 p2 p3 p4))))

(define x (make-interval 5.0 15.0))
(define y (make-interval -5.0 0.0))
(define z (make-interval 0.0 5.0))

(define (attempt a b)
  (with-handlers ([exn:fail?
                    (lambda (e) (exn->string e))])
     (div-interval a b)))

(define error-message "division by zero\n")

(check-equal? (attempt x y) error-message)
(check-equal? (attempt x z) error-message)
(check-equal? (attempt x (make-interval 1 1)) x)