commit 427e2d689d34637bf7621be49beb7ec81b3fec0f (tree)
parent ac85befbb4356daa774b6083a3aa853b9c78f548
Author: Andrew Kelley <andrew@ziglang.org>
Date: Sun, 30 Aug 2020 01:09:39 -0400
Merge pull request #6189 from katesuyu/div-ceil
std: Add std.math.divCeil
Diffstat:
1 file changed, 53 insertions(+), 0 deletions(-)
diff --git a/lib/std/math.zig b/lib/std/math.zig
@@ -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;