You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const std = @import("std");
test "abs" {
const a: i32 = std.crypto.random.int(i32); // random to prevent comptime evaluation
const b: i32 = @abs(a);
try std.testing.expect(a == b or a == -b);
}
Fails to compile with:
test.zig:4:20: error: expected type 'i32', found 'u32'
const b: i32 = @abs(a);
^~~~~~~
test.zig:4:20: note: signed 32-bit int cannot represent all possible unsigned 32-bit values
Expected Behavior
I expect the test to compile.
As far as I can tell, this is because abs returns a u32 when it should return a u31, but I am not super certain about the type system.
The text was updated successfully, but these errors were encountered:
Unfortunately, it has to be a u32, because signed integer have a negative range one larger than their positive range. For instance, an i8 can represent the value -128, but a u7 can only represent values up to 127.
mlugg
added
question
No questions on the issue tracker, please.
and removed
bug
Observed behavior contradicts documented or intended behavior
labels
Dec 28, 2024
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
This test:
Fails to compile with:
Expected Behavior
I expect the test to compile.
As far as I can tell, this is because abs returns a u32 when it should return a u31, but I am not super certain about the type system.
The text was updated successfully, but these errors were encountered: