mirror of
https://github.com/ziglang/zig.git
synced 2026-02-16 14:28:57 +00:00
std.math.cast handles signed integers
This commit is contained in:
parent
6cb99fdac3
commit
5b00dee0c2
@ -515,15 +515,28 @@ test "math.negateCast" {
|
||||
|
||||
/// Cast an integer to a different integer type. If the value doesn't fit,
|
||||
/// return an error.
|
||||
pub fn cast(comptime T: type, x: var) !T {
|
||||
pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
|
||||
comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer
|
||||
if (x > @maxValue(T)) {
|
||||
comptime assert(@typeId(@typeOf(x)) == builtin.TypeId.Int); // must pass an integer
|
||||
if (@maxValue(@typeOf(x)) > @maxValue(T) and x > @maxValue(T)) {
|
||||
return error.Overflow;
|
||||
} else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) {
|
||||
return error.Overflow;
|
||||
} else {
|
||||
return T(x);
|
||||
}
|
||||
}
|
||||
|
||||
test "math.cast" {
|
||||
if (cast(u8, u32(300))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
||||
if (cast(i8, i32(-200))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
||||
if (cast(u8, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
||||
if (cast(u64, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
|
||||
|
||||
assert((try cast(u8, u32(255))) == u8(255));
|
||||
assert(@typeOf(try cast(u8, u32(255))) == u8);
|
||||
}
|
||||
|
||||
pub fn floorPowerOfTwo(comptime T: type, value: T) T {
|
||||
var x = value;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user