diff --git a/lib/ocaml/baseline/bisect.ml b/lib/ocaml/baseline/bisect.ml new file mode 100644 index 00000000..e7e5b9e6 --- /dev/null +++ b/lib/ocaml/baseline/bisect.ml @@ -0,0 +1,13 @@ +let bisect f lo hi = + let lo = ref lo and hi = ref hi in + for _ = 1 to 50 do + let mid = (!lo +. !hi) /. 2.0 in + if f mid = 0.0 || f !lo *. f mid < 0.0 then hi := mid + else lo := mid + done; + !lo + +;; + +let r = bisect (fun x -> x *. x -. 2.0) 1.0 2.0 in +int_of_float (r *. 100.0) diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 3678a818..9788d392 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -5,6 +5,7 @@ "atm.ml": 120, "bag.ml": 3, "bf_full.ml": 6, + "bisect.ml": 141, "bigint_add.ml": 28, "bits.ml": 21, "balance.ml": 3, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index bad1dfe8..acf3e929 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — bisect.ml baseline (root-finding via + bisection, sqrt(2) * 100 = 141). 50 iterations of bisection + searching for x^2 - 2 = 0 in [1, 2]. Tests higher-order function + passing (the function-to-zero is `(fun x -> x *. x -. 2.0)`), + multi-let `let lo = ref ... and hi = ref ...`, float arithmetic, + and the int_of_float truncate-toward-zero from iteration 117. 62 + baseline programs total. - 2026-05-09 Phase 5.1 — base_n.ml baseline (int -> base-N string, length sum 2+11+3+1 = 17). 36-character digit alphabet supports up to base 36. Loop divides quotient by base, prepends digit. Tests: