std: Add std.math.divCeil

This commit is contained in:
Kate Tsuyu 2020-08-28 08:58:51 -04:00
parent 84d50c892d
commit 9dfb917c20
No known key found for this signature in database
GPG Key ID: 144B2129293B0F59

View File

@ -621,6 +621,26 @@ fn testDivFloor() void {
testing.expect((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0);
}
pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
@setRuntimeSafety(false);
if (numerator <= 0) return divTrunc(T, numerator, denominator);
return (try divFloor(T, numerator - 1, denominator)) + 1;
}
test "math.divCeil" {
testDivCeil();
comptime testDivCeil();
}
fn testDivCeil() void {
testing.expect((divCeil(i32, 5, 3) catch unreachable) == 2);
testing.expect((divCeil(i32, -5, 3) catch unreachable) == -1);
testing.expectError(error.DivisionByZero, divCeil(i8, -5, 0));
testing.expectError(error.Overflow, divCeil(i8, -128, -1));
testing.expect((divCeil(f32, 5.0, 3.0) catch unreachable) == 2.0);
testing.expect((divCeil(f32, -5.0, 3.0) catch unreachable) == -1.0);
}
pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
@setRuntimeSafety(false);
if (denominator == 0) return error.DivisionByZero;