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
packages/interpreter/src/interpreter.zigmodified+5-3
Changes to packages/interpreter/src/interpreter.zig
@@ -3464,8 +3464,10 @@ pub const Interpreter = struct {
34643464 if (val == .Float) {
34653465 const floored = @floor(val.Float);
34663466 // If the result fits in i64, return Int; otherwise Float.
3467 // maxInt(i64) rounds up to 2^63 as an f64, which does not fit,
3468 // so the upper bound is exclusive.
34673469 if (floored >= @as(f64, @floatFromInt(std.math.minInt(i64))) and
3468 floored <= @as(f64, @floatFromInt(std.math.maxInt(i64))))
3470 floored < @as(f64, @floatFromInt(std.math.maxInt(i64))))
34693471 {
34703472 return Value{ .Int = @intFromFloat(floored) };
34713473 }
@@ -3481,7 +3483,7 @@ pub const Interpreter = struct {
34813483 if (val == .Float) {
34823484 const ceiled = @ceil(val.Float);
34833485 if (ceiled >= @as(f64, @floatFromInt(std.math.minInt(i64))) and
3484 ceiled <= @as(f64, @floatFromInt(std.math.maxInt(i64))))
3486 ceiled < @as(f64, @floatFromInt(std.math.maxInt(i64))))
34853487 {
34863488 return Value{ .Int = @intFromFloat(ceiled) };
34873489 }
@@ -3497,7 +3499,7 @@ pub const Interpreter = struct {
34973499 if (val == .Float) {
34983500 const rounded = @round(val.Float);
34993501 if (rounded >= @as(f64, @floatFromInt(std.math.minInt(i64))) and
3500 rounded <= @as(f64, @floatFromInt(std.math.maxInt(i64))))
3502 rounded < @as(f64, @floatFromInt(std.math.maxInt(i64))))
35013503 {
35023504 return Value{ .Int = @intFromFloat(rounded) };
35033505 }