@@ -35,6 +35,17 @@ impl<T: Copy + From<u8> + PartialEq> Fraction<T> for (T, T) {
3535macro_rules! impl_mul_fraction {
3636 ( $Uint: ident) => {
3737 impl $Uint {
38+ /// Multiply `self` with a struct implementing [`Fraction`] (e.g. [`crate::Decimal`]).
39+ /// Result is rounded down.
40+ ///
41+ /// ## Examples
42+ ///
43+ /// ```
44+ /// use cosmwasm_std::Uint128;
45+ /// let fraction = (8u128, 21u128);
46+ /// let res = Uint128::new(123456).checked_mul_floor(fraction).unwrap();
47+ /// assert_eq!(Uint128::new(47030), res); // 47030.8571 rounds down
48+ /// ```
3849 pub fn checked_mul_floor<F : Fraction <T >, T : Into <$Uint>>(
3950 self ,
4051 rhs: F ,
@@ -46,10 +57,22 @@ macro_rules! impl_mul_fraction {
4657 Ok ( res. try_into( ) ?)
4758 }
4859
60+ /// Same operation as `checked_mul_floor` except unwrapped
4961 pub fn mul_floor<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self {
5062 self . checked_mul_floor( rhs) . unwrap( )
5163 }
5264
65+ /// Multiply `self` with a struct implementing [`Fraction`] (e.g. [`crate::Decimal`]).
66+ /// Result is rounded up.
67+ ///
68+ /// ## Examples
69+ ///
70+ /// ```
71+ /// use cosmwasm_std::Uint128;
72+ /// let fraction = (8u128, 21u128);
73+ /// let res = Uint128::new(123456).checked_mul_ceil(fraction).unwrap();
74+ /// assert_eq!(Uint128::new(47031), res); // 47030.8571 rounds up
75+ /// ```
5376 pub fn checked_mul_ceil<F : Fraction <T >, T : Into <$Uint>>(
5477 self ,
5578 rhs: F ,
@@ -65,17 +88,22 @@ macro_rules! impl_mul_fraction {
6588 }
6689 }
6790
91+ /// Same operation as `checked_mul_ceil` except unwrapped
6892 pub fn mul_ceil<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self {
6993 self . checked_mul_ceil( rhs) . unwrap( )
7094 }
7195
72- pub fn div_floor<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self
73- where
74- Self : Sized ,
75- {
76- self . checked_div_floor( rhs) . unwrap( )
77- }
78-
96+ /// Divide `self` with a struct implementing [`Fraction`] (e.g. [`crate::Decimal`]).
97+ /// Result is rounded down.
98+ ///
99+ /// ## Examples
100+ ///
101+ /// ```
102+ /// use cosmwasm_std::Uint128;
103+ /// let fraction = (4u128, 5u128);
104+ /// let res = Uint128::new(789).checked_div_floor(fraction).unwrap();
105+ /// assert_eq!(Uint128::new(986), res); // 986.25 rounds down
106+ /// ```
79107 pub fn checked_div_floor<F : Fraction <T >, T : Into <$Uint>>(
80108 self ,
81109 rhs: F ,
@@ -90,13 +118,25 @@ macro_rules! impl_mul_fraction {
90118 Ok ( res. try_into( ) ?)
91119 }
92120
93- pub fn div_ceil<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self
121+ /// Same operation as `checked_div_floor` except unwrapped
122+ pub fn div_floor<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self
94123 where
95124 Self : Sized ,
96125 {
97- self . checked_div_ceil ( rhs) . unwrap( )
126+ self . checked_div_floor ( rhs) . unwrap( )
98127 }
99128
129+ /// Divide `self` with a struct implementing [`Fraction`] (e.g. [`crate::Decimal`]).
130+ /// Result is rounded up.
131+ ///
132+ /// ## Examples
133+ ///
134+ /// ```
135+ /// use cosmwasm_std::Uint128;
136+ /// let fraction = (4u128, 5u128);
137+ /// let res = Uint128::new(789).checked_div_ceil(fraction).unwrap();
138+ /// assert_eq!(Uint128::new(987), res); // 986.25 rounds up
139+ /// ```
100140 pub fn checked_div_ceil<F : Fraction <T >, T : Into <$Uint>>(
101141 self ,
102142 rhs: F ,
@@ -114,6 +154,14 @@ macro_rules! impl_mul_fraction {
114154 Ok ( floor_result)
115155 }
116156 }
157+
158+ /// Same operation as `checked_div_ceil` except unwrapped
159+ pub fn div_ceil<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self
160+ where
161+ Self : Sized ,
162+ {
163+ self . checked_div_ceil( rhs) . unwrap( )
164+ }
117165 }
118166 } ;
119167}
0 commit comments