Function divCeil [src]
Divide numerator by denominator, rounding toward positive
infinity. Returns an error on overflow or when denominator is
zero.
Prototype
pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T
Parameters
T: type
numerator: T
denominator: T
Example
test divCeil {
try testDivCeil();
try comptime testDivCeil();
}
Source
pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
@setRuntimeSafety(false);
if (denominator == 0) return error.DivisionByZero;
const info = @typeInfo(T);
switch (info) {
.comptime_float, .float => return @ceil(numerator / denominator),
.comptime_int, .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)),
}
}