Closed
Description
Zig Version
0.15.0-dev.695+041eedc1c
Steps to Reproduce and Observed Behavior
Use @intFromFloat
to convert a floating point number to a small result type. Observe that safety checking does not occur in expected circumstances.
Test code:
const std = @import("std");
test "scalar" {
// If this is const, the compiler figures out it can't fit into all of the below types
var f: f32 = undefined;
f = 1000;
// Doesn't fit, but succeeds
const u_9: u9 = @as(u9, @intFromFloat(f));
std.debug.print("\n{d}", .{u_9});
// Doesn't fit, but succeeds
const i_10: i10 = @as(i10, @intFromFloat(f));
std.debug.print("\n{d}\n", .{i_10});
// Doesn't fit, invokes "integer part of floating point value out of bounds"
// const i_4: i8 = @as(i8, @intFromFloat(f));
// std.debug.print("\n{d}", .{i_4});
}
Output:
[2025-06-12T22:31:30.783Z] Running test: test.zig - scalar
1/1 test.test.scalar...
488
-24
OK
All 1 tests passed.
Expected Behavior
Based on the docs that say:
If the integer part of the floating point number cannot fit in the destination type, it invokes safety-checked Illegal Behavior.
I would expect an integer part of floating point value out of bounds
panic from all of the @intFromFloat
calls above, not just for i8
.