Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created September 18, 2009 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jimweirich/189270 to your computer and use it in GitHub Desktop.
Save jimweirich/189270 to your computer and use it in GitHub Desktop.
;; Exercise 1.8. Newton's method for cube roots is based on the fact
;; that if y is an approximation to the cube root of x, then a better
;; approximation is given by the value
;;
;; Use this formula to implement a cube-root procedure analogous to
;; the square-root procedure. (In section 1.3.4 we will see how to
;; implement Newton's method in general as an abstraction of these
;; square-root and cube-root procedures.)
;; ANSWER ------------------------------------------------------------
(define (cbrt x)
(define (square x) (* x x))
;; New improve formula for cube root.
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess))
3) )
;; New good enough test for cube root. Using the relative epsilon
;; code from ex1.7.
(define (good-enough? guess x)
(< (abs (- guess (/ x (square guess))))
(/ guess 1000000)) )
(define (try guess)
(if (good-enough? guess x)
guess
(try (improve guess x))))
(try 1.0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment