| ... | ... | @@ -621,6 +621,26 @@ fn testDivFloor() void { |
| 621 | 621 | testing.expect((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0); |
| 622 | 622 | } |
| 623 | 623 | |
| 624 | pub 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 | |
| 630 | test "math.divCeil" { |
| 631 | testDivCeil(); |
| 632 | comptime testDivCeil(); |
| 633 | } |
| 634 | fn 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 | |
| 624 | 644 | pub fn divExact(comptime T: type, numerator: T, denominator: T) !T { |
| 625 | 645 | @setRuntimeSafety(false); |
| 626 | 646 | if (denominator == 0) return error.DivisionByZero; |