File tree 3 files changed +31
-1
lines changed
3 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 1
1
---
2
- refs/heads/master: f4399063fc2a3bd6e34bee185abfb6b56c4236a7
2
+ refs/heads/master: 07ffe68ad9145a209725ac289f8c4235b4c9b334
Original file line number Diff line number Diff line change @@ -34,6 +34,29 @@ pure fn mul(x: uint, y: uint) -> uint { ret x * y; }
34
34
/* Function: div */
35
35
pure fn div ( x : uint , y : uint ) -> uint { ret x / y; }
36
36
37
+ /**
38
+ * Divide two numbers, return the result, rounded up.
39
+ */
40
+ pure fn div_ceil ( x : uint , y : uint ) -> uint {
41
+ let div = div ( x, y) ;
42
+ if x % y == 0 u { ret div; }
43
+ else { ret div + 1 u; }
44
+ }
45
+
46
+ /**
47
+ * Divide two numbers, return the result, rounded to the closest integer.
48
+ */
49
+ pure fn div_round ( x : uint , y : uint ) -> uint {
50
+ let div = div ( x, y) ;
51
+ if x % y * 2 u < y { ret div; }
52
+ else { ret div + 1 u; }
53
+ }
54
+
55
+ /**
56
+ * Divide two numbers, return the result, rounded down.
57
+ */
58
+ pure fn div_floor ( x : uint , y : uint ) -> uint { ret x / y; }
59
+
37
60
/* Function: rem */
38
61
pure fn rem ( x : uint , y : uint ) -> uint { ret x % y; }
39
62
Original file line number Diff line number Diff line change @@ -102,3 +102,10 @@ fn test_overflows() {
102
102
assert ( uint:: min_value ( ) <= 0 u) ;
103
103
assert ( uint:: min_value ( ) + uint:: max_value ( ) + 1 u == 0 u) ;
104
104
}
105
+
106
+ #[ test]
107
+ fn test_div ( ) {
108
+ assert ( uint:: div_floor ( 3 u, 4 u) == 0 u) ;
109
+ assert ( uint:: div_ceil ( 3 u, 4 u) == 1 u) ;
110
+ assert ( uint:: div_round ( 3 u, 4 u) == 1 u) ;
111
+ }
You can’t perform that action at this time.
0 commit comments