|
| 1 | +/* |
| 2 | +Copyright 2025 Owain Davies |
| 3 | +SPDX-License-Identifier: Apache-2.0 OR MIT |
| 4 | +*/ |
| 5 | + |
| 6 | +use crate::{Arbi, Assign}; |
| 7 | + |
| 8 | +impl Arbi { |
| 9 | + /// If `rhs` is positive, return the smallest value greater than or equal to |
| 10 | + /// `self` that is a multiple of `rhs`. If `rhs` is negative, return the |
| 11 | + /// largest value less than or equal to `self` that is a multiple of `rhs`. |
| 12 | + /// |
| 13 | + /// # Panic |
| 14 | + /// Panics if `rhs` is zero. |
| 15 | + /// |
| 16 | + /// # Examples |
| 17 | + /// ``` |
| 18 | + /// use arbi::Arbi; |
| 19 | + /// assert_eq!(Arbi::from(12).next_multiple_of(&Arbi::from(6)), 12); |
| 20 | + /// assert_eq!(Arbi::from(19).next_multiple_of(&Arbi::from(7)), 21); |
| 21 | + /// assert_eq!(Arbi::from(25).next_multiple_of(&Arbi::from(-5)), 25); |
| 22 | + /// assert_eq!(Arbi::from(25).next_multiple_of(&Arbi::from(-7)), 21); |
| 23 | + /// assert_eq!(Arbi::from(-21).next_multiple_of(&Arbi::from(7)), -21); |
| 24 | + /// assert_eq!(Arbi::from(-25).next_multiple_of(&Arbi::from(7)), -21); |
| 25 | + /// assert_eq!(Arbi::from(-21).next_multiple_of(&Arbi::from(-7)), -21); |
| 26 | + /// assert_eq!(Arbi::from(-25).next_multiple_of(&Arbi::from(-7)), -28); |
| 27 | + /// ``` |
| 28 | + /// |
| 29 | + /// Panics if `rhs` is zero: |
| 30 | + /// ```should_panic |
| 31 | + /// use arbi::Arbi; |
| 32 | + /// Arbi::from(123).next_multiple_of(&Arbi::zero()); |
| 33 | + /// ``` |
| 34 | + pub fn next_multiple_of(&self, rhs: &Self) -> Self { |
| 35 | + let (_, mut r) = self.divrem_floor_ref(rhs); |
| 36 | + if r.is_zero() { |
| 37 | + r.assign(self); |
| 38 | + r |
| 39 | + } else { |
| 40 | + (rhs - r) + self |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// If `rhs` is positive, return the largest value less than or equal to |
| 45 | + /// `self` that is a multiple of `rhs`. If `rhs` is negative, return the |
| 46 | + /// smallest value greater than or equal to `self` that is a multiple of |
| 47 | + /// `rhs`. |
| 48 | + /// |
| 49 | + /// # Panic |
| 50 | + /// Panics if `rhs` is zero. |
| 51 | + /// |
| 52 | + /// # Examples |
| 53 | + /// ``` |
| 54 | + /// use arbi::Arbi; |
| 55 | + /// assert_eq!(Arbi::from(12).prev_multiple_of(&Arbi::from(6)), 12); |
| 56 | + /// assert_eq!(Arbi::from(19).prev_multiple_of(&Arbi::from(7)), 14); |
| 57 | + /// assert_eq!(Arbi::from(25).prev_multiple_of(&Arbi::from(-5)), 25); |
| 58 | + /// assert_eq!(Arbi::from(25).prev_multiple_of(&Arbi::from(-7)), 28); |
| 59 | + /// assert_eq!(Arbi::from(-21).prev_multiple_of(&Arbi::from(7)), -21); |
| 60 | + /// assert_eq!(Arbi::from(-25).prev_multiple_of(&Arbi::from(7)), -28); |
| 61 | + /// assert_eq!(Arbi::from(-21).prev_multiple_of(&Arbi::from(-7)), -21); |
| 62 | + /// assert_eq!(Arbi::from(-25).prev_multiple_of(&Arbi::from(-7)), -21); |
| 63 | + /// ``` |
| 64 | + /// |
| 65 | + /// Panics if `rhs` is zero: |
| 66 | + /// ```should_panic |
| 67 | + /// use arbi::Arbi; |
| 68 | + /// Arbi::from(123).prev_multiple_of(&Arbi::zero()); |
| 69 | + /// ``` |
| 70 | + pub fn prev_multiple_of(&self, rhs: &Self) -> Self { |
| 71 | + let (_, mut r) = self.divrem_floor_ref(rhs); |
| 72 | + if r.is_zero() { |
| 73 | + // Potentially avoid memory allocation |
| 74 | + r.assign(self); |
| 75 | + r |
| 76 | + } else { |
| 77 | + self - r |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments