Add number wrapping utility to coral
This commit is contained in:
parent
54e716e4cf
commit
181c44d85f
|
@ -161,3 +161,12 @@ pub fn min_int(comptime int: std.builtin.Type.Int) comptime_int {
|
||||||
|
|
||||||
return -(1 << (bit_count - 1));
|
return -(1 << (bit_count - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Returns `value` wrapped around the inclusive bounds of `lower` and `upper`.
|
||||||
|
///
|
||||||
|
pub fn wrap(value: anytype, lower: anytype, upper: anytype) @TypeOf(value, lower, upper) {
|
||||||
|
const range = upper - lower;
|
||||||
|
|
||||||
|
return if (range == 0) lower else lower + @mod((@mod((value - lower), range) + range), range);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue