Question

I have been reading from the courses CS61A (Spring 2011) from Berkeley Opencourseware and MIT 6.001 from OCW. One uses STk (called as stk-simply) and the other uses mit-scheme as programming language for the lectures.

I just wrote a simple square root procedure using Heron's method. The file was saved as 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))

This works fine in 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>

But not in scheme.

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 ]=> 

I checked the manuals but I was unable to find the cause. Can anyone let me know why is this? Or is there an error in the code which I am ignoring?. I am a beginner in both scheme & STk.

Was it helpful?

Solution

Try this, now the code should return the same results in both interpreters (ignoring tiny rounding differences):

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

The answer was the same all along, it's just that MIT Scheme is producing an exact result by default, whereas STk is returning an inexact value. With the above code in place, we convert the result to an inexact number after performing the calculation. Alternatively, we could perform the conversion from the beginning (possibly losing some precision in the process):

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

This quote explains the observed behavior:

Scheme numbers are either exact or inexact. A number is exact if it was written as an exact constant or was derived from exact numbers using only exact operations. A number is inexact if it was written as an inexact constant, if it was derived using inexact ingredients, or if it was derived using inexact operations. Thus inexactness is a contagious property of a number.

OTHER TIPS

You'd have both answers look the same if in MIT Scheme you had written:

(sqr-root 25.)

since 'inexactness' is 'sticky'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top