Alyssa P. Hacker is designing a system to help people solve engineering
problems. One feature she wants to provide in her system is the ability to
manipulate inexact quantities (such as measured parameters of physical devices)
with known precision, so that when computations are done with such approximate
quantities the results will be numbers of known precision.
Alyssa P. Hacker 正在设计一个帮助人们解决工程问题的系统。她希望在系统中提供的一项功能是:能够以已知的精度处理不精确的量(例如物理器件测量参数),从而使得用这些近似量进行计算时,结果也是精度已知的数。
Electrical engineers will be using Alyssa’s system to compute electrical
quantities. It is sometimes necessary for them to compute the value of a
parallel equivalent resistance R
p of two resistors R
1 and R
2
using the formula
R
p
电气工程师将使用 Alyssa 的系统来计算电气量。有时他们需要用如下公式计算两个电阻 R₁ 和 R₂ 并联的等效电阻 Rₚ
Rₚ
=
1
1
/
R
1
+
1
/
R
2
.
Resistance values are usually known only up to some tolerance guaranteed by the
manufacturer of the resistor. For example, if you buy a resistor labeled “6.8
ohms with 10% tolerance” you can only be sure that the resistor has a
resistance between 6.8 − 0.68 = 6.12 and 6.8 + 0.68 = 7.48 ohms. Thus, if you
have a 6.8-ohm 10% resistor in parallel with a 4.7-ohm 5% resistor, the
resistance of the combination can range from about 2.58 ohms (if the two
resistors are at the lower bounds) to about 2.97 ohms (if the two resistors are
at the upper bounds).
。
电阻值通常只能精确到制造商所保证的某个容差范围内。例如,若你购买了一个标注为"6.8 欧姆、10% 容差"的电阻,你只能确定该电阻的阻值在 6.8 − 0.68 = 6.12 欧姆到 6.8 + 0.68 = 7.48 欧姆之间。因此,若将一个 6.8 欧姆 10% 容差的电阻与一个 4.7 欧姆 5% 容差的电阻并联,组合后的电阻值可能在约 2.58 欧姆(两个电阻均取下界时)到约 2.97 欧姆(两个电阻均取上界时)之间。
Alyssa’s idea is to implement “interval arithmetic” as a set of arithmetic
operations for combining “intervals” (objects that represent the range of
possible values of an inexact quantity). The result of adding, subtracting,
multiplying, or dividing two intervals is itself an interval, representing the
range of the result.
Alyssa 的想法是将"区间算术"实现为一组算术运算,用于对"区间"(表示不精确量的可能取值范围的对象)进行组合运算。两个区间相加、相减、相乘或相除的结果本身也是一个区间,表示运算结果的取值范围。
Alyssa postulates the existence of an abstract object called an “interval”
that has two endpoints: a lower bound and an upper bound. She also presumes
that, given the endpoints of an interval, she can construct the interval using
the data constructor make-interval. Alyssa first writes a procedure for
adding two intervals. She reasons that the minimum value the sum could be is
the sum of the two lower bounds and the maximum value it could be is the sum of
the two upper bounds:
Alyssa 假定存在一种称为"区间"的抽象对象,它有两个端点:下界和上界。她还假定,在给定区间端点的情况下,可以用数据构造函数 make-interval 来构造该区间。Alyssa 首先编写了一个用于两个区间相加的过程。她推断:和的最小可能值是两个下界之和,最大可能值是两个上界之和:
Alyssa also works out the product of two intervals by finding the minimum and
the maximum of the products of the bounds and using them as the bounds of the
resulting interval. (Min and max are primitives that find the
minimum or maximum of any number of arguments.)
Alyssa 还给出了两个区间相乘的方法:计算各端点乘积的最小值和最大值,并以此作为结果区间的端点。(min 和 max 是基本过程,用于求任意个参数中的最小值或最大值。)
To divide two intervals, Alyssa multiplies the first by the reciprocal of the
second. Note that the bounds of the reciprocal interval are the reciprocal of
the upper bound and the reciprocal of the lower bound, in that order.
对于两个区间的除法,Alyssa 将第一个区间乘以第二个区间的倒数。注意,倒数区间的端点是原区间上界的倒数和下界的倒数,顺序如此。
(define (add-interval x y)
(make-interval (+ (lower-bound x)
(lower-bound y))
(+ (upper-bound x)
(upper-bound y)))) (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 (div-interval x y)
(mul-interval x
(make-interval
(/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))