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: | ||
|
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: | ||||
|
A parameter which sets the tolerance for the almost-equal? predicate. The default value is 5e-16.
Example: | ||||||
|
With tolerance set to 0.001 iteration stop when only 3 significant digits of consequent approximations coinside.