ReviewOS

also looking at this

home-lang/home

fix(interpreter): keep 2^63 as a float in floor, ceil and round

#932
Open mkantautas wants to merge fix/interpreter-floor-ceil-round-max into main
2 files +22 -3
8 days ago

Problem

floor, ceil and round abort the interpreter when the result is exactly 2^63:

fn main() {
  print(floor(9223372036854775807.0))
}

The literal is the f64 9223372036854775808.0 (2^63). home run stops with panic: integer part of floating point value out of bounds in evaluateCallExpression. These builtins return an Int when the result fits in i64 and a Float otherwise, but the upper check is result <= @as(f64, @floatFromInt(std.math.maxInt(i64))), and maxInt(i64) converts to 2^63 as an f64. So 2^63 passes the check and @intFromFloat is called with a value that does not fit.

Fix

The upper bound is exclusive in all three builtins, so 2^63 takes the existing Float path. Every rounded result in [-2^63, 2^63), including the largest f64 below 2^63, still returns an Int.

Tests

New eval: floor, ceil and round keep 2^63 as a float in packages/interpreter/tests/program_eval_test.zig checks that all three return a Float for 2^63 and that the largest f64 below 2^63, -2^63 and an ordinary value still return Ints with the right values. On main it crashes the test runner with that panic in floor; with this change it passes, and restoring <= in any one of the three builtins makes it crash again. zig build test -Denable_jsc=false -Dfilter=interpreter on macOS: 24/24. On Linux the interpreter test step does not compile on main or on this branch, because the parser's module resolver calls libc access and that test target does not link libc.

GitHub Actions does not run on this repo at the moment, so I ran the full suite on Linux x86_64 with the pinned Zig 0.17.0-dev.2163. On this branch zig build test --summary all passes 9163/9174, with the same 3 failing tests, 8 skips and 21 failed build steps as an earlier completed run of the same main commit (ff51b18), and identical diagnostic snapshot output; a fresh run on main stalled in an unrelated runtime test and timed out. With the built home, a program that prints floor(9223372036854774784.0) and then floor(9223372036854775807.0) prints the first value and then panics (exit 134) on main; on this branch it prints 9223372036854774784 and then the Float.

2 changed files on the files tab, with 0 review threads.

Sign in to comment on this pull request.