Merge pull request #6189 from katesuyu/div-ceil

std: Add std.math.divCeil
This commit is contained in:
Andrew Kelley 2020-08-30 01:09:39 -04:00 committed by GitHub
commit 427e2d689d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -596,6 +596,59 @@ 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 (comptime std.meta.trait.isNumber(T) and denominator == 0) return error.DivisionByZero;
const info = @typeInfo(T);
switch (info) {
.ComptimeFloat, .Float => return @ceil(numerator / denominator),
.ComptimeInt, .Int => {
if (numerator < 0 and denominator < 0) {
if (info == .Int and numerator == minInt(T) and denominator == -1)
return error.Overflow;
return @divFloor(numerator + 1, denominator) + 1;
}
if (numerator > 0 and denominator > 0)
return @divFloor(numerator - 1, denominator) + 1;
return @divTrunc(numerator, denominator);
},
else => @compileError("divCeil unsupported on " ++ @typeName(T)),
}
}
test "math.divCeil" {
testDivCeil();
comptime testDivCeil();
}
fn testDivCeil() void {
testing.expectEqual(@as(i32, 2), divCeil(i32, 5, 3) catch unreachable);
testing.expectEqual(@as(i32, -1), divCeil(i32, -5, 3) catch unreachable);
testing.expectEqual(@as(i32, -1), divCeil(i32, 5, -3) catch unreachable);
testing.expectEqual(@as(i32, 2), divCeil(i32, -5, -3) catch unreachable);
testing.expectEqual(@as(i32, 0), divCeil(i32, 0, 5) catch unreachable);
testing.expectEqual(@as(u32, 0), divCeil(u32, 0, 5) catch unreachable);
testing.expectError(error.DivisionByZero, divCeil(i8, -5, 0));
testing.expectError(error.Overflow, divCeil(i8, -128, -1));
testing.expectEqual(@as(f32, 0.0), divCeil(f32, 0.0, 5.0) catch unreachable);
testing.expectEqual(@as(f32, 2.0), divCeil(f32, 5.0, 3.0) catch unreachable);
testing.expectEqual(@as(f32, -1.0), divCeil(f32, -5.0, 3.0) catch unreachable);
testing.expectEqual(@as(f32, -1.0), divCeil(f32, 5.0, -3.0) catch unreachable);
testing.expectEqual(@as(f32, 2.0), divCeil(f32, -5.0, -3.0) catch unreachable);
testing.expectEqual(6, divCeil(comptime_int, 23, 4) catch unreachable);
testing.expectEqual(-5, divCeil(comptime_int, -23, 4) catch unreachable);
testing.expectEqual(-5, divCeil(comptime_int, 23, -4) catch unreachable);
testing.expectEqual(6, divCeil(comptime_int, -23, -4) catch unreachable);
testing.expectError(error.DivisionByZero, divCeil(comptime_int, 23, 0));
testing.expectEqual(6.0, divCeil(comptime_float, 23.0, 4.0) catch unreachable);
testing.expectEqual(-5.0, divCeil(comptime_float, -23.0, 4.0) catch unreachable);
testing.expectEqual(-5.0, divCeil(comptime_float, 23.0, -4.0) catch unreachable);
testing.expectEqual(6.0, divCeil(comptime_float, -23.0, -4.0) catch unreachable);
testing.expectError(error.DivisionByZero, divCeil(comptime_float, 23.0, 0.0));
}
pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
@setRuntimeSafety(false);
if (denominator == 0) return error.DivisionByZero;