sicp每日一题[2.12]
Exercise 2.12
Defineaconstructor m a k e − c e n t e r − p e r c e n t make-center-percent make−center−percent that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector p e r c e n t percent percent that produces the percentage tolerance for a given interval. The c e n t e r center 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%