diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 47733ffd..9e36ca4a 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -23,6 +23,7 @@ "module_use.ml": 3, "mutable_record.ml": 10, "option_match.ml": 5, + "pi_leibniz.ml": 314, "poly_stack.ml": 5, "queens.ml": 2, "quicksort.ml": 44, diff --git a/lib/ocaml/baseline/pi_leibniz.ml b/lib/ocaml/baseline/pi_leibniz.ml new file mode 100644 index 00000000..343acfe0 --- /dev/null +++ b/lib/ocaml/baseline/pi_leibniz.ml @@ -0,0 +1,10 @@ +let pi_approx n = + let total = ref 0.0 in + for k = 0 to n - 1 do + let sign = if k mod 2 = 0 then 1.0 else -1.0 in + total := !total +. sign /. float_of_int (2 * k + 1) + done; + 4.0 *. !total +;; + +int_of_float (pi_approx 1000 *. 100.0) diff --git a/lib/ocaml/runtime.sx b/lib/ocaml/runtime.sx index 6f233907..d734a29d 100644 --- a/lib/ocaml/runtime.sx +++ b/lib/ocaml/runtime.sx @@ -1180,7 +1180,8 @@ let min_int = -4611686018427387904 let abs_float f = if f < 0.0 then 0.0 -. f else f let float_of_int n = n - let int_of_float f = f + let int_of_float f = + if f < 0.0 then _float_ceil f else _float_floor f ") (define ocaml-stdlib-loaded false) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index da8230e0..5c8df858 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _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 — pi_leibniz.ml baseline (Leibniz formula, + 1000 terms × 100 → 314). Side-quest: `int_of_float` was wrong — + defined as identity in iteration 94 instead of truncation. Fixed + to `if f < 0.0 then ceil else floor` (truncate toward zero, real + OCaml semantics). Float.to_int still uses floor since OCaml's + documentation says "result is unspecified if the argument is nan + or falls outside the int range" — close enough for our scope. 32 + baseline programs total. - 2026-05-09 Phase 6 — Float module fleshed out (+6 tests, 598 total). New Float members: zero, one, minus_one, abs, neg, add, sub, mul, div, max, min, equal, compare, to_int, of_int,