灯下 登录
计算机科学 / SICP / 2.1.4 Extended Exercise: Interval Arithmetic

Exercise 2.11 · 习题

Exercise 2.11: In passing, Ben also cryptically
comments: “By testing the signs of the endpoints of the intervals, it is
possible to break mul-interval into nine cases, only one of which
requires more than two multiplications.” Rewrite this procedure using Ben’s
suggestion.

After debugging her program, Alyssa shows it to a potential user, who complains
that her program solves the wrong problem. He wants a program that can deal
with numbers represented as a center value and an additive tolerance; for
example, he wants to work with intervals such as 3.5 ± 0.15 rather than
[3.35, 3.65]. Alyssa returns to her desk and fixes this problem by supplying
an alternate constructor and alternate selectors:

(define (make-center-width c w)
(make-interval (- c w) (+ c w)))

(define (center i)
(/ (+ (lower-bound i)
(upper-bound i))
2))

(define (width i)
(/ (- (upper-bound i)
(lower-bound i))
2))

Unfortunately, most of Alyssa’s users are engineers. Real engineering

situations usually involve measurements with only a small uncertainty, measured

as the ratio of the width of the interval to the midpoint of the interval.

Engineers usually specify percentage tolerances on the parameters of devices,

as in the resistor specifications given earlier.

练习 2.11:随口一提,Ben 还神秘地说道:“通过检验区间端点的符号,可以将 mul-interval 分解为九种情形,其中只有一种需要超过两次乘法。”按照 Ben 的建议重写这一过程。

调试完程序后,Alyssa 将其展示给一位潜在用户,该用户抱怨她的程序解决了错误的问题。他希望程序能处理以中心值加减偏差形式表示的数,例如,他想处理 3.5 ± 0.15 这样的区间,而不是 [3.35, 3.65]。Alyssa 回到座位上,通过提供一个替代构造函数和替代选择函数解决了这一问题:

(define (make-center-width c w)
(make-interval (- c w) (+ c w)))

(define (center i)
(/ (+ (lower-bound i)
(upper-bound i))
2))

(define (width i)
(/ (- (upper-bound i)
(lower-bound i))
2))

然而,Alyssa 的大多数用户都是工程师。实际工程中通常涉及不确定性较小的测量,以区间宽度相对于区间中点的比值来度量。工程师通常以百分比容差来规定元件参数,如前面给出的电阻规格所示。

Racket #lang sicp
(define (make-center-width c w)
 (make-interval (- c w) (+ c w)))

(define (center i)
 (/ (+ (lower-bound i)
 (upper-bound i))
 2))

(define (width i)
 (/ (- (upper-bound i)
 (lower-bound i))
 2))