# SICP Exercise 1.07

Code

```
#lang racket
(require "common.scm")
; first variant
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 1e-3))
(define (mysqrt x) (sqrt-iter 1.0 x))
(define (test radicand)
(define calculated (mysqrt radicand))
(define exact (sqrt radicand))
(display "radicand: ")
(display radicand)
(newline)
(display "sqrt exact: ")
(display exact)
(newline)
(display "sqrt calculated: ")
(display calculated)
(newline))
; tests
(test 1e-80)
;(test 1e+80)
; process doesn't stop
```

Output

```
radicand: 1e-80
sqrt exact: 1e-40
sqrt calculated: 0.03125
```

The process stops when absolute error is small. For very small numbers the square of a small guess becomes less than 0.001 and the process stops too early, giving the wrong answer. For very large numbers and guesses the difference between two large numbers is always larger than 0.001, exactly due to limited precision of decimal values, and the guess is infinitely improved.

Code

```
#lang racket
(require "common.scm")
; second variant
(define (sqrt-iter oldguess guess x)
(if (good-enough? oldguess guess)
guess
(sqrt-iter guess (improve guess x) x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? oldguess guess)
(< (/ (abs (- oldguess guess)) guess) 1e-10))
(define (mysqrt x) (sqrt-iter 2.0 1.0 x))
(define (test radicand)
(define calculated (mysqrt radicand))
(define exact (sqrt radicand))
(display "radicand: ")
(display radicand)
(newline)
(display "sqrt exact: ")
(display exact)
(newline)
(display "sqrt calculated: ")
(display calculated)
(newline))
; tests
(test 1e-80)
(test 1e+80)
```

Output

```
radicand: 1e-80
sqrt exact: 1e-40
sqrt calculated: 1e-40
radicand: 1e+80
sqrt exact: 1e+40
sqrt calculated: 9.999999999999999e+39
```

The process stops when relative error is small.