【问题标题】:Code works in stk-simply but not in mit-scheme代码在 stk-simply 中有效,但在 mit-scheme 中无效
【发布时间】:2013-05-24 20:55:33
【问题描述】:

我一直在阅读课程CS61A (Spring 2011) 来自Berkeley OpencoursewareMIT 6.001 来自OCW。一种使用STk(称为stk-simply),另一种使用mit-scheme作为讲座的编程语言。

我刚刚使用Heron's method 编写了一个简单的平方根程序。该文件保存为 sqrt.scm

;; setting an accuracy or tolerance to deviation between the actual and the expected values
(define tolerance 0.001)

;; gives average of two numbers
(define (average x y)
  (/ (+ x y) 2))

;; gives the absolute values of any number
(define (abs x)
  (if (< x 0) (- x) x))

;; gives the square of a number
(define (square x) (* x x))

;; tests whether the guess is good enough by checking if the difference between square of the guess
;; and the number is tolerable
(define (good-enuf? guess x)
  (< (abs (- (square guess) x)) tolerance))

;; improves the guess by averaging guess the number divided by the guess
(define (improve guess x)
  (average (/ x guess) guess))

;;  when a tried guess does not pass the good-enuf test then the tries the improved guess
(define (try guess x)
  (if (good-enuf? guess x)
      guess
      (try (improve guess x) x)))

;; gives back square root of number by starting guess with 1 and then improving the guess until good-enuf
(define (sqr-root x)
  (try 1 x))

这在 STk 中运行良好。

sam@Panzer:~/code/src/scheme$ sudo stk-simply
[sudo] password for sam: 
Welcome to the STk interpreter version 4.0.1-ucb1.3.6 [Linux-2.6.16-1.2108_FC4-i686]
Copyright (c) 1993-1999 Erick Gallesio - I3S - CNRS / ESSI <eg@unice.fr>
Modifications by UCB EECS Instructional Support Group
Questions, comments, or bug reports to <inst@EECS.Berkeley.EDU>.
STk> (load "sqrt.scm")
okay
STk> (sqr-root 25)
5.00002317825395
STk>

但不在计划中。

sam@Panzer:~/code/src/scheme$ sudo scheme
[sudo] password for sam: 
MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Thursday October 27, 2011 at 7:44:21 PM
  Release 9.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/i386 4.118 || Edwin 3.116

1 ]=> (load "sqrt.scm")

;Loading "sqrt.scm"... done
;Value: sqr-root

1 ]=> (sqr-root 25)

;Value: 1853024483819137/370603178776909

1 ]=> 

我查看了手册,但找不到原因。谁能让我知道这是为什么?还是我忽略的代码中有错误?我是方案和 STk 的初学者。

【问题讨论】:

  • 1853024483819137/370603178776909 与 5.00002317825395 的值大致相同。只是其中一个是精确数(分数),另一个是不精确数(小数)。
  • 也许您可以重申您的问题:“我知道 2.5 与 5/2 相同,但 5.00002317825395 与 1853024483819137/370603178776909 相同?有人知道如何在 Scheme 中将大分数转换为浮点数吗? "
  • 只是出于好奇,你为什么要用sudo开始stk-simplyscheme
  • 代码在这两种情况下都有效,但 STK 没有精确的分数。麻省理工学院让你有机会在 STK 中得到错误答案时,让你的计算不基于近似值。

标签: scheme square-root mit-scheme stk


【解决方案1】:

试试这个,现在代码应该在两个解释器中返回相同的结果(忽略微小的舍入差异):

(define (sqr-root x)
  (exact->inexact (try 1 x)))

答案一直都是一样的,只是 MIT Scheme 默认产生了一个精确的结果,而 STk 返回了一个不精确的值。使用上面的代码,我们在执行计算后将结果转换为不精确的数字。或者,我们可以从头开始执行转换(可能会在此过程中丢失一些精度):

(define (sqr-root x)
  (try 1 (exact->inexact x)))

quote 解释了观察到的行为:

方案编号要么准确,要么不准确。如果一个数字被写成一个精确的常数,或者只使用精确的运算从精确的数字推导出来,那么它就是精确的。如果一个数字被写成一个不精确的常数,如果它是使用不精确的成分导出的,或者它是使用不精确的操作导出的,那么它就是不精确的。因此,不精确是数字的一种传染性。

【讨论】:

【解决方案2】:

如果您在 MIT Scheme 中编写了两个答案,那么您的两个答案看起来都一样:

(sqr-root 25.)

因为“不精确”是“粘性”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 2013-01-16
    • 2011-12-17
    • 2019-04-28
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多