authorgravatar for kate@kxt.ioKate Tsuyu <kate@kxt.io> 2020-08-28 08:58:51-04:00
committergravatar for kate@kxt.ioKate Tsuyu <kate@kxt.io> 2020-08-28 08:58:51-04:00
log9dfb917c200eb97223bdfcda93c08e509fe1362f
treefdfa499d4489227bc1e57bfda4ddb35a0c7c8db7
parent84d50c892d41850f666f05e05472c23ffabee434
signaturelock-open Commit is signed but in an unrecognized format.

std: Add std.math.divCeil


1 files changed, 20 insertions(+), 0 deletions(-)

lib/std/math.zig+20
......@@ -621,6 +621,26 @@ fn testDivFloor() void {
621621 testing.expect((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0);
622622}
623623
624pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
625 @setRuntimeSafety(false);
626 if (numerator <= 0) return divTrunc(T, numerator, denominator);
627 return (try divFloor(T, numerator - 1, denominator)) + 1;
628}
629
630test "math.divCeil" {
631 testDivCeil();
632 comptime testDivCeil();
633}
634fn testDivCeil() void {
635 testing.expect((divCeil(i32, 5, 3) catch unreachable) == 2);
636 testing.expect((divCeil(i32, -5, 3) catch unreachable) == -1);
637 testing.expectError(error.DivisionByZero, divCeil(i8, -5, 0));
638 testing.expectError(error.Overflow, divCeil(i8, -128, -1));
639
640 testing.expect((divCeil(f32, 5.0, 3.0) catch unreachable) == 2.0);
641 testing.expect((divCeil(f32, -5.0, 3.0) catch unreachable) == -1.0);
642}
643
624644pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
625645 @setRuntimeSafety(false);
626646 if (denominator == 0) return error.DivisionByZero;