Add number wrapping utility to coral

This commit is contained in:
kayomn 2023-05-23 23:22:12 +00:00
parent 54e716e4cf
commit 181c44d85f
1 changed files with 9 additions and 0 deletions

View File

@ -161,3 +161,12 @@ pub fn min_int(comptime int: std.builtin.Type.Int) comptime_int {
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);
}