ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.36k stars 2.51k forks source link

conversion from int to float silently truncates bits #21586

Open bwbuhse opened 1 week ago

bwbuhse commented 1 week ago

Zig Version

0.14.0-dev.1700+038e002b1

Steps to Reproduce and Observed Behavior

const std = @import("std");

pub fn main() void {
    const foo: u16 = 65535;
    const bar: f16 = foo;
    const baz: bool = (foo == bar);

    std.debug.print("{} {} {}\n", .{foo, bar, baz});
}

will output 65535 inf false

https://zig.godbolt.org/z/aYGWobfa5

It looks like this started happening sometime after zig 0.9.0 (according to Godbolt).

Thanks to @GrayHatter and @torque for helping with this :smile:

Expected Behavior

A compile error like in zig 0.9.0:

./example.zig:5:22: error: type f16 cannot represent integer value 65535
    const bar: f16 = foo;
                     ^
nektro commented 1 week ago

that's supposed to be a compile error and require @floatFromInt