sicp每日一题[2.12]
Exercise 2.12
Defineaconstructor make-center-percent that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector percent that produces the percentage tolerance for a given interval. The center selector is the same as the one shown above.
我以为这是上道题的要求,已经实现了。。
; 把 w 改为百分比形式
(define (make-center-width c w)
(let ((tolerance (* c w)))
(make-interval (- c tolerance) (+ c tolerance))))
(define (center i)
(average (lower-bound i) (upper-bound i)))
(define (percent i)
(* (/ (/ (- (upper-bound i) (lower-bound i)) 2)
(center i))
100))
(define (display-interval-tolerance i)
(newline)
(display (center i))
(display " ± ")
(display (percent i))
(display "%"))
(define a (make-interval 6.12 7.48))
(define b (make-interval 4.465 4.935))
(display-interval-tolerance a)
(display-interval-tolerance b)
; 执行结果
6.800000000000001 ± 10.000000000000002%
4.699999999999999 ± 4.999999999999998%