diff --git a/utils/fixed_decimal/src/decimal.rs b/utils/fixed_decimal/src/decimal.rs index 181ef117117..fc791b0e6c4 100644 --- a/utils/fixed_decimal/src/decimal.rs +++ b/utils/fixed_decimal/src/decimal.rs @@ -352,6 +352,9 @@ impl FixedDecimal { /// Returns the relative ordering of the digits after `magnitude` with respect to 5. #[cfg(feature = "experimental")] fn half_at_next_magnitude(&self, magnitude: i16) -> Ordering { + #[cfg(debug_assertions)] // Depends on having no trailing zeroes. + self.check_invariants(); + match self.digit_at_next_position(magnitude).cmp(&5) { // If the next digit is equal to 5, we can know if we're at exactly 5 // by comparing the next magnitude with the last nonzero magnitude of @@ -386,6 +389,9 @@ impl FixedDecimal { magnitude: i16, increment: RoundingIncrement, ) -> Ordering { + #[cfg(debug_assertions)] // Depends on having no trailing zeroes. + self.check_invariants(); + match increment { RoundingIncrement::MultiplesOf1 => self.half_at_next_magnitude(magnitude), RoundingIncrement::MultiplesOf2 => { @@ -1475,8 +1481,20 @@ impl FixedDecimal { /// dec.half_trunc(0); /// assert_eq!("4", dec.to_string()); /// ``` - #[cfg(not(feature = "experimental"))] pub fn half_trunc(&mut self, position: i16) { + #[cfg(feature = "experimental")] + { + self.half_trunc_to_increment(position, RoundingIncrement::MultiplesOf1); + } + + #[cfg(not(feature = "experimental"))] + { + self.half_trunc_internal(position); + } + } + + #[cfg(not(feature = "experimental"))] + fn half_trunc_internal(&mut self, position: i16) { let digit_after_position = self.digit_at_next_position(position); let should_expand = match digit_after_position.cmp(&5) { Ordering::Less => false, @@ -1495,39 +1513,6 @@ impl FixedDecimal { } } - /// Half Truncates the number on the right to a particular position, deleting - /// digits if necessary. - /// - /// # Examples - /// - /// ``` - /// use fixed_decimal::FixedDecimal; - /// # use std::str::FromStr; - /// - /// let mut dec = FixedDecimal::from_str("-1.5").unwrap(); - /// dec.half_trunc(0); - /// assert_eq!("-1", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("0.4").unwrap(); - /// dec.half_trunc(0); - /// assert_eq!("0", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("0.5").unwrap(); - /// dec.half_trunc(0); - /// assert_eq!("0", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("0.6").unwrap(); - /// dec.half_trunc(0); - /// assert_eq!("1", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("1.5").unwrap(); - /// dec.half_trunc(0); - /// assert_eq!("1", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("3.954").unwrap(); - /// dec.half_trunc(0); - /// assert_eq!("4", dec.to_string()); - /// ``` - #[cfg(feature = "experimental")] - pub fn half_trunc(&mut self, position: i16) { - self.half_trunc_to_increment(position, RoundingIncrement::MultiplesOf1); - } - /// Half Truncates the number on the right to a particular position and rounding increment, /// deleting digits if necessary. /// @@ -2114,8 +2099,20 @@ impl FixedDecimal { /// dec.half_expand(0); /// assert_eq!("2", dec.to_string()); /// ``` - #[cfg(not(feature = "experimental"))] pub fn half_expand(&mut self, position: i16) { + #[cfg(feature = "experimental")] + { + self.half_expand_to_increment(position, RoundingIncrement::MultiplesOf1); + } + + #[cfg(not(feature = "experimental"))] + { + self.half_expand_internal(position); + } + } + + #[cfg(not(feature = "experimental"))] + pub fn half_expand_internal(&mut self, position: i16) { let digit_after_position = self.digit_at_next_position(position); if digit_after_position >= 5 { @@ -2125,35 +2122,6 @@ impl FixedDecimal { } } - /// Take the half expand of the number at a particular position. - /// - /// # Examples - /// - /// ``` - /// use fixed_decimal::FixedDecimal; - /// # use std::str::FromStr; - /// - /// let mut dec = FixedDecimal::from_str("-1.5").unwrap(); - /// dec.half_expand(0); - /// assert_eq!("-2", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("0.4").unwrap(); - /// dec.half_expand(0); - /// assert_eq!("0", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("0.5").unwrap(); - /// dec.half_expand(0); - /// assert_eq!("1", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("0.6").unwrap(); - /// dec.half_expand(0); - /// assert_eq!("1", dec.to_string()); - /// let mut dec = FixedDecimal::from_str("1.5").unwrap(); - /// dec.half_expand(0); - /// assert_eq!("2", dec.to_string()); - /// ``` - #[cfg(feature = "experimental")] - pub fn half_expand(&mut self, position: i16) { - self.half_expand_to_increment(position, RoundingIncrement::MultiplesOf1); - } - /// Take the half expand of the number at a particular position and rounding increment. /// ///