Skip to main content

pyra_margin/
math.rs

1/// Checked ceiling division for integer types.
2pub trait CheckedDivCeil {
3    fn checked_div_ceil(self, rhs: Self) -> Option<Self>
4    where
5        Self: Sized;
6}
7
8impl CheckedDivCeil for u128 {
9    fn checked_div_ceil(self, divisor: u128) -> Option<u128> {
10        if divisor == 0 {
11            return None;
12        }
13        let quotient = self.checked_div(divisor)?;
14        let product = quotient.checked_mul(divisor)?;
15        let remainder = self.checked_sub(product)?;
16        if remainder == 0 {
17            Some(quotient)
18        } else {
19            quotient.checked_add(1)
20        }
21    }
22}
23
24impl CheckedDivCeil for i128 {
25    fn checked_div_ceil(self, divisor: i128) -> Option<i128> {
26        if divisor == 0 {
27            return None;
28        }
29        let quotient = self.checked_div(divisor)?;
30        let quotient_times_divisor = quotient.checked_mul(divisor)?;
31        let remainder = self.checked_sub(quotient_times_divisor)?;
32        if remainder == 0 {
33            return Some(quotient);
34        }
35        // Round up only when both operands have the same sign
36        let same_sign = (self > 0 && divisor > 0) || (self < 0 && divisor < 0);
37        if same_sign {
38            quotient.checked_add(1)
39        } else {
40            Some(quotient)
41        }
42    }
43}
44
45impl CheckedDivCeil for i64 {
46    fn checked_div_ceil(self, divisor: i64) -> Option<i64> {
47        if divisor == 0 {
48            return None;
49        }
50        let quotient = self.checked_div(divisor)?;
51        let quotient_times_divisor = quotient.checked_mul(divisor)?;
52        let remainder = self.checked_sub(quotient_times_divisor)?;
53        if remainder == 0 {
54            return Some(quotient);
55        }
56        let same_sign = (self > 0 && divisor > 0) || (self < 0 && divisor < 0);
57        if same_sign {
58            quotient.checked_add(1)
59        } else {
60            Some(quotient)
61        }
62    }
63}
64
65#[cfg(test)]
66#[allow(
67    clippy::allow_attributes,
68    clippy::allow_attributes_without_reason,
69    clippy::unwrap_used,
70    clippy::expect_used,
71    clippy::panic,
72    clippy::arithmetic_side_effects
73)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn u128_div_ceil_exact() {
79        assert_eq!(10u128.checked_div_ceil(5), Some(2));
80    }
81
82    #[test]
83    fn u128_div_ceil_rounds_up() {
84        assert_eq!(11u128.checked_div_ceil(5), Some(3));
85    }
86
87    #[test]
88    fn u128_div_ceil_zero_divisor() {
89        assert_eq!(10u128.checked_div_ceil(0), None);
90    }
91
92    #[test]
93    fn i128_div_ceil_positive() {
94        assert_eq!(11i128.checked_div_ceil(5), Some(3));
95    }
96
97    #[test]
98    fn i128_div_ceil_negative_no_roundup() {
99        // -11 / 5 = -2 remainder -1; different signs, no round up
100        assert_eq!((-11i128).checked_div_ceil(5), Some(-2));
101    }
102
103    #[test]
104    fn i128_div_ceil_both_negative_rounds_up() {
105        // -11 / -5 = 2 remainder -1; same signs, round up
106        assert_eq!((-11i128).checked_div_ceil(-5), Some(3));
107    }
108
109    #[test]
110    fn i64_div_ceil_exact() {
111        assert_eq!(10i64.checked_div_ceil(5), Some(2));
112    }
113
114    #[test]
115    fn i64_div_ceil_rounds_up() {
116        assert_eq!(11i64.checked_div_ceil(5), Some(3));
117    }
118}
119
120#[cfg(test)]
121#[allow(
122    clippy::allow_attributes,
123    clippy::allow_attributes_without_reason,
124    clippy::unwrap_used,
125    clippy::expect_used,
126    clippy::panic,
127    clippy::arithmetic_side_effects
128)]
129mod proptests {
130    use super::*;
131    use proptest::prelude::*;
132
133    proptest! {
134        #[test]
135        fn u128_div_ceil_never_panics(a: u128, b: u128) {
136            let _ = a.checked_div_ceil(b);
137        }
138
139        #[test]
140        fn u128_div_ceil_correct(a in 0u128..=u128::MAX / 2, b in 1u128..=1_000_000_000) {
141            let result = a.checked_div_ceil(b).unwrap();
142            // result >= a/b (ceiling property)
143            let floor = a / b;
144            let remainder = a % b;
145            if remainder == 0 {
146                prop_assert_eq!(result, floor);
147            } else {
148                prop_assert_eq!(result, floor + 1);
149            }
150        }
151
152        #[test]
153        fn i128_div_ceil_never_panics(a: i64, b: i64) {
154            let _ = (a as i128).checked_div_ceil(b as i128);
155        }
156
157        #[test]
158        fn i128_div_ceil_rounds_toward_positive_infinity_for_same_sign(
159            a in 1i128..=1_000_000_000_000,
160            b in 1i128..=1_000_000_000,
161        ) {
162            let result = a.checked_div_ceil(b).unwrap();
163            // For positive/positive: result * b >= a
164            prop_assert!(result * b >= a);
165            // But (result - 1) * b < a (tightest ceiling)
166            prop_assert!((result - 1) * b < a);
167        }
168    }
169}