sx: format_number helper — defuse int_of_float overflow on huge floats

Shared formatter in sx_types.ml. Small integer-valued floats still print
as plain ints; floats outside safe-int range (|n| >= 1e16) now print as
%.17g (full precision) instead of silently wrapping to negative or 0.
Non-integer values keep %g 6-digit behavior — no existing SX tests regress.

Unblocks Number.MAX_VALUE / Math.pow(2,N) style tests in js-on-sx where
iterative float loops were collapsing to 0 at ~2^63.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-24 15:09:11 +00:00
parent 8f202e03c2
commit b45a69b7a4
9 changed files with 400 additions and 127 deletions

View File

@@ -15,9 +15,7 @@ let prim_call name args =
(** Convert any SX value to an OCaml string (internal). *)
let value_to_str = function
| String s -> s
| Number n ->
if Float.is_integer n then string_of_int (int_of_float n)
else Printf.sprintf "%g" n
| Number n -> Sx_types.format_number n
| Bool true -> "true"
| Bool false -> "false"
| Nil -> ""