e1.08.scm 835 B

1234567891011121314151617181920212223242526272829
  1. ; use newtons better approximation formula
  2. ; (x / y^2 + 2y) / 3
  3. (define (sqrt-iter prev-guess guess x)
  4. ; util procs
  5. (define (square x) (* x x))
  6. ; ??? somehow throws up invalid values
  7. (define (approx g)
  8. (/ (+ (/ x (square g))
  9. (* 2 g))
  10. 3))
  11. (define (delta prev-guess guess)
  12. (abs (- prev-guess guess)))
  13. ; body
  14. (if (< (delta prev-guess guess) (* prev-guess 0.0000001))
  15. guess
  16. (sqrt-iter guess (approx guess) x)))
  17. (define (sqrt x) (sqrt-iter 0. 1. x))
  18. 3 (sqrt 3)
  19. 4 (sqrt 4)
  20. 9 (sqrt 9)
  21. 100 (sqrt 100)
  22. 400000000 (sqrt 400000000)
  23. 0.1 (sqrt 0.1)
  24. 0.0000001 (sqrt 0.0000001)
  25. 123456789123456789 (sqrt 123456789123456789)
  26. .98765432198765432 (sqrt .98765432198765432)