ocaml: phase 5.1 bisect.ml baseline (root-finding, sqrt(2)*100 = 141)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s

Bisection method searching for f(x) = 0 in [lo, hi] over 50
iterations:

  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

Solving x^2 - 2 = 0 in [1, 2] via 'bisect (fun x -> x *. x -. 2.0)
1.0 2.0' converges to ~1.41421356... -> int_of_float (r *. 100) =
141.

Tests:
  - higher-order function passing
  - multi-let 'let lo = ref ... and hi = ref ...'
  - float arithmetic
  - int_of_float truncate-toward-zero (iter 117)

62 baseline programs total.
This commit is contained in:
2026-05-09 13:02:17 +00:00
parent 05487b497d
commit 50981a2a9b
3 changed files with 21 additions and 0 deletions

View File

@@ -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)

View File

@@ -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,

View File

@@ -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: