ocaml: phase 5.1 simpson_int.ml baseline (Simpson 1/3 rule, integral_0^1 x^2 -> 10000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

Composite Simpson's 1/3 rule with 100 panels:

  let simpson f a b n =
    let h = (b -. a) /. float_of_int n in
    let sum = ref (f a +. f b) in
    for i = 1 to n - 1 do
      let x = a +. float_of_int i *. h in
      let coef = if i mod 2 = 0 then 2.0 else 4.0 in
      sum := !sum +. coef *. f x
    done;
    h *. !sum /. 3.0

The 1-4-2-4-...-4-1 coefficient pattern is implemented via even/odd
index dispatch. Endpoints get coefficient 1.

For x^2 over [0, 1], exact value is 1/3 ~= 0.33333. Scaled by 30000
gives 9999.99..., int_of_float -> 10000.

Tests higher-order function (passing the integrand 'fun x -> x *. x'),
float arithmetic in for-loop, and float_of_int for index->x conversion.

89 baseline programs total.
This commit is contained in:
2026-05-09 18:09:33 +00:00
parent e8a0c86de0
commit eb621240d7
3 changed files with 21 additions and 0 deletions

View File

@@ -75,6 +75,7 @@
"run_length.ml": 11,
"safe_div.ml": 20,
"shuffle.ml": 55,
"simpson_int.ml": 10000,
"stable_unique.ml": 46,
"subset_sum.ml": 8,
"tic_tac_toe.ml": 1,

View File

@@ -0,0 +1,13 @@
let simpson f a b n =
let h = (b -. a) /. float_of_int n in
let sum = ref (f a +. f b) in
for i = 1 to n - 1 do
let x = a +. float_of_int i *. h in
let coef = if i mod 2 = 0 then 2.0 else 4.0 in
sum := !sum +. coef *. f x
done;
h *. !sum /. 3.0
;;
int_of_float (simpson (fun x -> x *. x) 0.0 1.0 100 *. 30000.0)

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 — simpson_int.ml baseline (Simpson's rule
numerical integration, ∫₀¹ x² dx ≈ 1/3, scaled = 10000). Composite
Simpson's 1/3 rule with 100 panels. Coefficients 1-4-2-...-2-4-1
via even/odd index dispatch. Result × 30000 = 9999.99... → int =
10000 (rounding artifact). Tests higher-order function (passing
the integrand `(fun x -> x *. x)`), float arithmetic in for-loop,
and float_of_int for index→x conversion. 89 baseline programs total.
- 2026-05-09 Phase 5.1 — int_sqrt.ml baseline (integer Newton sqrt,
12+14+1000+1 = 1027). Newton's method on integers using `(x +
n/x) / 2` until convergence (`y >= x`). Tests: