目录
正在加载目录…
正在阅读专栏 SICP:Structure and Interpretation of Computer Programs 第 7 篇 · 共 80 篇 · 9%

SICP:1.1.7 Example: Square Roots by Newton's Method

发布于 2026-09-18 08:12 · 最后编辑于 2026-09-18 08:28 · 版本 v2 · 字数 5,167 · 预计阅读 11 分钟 👁 38 次阅读

SICP:1.1.7 Example: Square Roots by Newton's Method

本文逐段翻译 SICP 第二版 §1.1.7《Example: Square Roots by Newton's Method》,保留数学定义、Newton's method、Scheme code、数值表格、REPL 输出、两幅原始 GIF 以及 Exercises 1.6–1.8 的原文顺序。

TL;DR

  • Mathematical function 描述 value 的 property;computer procedure 必须 effective,也就是描述如何实际得到 value。
  • Newton's method 用 successive approximations 计算 square roots:把当前 guess 与 x / guess 求 average,不断改进 guess。
  • sqrt-iter 展示了只依靠 ordinary procedure calls 就能实现 iteration;但 good-enough? 的测试在极小或极大的 numbers 上存在局限。

目录

章节说明
1.1.7 Example: Square Roots by Newton's Method从 mathematical function 到 effective procedure
Newton's methodsuccessive approximations
Square-root proceduresqrt-iter、improve 与 good-enough?
Exercises 1.6–1.8if、精度与 cube roots

1.1.7 Example: Square Roots by Newton's Method

如上所述,procedures 很像 ordinary mathematical functions。它们指定一个由一个或多个 parameters 决定的 value。但 mathematical functions 与 computer procedures 之间存在一个 important difference:procedures 必须 effective。

作为一个例子,考虑计算 square roots 的问题。我们可以把 square-root function 定义为:

SICP square root definition

这描述了一个完全 legitimate 的 mathematical function。我们可以用它判断一个 number 是否是另一个 number 的 square root,也可以推导关于 square roots 的 general facts。另一方面,这个 definition 并没有描述一个 procedure。事实上,它几乎没有告诉我们如何实际找到给定 number 的 square root。把这个 definition 改写成 pseudo-Lisp 也没有帮助:

scheme
(define (sqrt x)
  (the y (and (>= y 0)
              (= (square y) x))))

这只是回避了问题。

Function 与 procedure 之间的 contrast,反映了一个 general distinction:描述事物的 properties 与描述如何做事之间的 distinction;有时也称为 declarative knowledge 与 imperative knowledge 的 distinction。在 mathematics 中,我们通常关注 declarative(what is)descriptions;而在 computer science 中,我们通常关注 imperative(how to)descriptions1。

Newton's method

如何计算 square roots?最常见的方法是使用 Newton's method of successive approximations。它指出:每当我们有一个 number x 的 square root 的 guess y 时,都可以通过一个 simple manipulation 得到一个更好的 guess(更接近 actual square root):将 y 与 x / y 求 average2。

例如,我们可以如下计算 2 的 square root。假设 initial guess 是 1:

GuessQuotientAverage
1(2/1) = 2((2 + 1)/2) = 1.5
1.5(2/1.5) = 1.3333((1.3333 + 1.5)/2) = 1.4167
1.4167(2/1.4167) = 1.4118((1.4167 + 1.4118)/2) = 1.4142
1.4142......

持续这个 process,我们就会得到越来越好的 square-root approximations。

Square-root procedure

现在用 procedures 来 formalize 这个 process。我们从 radicand(要计算其 square root 的 number)和 guess 的 value 开始。如果 guess 对我们的目的来说已经 good enough,就完成;否则,必须使用 improved guess 重复这个 process。我们把这个 basic strategy 写成一个 procedure:

scheme
(define (sqrt-iter guess x)
  (if (good-enough? guess x)
      guess
      (sqrt-iter (improve guess x)
                 x)))

通过将 guess 与 radicand 除以 old guess 所得的 quotient 求 average,可以改进 guess:

scheme
(define (improve guess x)
  (average guess (/ x guess)))

其中:

scheme
(define (average x y)
  (/ (+ x y) 2))

我们还必须说明 “good enough” 的含义。下面的 definition 可以用于 illustration,但它并不是一个 very good test(见 Exercise 1.7)。其 idea 是不断改进 answer,直到它足够接近,使它的 square 与 radicand 的 difference 小于一个 predetermined tolerance(这里是 0.001)3:

scheme
(define (good-enough? guess x)
  (< (abs (- (square guess) x)) 0.001))

最后,我们需要一种启动 procedure 的方式。例如,可以总是假设任何 number 的 square root 是 14:

scheme
(define (sqrt x)
  (sqrt-iter 1.0 x))

把这些 definitions 输入 interpreter 后,就可以像使用任何 procedure 一样使用 sqrt:

scheme
(sqrt 9)
3.00009155413138

(sqrt (+ 100 37))
11.704699917758145

(sqrt (+ (sqrt 2) (sqrt 3)))
1.7739279023207892

(square (sqrt 1000))
1000.000369924366

sqrt program 还展示了:到目前为止介绍的 simple procedural language,已经足以编写任何用例如 C 或 Pascal 编写的 purely numerical program。这也许令人惊讶,因为我们的 language 中还没有包含任何 iterative(looping)construct,来指示 computer 反复执行某件事。然而,sqrt-iter 展示了如何只使用 ordinary ability to call a procedure、而不依赖任何 special construct 来实现 iteration5。

Exercises 1.6–1.8

Exercise 1.6

Alyssa P. Hacker 不明白为什么 if 必须作为 special form 提供。她问:“为什么不能只用 cond 把它定义为 ordinary procedure?”她的朋友 Eva Lu Ator 声称确实可以这样做,于是定义了一个新的 if version:

scheme
(define (new-if predicate then-clause else-clause)
  (cond (predicate then-clause)
        (else else-clause)))

Eva 向 Alyssa 演示这个 program:

scheme
(new-if (= 2 3) 0 5)
5

(new-if (= 1 1) 0 5)
0

Alyssa 很高兴,于是使用 new-if 重写 square-root program:

scheme
(define (sqrt-iter guess x)
  (new-if (good-enough? guess x)
          guess
          (sqrt-iter (improve guess x)
                     x)))

当 Alyssa 尝试用它计算 square roots 时,会发生什么?解释你的 answer。

Exercise 1.7

计算 square roots 时使用的 good-enough? test,在寻找 very small numbers 的 square roots 时不会很 effective。此外,在 real computers 中,arithmetic operations 几乎总是以 limited precision 执行。这会使我们的 test 在 very large numbers 上也 inadequate。解释这些 statements,并给出 examples,展示该 test 如何在 small 和 large numbers 上失败。

实现 good-enough? 的一种 alternative strategy,是观察 guess 从一次 iteration 到下一次 iteration 如何变化,并在 change 仅占 guess 的 very small fraction 时停止。设计一个使用这种 end test 的 square-root procedure。对于 small 和 large numbers,它是否工作得更好?

Exercise 1.8

Newton's method for cube roots 建立在以下事实之上:如果 y 是 x 的 cube root 的 approximation,那么更好的 approximation 是下面这个 value:

SICP cube root formula

使用这个 formula 实现一个类似于 square-root procedure 的 cube-root procedure。(在 §1.3.4 中,我们会看到如何把 Newton's method generalize 成一个 abstraction,用于这些 square-root 和 cube-root procedures。)

图与格式复核

  • Square-root mathematical definition:保留原始 GIF,尺寸为 245 × 16,位置在函数定义段落之后。
  • Cube-root formula:保留原始 GIF,尺寸为 60 × 32,位置在 Exercise 1.8 题干中。
  • Newton's method 的 Guess / Quotient / Average 表格、所有 Scheme code、REPL output 与 Exercises 1.6–1.8 顺序保持不变。

术语对照

English译法/处理
effective保留 effective;必要处译作有效
declarative knowledge保留 declarative knowledge;必要处译作声明式知识
imperative knowledge保留 imperative knowledge;必要处译作命令式知识
successive approximation保留 successive approximation;必要处译作逐次逼近
radicand保留 radicand;必要处译作被开方数
guess / improve保留 guess、improve;必要处译作猜测值、改进
tolerance保留 tolerance;必要处译作容差
iteration保留 iteration;必要处译作迭代
tail recursion保留 tail recursion;必要处译作尾递归

参考资料

  1. Declarative 与 imperative descriptions 紧密相关,mathematics 与 computer science 也是如此。例如,说一个 program 产生的 answer 是“correct”,就是在对该 program 作一个 declarative statement。大量 research 致力于建立证明 programs correct 的 techniques;其中许多 technical difficulties 都与处理 imperative statements(programs 由其构成)到 declarative statements(可用来进行 deductions)的 transition 有关。 ↩

  2. 这个 square-root algorithm 实际上是 Newton's method 的一个 special case;Newton's method 是寻找 equations 的 roots 的 general technique。square-root algorithm 本身由 Alexandria 的 Heron 在公元一世纪发展出来。本书 §1.3.4 将展示如何把 general Newton's method 表达为 Lisp procedure。 ↩

  3. 我们通常会给 predicate 起以 question mark 结尾的 names,以帮助自己记住它们是 predicates。这只是 stylistic convention;对 interpreter 而言,question mark 只是 ordinary character。 ↩

  4. 注意,我们把 initial guess 表达为 1.0 而不是 1。在许多 Lisp implementations 中这没有区别;但 MIT Scheme 区分 exact integers 与 decimal values,两个 integers 相除会得到 rational number 而不是 decimal。使用 1.0 可以使这里的计算产生 decimal approximations。 ↩

  5. 对于使用 procedure calls 实现 iteration 的 efficiency issues,原文提示参见 §1.2.1 关于 tail recursion 的讨论。 ↩

← 返回列表
← 上一篇 SICP:1.1.6 Conditional Expressions and Predicates 下一篇 → SICP:1.1.8 Procedures as Black-Box Abstractions

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论