1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use super::big_num::U128;
pub trait UnsafeMathTrait {
fn div_rounding_up(x: Self, y: Self) -> Self;
}
impl UnsafeMathTrait for u64 {
fn div_rounding_up(x: Self, y: Self) -> Self {
x / y + ((x % y > 0) as u64)
}
}
impl UnsafeMathTrait for U128 {
fn div_rounding_up(x: Self, y: Self) -> Self {
x / y + U128::from((x % y > U128::default()) as u8)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn divide_by_factor() {
assert_eq!(u64::div_rounding_up(4, 2), 2);
}
#[test]
fn divide_and_round_up() {
assert_eq!(u64::div_rounding_up(4, 3), 2);
}
#[test]
#[should_panic]
fn divide_by_zero() {
u64::div_rounding_up(2, 0);
}
}