From e42ff3b1f6d512057c0cc9f4c0f4641364c6b795 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 07:09:29 +0000 Subject: [PATCH] ocaml: phase 6 Float module fleshed out (+6 tests, 598 total) New Float members: zero / one / minus_one abs / neg add / sub / mul / div (lift host '+.' '-.' '*.' '/.') max / min (if-based) equal / compare (Float.compare returns -1 / 0 / 1) to_int (host floor) of_int (identity in dynamic runtime) of_string (delegates to _int_of_string) Aligns Float with Int's API and lets baselines use Float.add / Float.compare / etc without lifting the symbols themselves. Float.add 3.5 4.5 = 8 Float.compare 2.5 5.0 = -1 Float.abs -3.7 = 3.7 Float.max 3.14 2.71 = 3.14 --- lib/ocaml/runtime.sx | 17 +++++++++++++++++ lib/ocaml/test.sh | 22 ++++++++++++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 45 insertions(+) diff --git a/lib/ocaml/runtime.sx b/lib/ocaml/runtime.sx index 132d7586..6f233907 100644 --- a/lib/ocaml/runtime.sx +++ b/lib/ocaml/runtime.sx @@ -630,6 +630,7 @@ module Float = struct let to_string f = _string_of_float f + let of_string s = _int_of_string s let sqrt f = _float_sqrt f let sin f = _float_sin f let cos f = _float_cos f @@ -638,6 +639,22 @@ let ceil f = _float_ceil f let round f = _float_round f let pi = 3.141592653589793 + let zero = 0.0 + let one = 1.0 + let minus_one = -1.0 + let abs f = if f < 0.0 then 0.0 -. f else f + let neg f = 0.0 -. f + let add a b = a +. b + let sub a b = a -. b + let mul a b = a *. b + let div a b = a /. b + let max a b = if a > b then a else b + let min a b = if a < b then a else b + let equal a b = a = b + let compare a b = + if a < b then -1 else if a > b then 1 else 0 + let to_int f = _float_floor f + let of_int n = n end ;; module Printf = struct diff --git a/lib/ocaml/test.sh b/lib/ocaml/test.sh index 1a2b5041..02fcfcc5 100755 --- a/lib/ocaml/test.sh +++ b/lib/ocaml/test.sh @@ -1480,6 +1480,20 @@ cat > "$TMPFILE" << 'EPOCHS' (epoch 5232) (eval "(ocaml-run \"Result.compare compare compare (Ok 1) (Error \\\"fail\\\")\")") +;; ── Float module additions ─────────────────────────────────── +(epoch 5240) +(eval "(ocaml-run \"Float.add 3.5 4.5\")") +(epoch 5241) +(eval "(ocaml-run \"Float.compare 2.5 5.0\")") +(epoch 5242) +(eval "(ocaml-run \"Float.abs (-3.7)\")") +(epoch 5243) +(eval "(ocaml-run \"Float.max 3.14 2.71\")") +(epoch 5244) +(eval "(ocaml-run \"Float.equal 1.5 1.5\")") +(epoch 5245) +(eval "(ocaml-run \"Float.zero +. Float.one\")") + EPOCHS OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null) @@ -2354,6 +2368,14 @@ check 5230 "Result.equal Ok 1 Ok 1" 'true' check 5231 "Result.compare Ok 5 Ok 3" '1' check 5232 "Result.compare Ok < Error" '-1' +# ── Float module additions ────────────────────────────────────── +check 5240 "Float.add 3.5 4.5" '8' +check 5241 "Float.compare 2.5 5.0" '-1' +check 5242 "Float.abs -3.7" '3.7' +check 5243 "Float.max 3.14 2.71" '3.14' +check 5244 "Float.equal 1.5 1.5" 'true' +check 5245 "Float.zero +. Float.one" '1' + TOTAL=$((PASS + FAIL)) if [ $FAIL -eq 0 ]; then echo "ok $PASS/$TOTAL OCaml-on-SX tests passed" diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 262543f8..da8230e0 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _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 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, + of_string. Most just lift the host operators (`+.` is already + available as a global). Aligns Float with Int module's API and + unblocks idiomatic float arithmetic in baselines. - 2026-05-09 Phase 5.1 — balance.ml baseline (paren/bracket/brace balance using Stack). is_balanced walks a string; on opener push, on closer check stack non-empty + top matches expected opener (else