zig/std/math.zig

32 lines
869 B
Zig
Raw Normal View History

pub const Cmp = enum {
Equal,
Greater,
Less,
};
pub fn min(x: var, y: var) -> @typeOf(x + y) {
if (x < y) x else y
}
pub fn max(x: var, y: var) -> @typeOf(x + y) {
if (x > y) x else y
}
2016-12-18 05:25:26 +00:00
error Overflow;
pub fn mulOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
2016-08-17 05:42:50 +00:00
if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer
}
pub fn addOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
2016-08-17 05:42:50 +00:00
if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer
}
pub fn subOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
2016-08-17 05:42:50 +00:00
if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer
}
pub fn shlOverflow(comptime T: type, a: T, b: T) -> %T {
var answer: T = undefined;
if (@shlWithOverflow(T, a, b, &answer)) error.Overflow else answer
}