On this page:
fixed-point
almost-equal?
tolerance

4 Utilities

(fixed-point f [#:same-test same?])  procedure?
  f : procedure?
  same? : (any/c any/c -> boolean?) = almost-equal?
Returns a function that starting with given arguments, applies f repeatedly until the result no longer changes. The function f of arity n must produce n values.

This function is used for repetitive rewriting.

Example:

> ((fixed-point cos) 1.0)

0.7390851332151603

Implementation of the secant method for solving algebraic equations f(x) = 0 numerically.
(define (secant f)
  (fixed-point (λ(x y)
                 (let ([fx (f x)] [fy (f y)])
                   (values y (/ (- (* x fy) (* y fx))
                                (- fy fx)))))))

 

> ((secant (λ(x)(- (* x x) 20))) 1.0 2.0)

4.47213595499958

4.472135954999579

Values returned by this function show the last two approximations to the solution of the equation x2 - 20 = 0.
> (sqrt 20)

4.47213595499958

(almost-equal? v1 v2)  boolean?
  v1 : any/c
  v2 : any/c
Returns #t either if (equal? v1 v2) yields #t, or if both v1 and v2 are inexact numbers, and they are equal with relative precision, given by the (tolerance) parameter.

Examples:

> (almost-equal? 1 (+ 1 1e-16))

#t

> (equal? 1 (+ 1 1e-16))

#f

A parameter which sets the tolerance for the almost-equal? predicate. The default value is 5e-16.

Example:

> (parameterize ([tolerance 0.001])
    ((secant (λ(x)(- (* x x) 20))) 1.0 2.0))

4.471719160741481

4.47213508926443

With tolerance set to 0.001 iteration stop when only 3 significant digits of consequent approximations coinside.