Skip to main content

nil_num/
mul_ceil.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4pub const trait MulCeil<Rhs = Self> {
5  type Output;
6
7  fn mul_ceil(self, rhs: Rhs) -> Self::Output;
8}
9
10impl const MulCeil for f64 {
11  type Output = f64;
12
13  fn mul_ceil(self, rhs: f64) -> f64 {
14    (self * rhs).ceil()
15  }
16}
17
18#[macro_export]
19macro_rules! impl_mul_ceil {
20  ($($name:ident),+ $(,)?) => {
21    $(
22      impl const $crate::mul_ceil::MulCeil<f64> for $name {
23        type Output = f64;
24
25        fn mul_ceil(self, rhs: f64) -> Self::Output {
26          (f64::from(self) * rhs).ceil()
27        }
28      }
29
30      impl const $crate::mul_ceil::MulCeil<$name> for f64 {
31        type Output = f64;
32
33        fn mul_ceil(self, rhs: $name) -> Self::Output {
34          (self * f64::from(rhs)).ceil()
35        }
36      }
37    )+
38  };
39}
40
41impl_mul_ceil!(i8, u8, i16, u16, i32, u32, f32);
42
43#[cfg(test)]
44mod tests {
45  use super::MulCeil;
46
47  #[test]
48  fn mul_ceil() {
49    assert_eq!(3.0.mul_ceil(2.0), 6.0);
50    assert_eq!(3.0.mul_ceil(2.1), 7.0);
51    assert_eq!(3.0.mul_ceil(2.5), 8.0);
52    assert_eq!(3.0.mul_ceil(2.9), 9.0);
53  }
54}