ocaml: phase 4 integer division semantics + Int module + max_int/min_int (+5 tests, 525 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s

Three things in this commit:

1. Integer / is now truncate-toward-zero on ints, IEEE on floats. The
   eval-op handler for '/' checks (number? + (= (round x) x)) on both
   sides; if both integral, applies host floor/ceil based on sign;
   otherwise falls through to host '/'.

2. Fixes Int.rem, which was returning 0 because (a - b * (a / b))
   was using float division: 17 - 5 * 3.4 = 0.0. Now Int.rem 17 5 = 2.

3. Int module fleshed out:
   max_int / min_int / zero / one / minus_one,
   succ / pred / neg, add / sub / mul / div / rem,
   equal, compare.

Also adds globals: max_int, min_int, abs_float, float_of_int,
int_of_float (the latter two are identity in our dynamic runtime).

  17 / 5         = 3
  -17 / 5        = -3   (trunc toward zero)
  Int.rem 17 5   = 2
  Int.compare 5 3 = 1
This commit is contained in:
2026-05-09 02:50:21 +00:00
parent 55fe1e4468
commit a0e8b64f5c
4 changed files with 55 additions and 1 deletions

View File

@@ -466,6 +466,21 @@
let abs n = if n < 0 then 0 - n else n
let max a b = if a > b then a else b
let min a b = if a < b then a else b
let max_int = 4611686018427387903
let min_int = -4611686018427387904
let zero = 0
let one = 1
let minus_one = -1
let succ n = n + 1
let pred n = n - 1
let neg n = 0 - n
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 rem a b = a - b * (a / b)
let equal a b = a = b
let compare a b = if a < b then -1 else if a > b then 1 else 0
end ;;
module Float = struct
@@ -832,6 +847,11 @@
let string_of_float f = _string_of_float f
let string_of_bool b = if b then \"true\" else \"false\"
let int_of_string s = _int_of_string s
let max_int = 4611686018427387903
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
")
(define ocaml-stdlib-loaded false)