<< Previous exercise (2.11) | Index | Next exercise (2.13) >>

Do you think we need to abs the width as well? (this uses proportion instead of percent)

; e.g. for (make-center-percent -10 0.05) (define (make-center-percent c p) (make-interval (- c (abs (* c p))) (+ c (abs (* c p)))))

I think make-center-width, center, width can be used here to make it concise.

(define (make-center-percent c p) (make-center-width c (* c p))) (define (percent i) (/ (width i) (center i)))

;We are talking about percentages rather than fractions. Therefore (define (make-center-percent c p) (make-interval (- c (abs (* c (/ p 100)))) (+ c (abs (* c (/ p 100)))))) ;Also, for the percentage selector (define (percent i) (* 100 (/ (width i) (center i))))

Two points about calculating the percentage with

(/ (width i) (center i))

- Because of division by 0, the percentage is undefined if center happens to be 0. However, given a center of 0, make-center-percent produces a 0-width interval. If this is a valid result, the percent selector should also produce a valid result given a center and width of 0. (It's fine for other widths to be undefined, though.)

- The ratio of width and center can be found without calculating width and center:

width / center = (2 * width) / (2 * center) = (upper-bound - lower-bound) / (upper-bound + lower-bound)

As dxdm mentions, if we have a center of zero, percentage in terms of a center which is zero is not well defined. Logically it is always zero, so I've defined a solution which lets the percentage be zero where the center is zero. Also, I don't use `width` as a procedure since they mentioned we'd only get to keep `center` as it appears in the previous definition:

(define (make-center-percent c p) (let ((width (abs (* (/ p 100) c)))) (make-interval (- c width) (+ c width)))) (define (percent i) (if (= (center i) 0) 0 (* 100 (abs (/ (- (upper-bound i) (center i)) (center i))))))

There are a few ways to do this problem, e.g.: