Commit 84956427 authored by POTTIER Francois's avatar POTTIER Francois

Add an alternate definition of [within] in NewtonRapshon.ml.

parent 09ca1afc
......@@ -147,6 +147,22 @@ let rec within (eps : float) (xs : float stream) : float =
if abs_float (a /. b -. 1.) <= eps then b
else within eps xs
(* OCaml allows forcing a thunk as part of pattern-matching construct,
via the syntax [lazy p] where [p] is a pattern. Thus, [within] can
also be written as follows: *)
let rec within (eps : float) (xs : float stream) : float =
match xs with
| lazy (Cons (a,
((lazy (Cons (b, _))) as xs)
)) ->
if abs_float (a /. b -. 1.) <= eps then b
else within eps xs
| _ ->
assert false
(* We can finally define [sqrt] and check that it seems to work: *)
let sqrt (n : float) : float =
within 0.0001 (repeat (next n) n)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment