ocaml: phase 5.1 pi_leibniz.ml baseline + int_of_float fix (1000 terms x 100 = 314)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

pi_leibniz.ml: Leibniz formula for pi.

  pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...
  pi  ~= 4 * sum_{k=0}^{n-1} (-1)^k / (2k+1)

For n=1000, pi ~= 3.140593. Multiply by 100 and int_of_float -> 314.

Side-quest: int_of_float was wrongly defined as identity in
iteration 94. Fixed to:

  let int_of_float f =
    if f < 0.0 then _float_ceil f else _float_floor f

(truncate toward zero, mirroring real OCaml's int_of_float). The
identity definition was a stub from when integer/float dispatch was
not yet split — now they're separate, the stub is wrong.

Float.to_int still uses floor since OCaml's docs say the result is
unspecified for nan / out-of-range; close enough for our scope.

32 baseline programs total.
This commit is contained in:
2026-05-09 07:19:52 +00:00
parent e42ff3b1f6
commit 90418c120b
4 changed files with 21 additions and 1 deletions

View File

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

View File

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

View File

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