@@ -35,6 +35,17 @@ impl<T: Copy + From<u8> + PartialEq> Fraction<T> for (T, T) {
35
35
macro_rules! impl_mul_fraction {
36
36
( $Uint: ident) => {
37
37
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
+ /// ```
38
49
pub fn checked_mul_floor<F : Fraction <T >, T : Into <$Uint>>(
39
50
self ,
40
51
rhs: F ,
@@ -46,10 +57,22 @@ macro_rules! impl_mul_fraction {
46
57
Ok ( res. try_into( ) ?)
47
58
}
48
59
60
+ /// Same operation as `checked_mul_floor` except unwrapped
49
61
pub fn mul_floor<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self {
50
62
self . checked_mul_floor( rhs) . unwrap( )
51
63
}
52
64
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
+ /// ```
53
76
pub fn checked_mul_ceil<F : Fraction <T >, T : Into <$Uint>>(
54
77
self ,
55
78
rhs: F ,
@@ -65,17 +88,22 @@ macro_rules! impl_mul_fraction {
65
88
}
66
89
}
67
90
91
+ /// Same operation as `checked_mul_ceil` except unwrapped
68
92
pub fn mul_ceil<F : Fraction <T >, T : Into <$Uint>>( self , rhs: F ) -> Self {
69
93
self . checked_mul_ceil( rhs) . unwrap( )
70
94
}
71
95
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
+ /// ```
79
107
pub fn checked_div_floor<F : Fraction <T >, T : Into <$Uint>>(
80
108
self ,
81
109
rhs: F ,
@@ -90,13 +118,25 @@ macro_rules! impl_mul_fraction {
90
118
Ok ( res. try_into( ) ?)
91
119
}
92
120
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
94
123
where
95
124
Self : Sized ,
96
125
{
97
- self . checked_div_ceil ( rhs) . unwrap( )
126
+ self . checked_div_floor ( rhs) . unwrap( )
98
127
}
99
128
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
+ /// ```
100
140
pub fn checked_div_ceil<F : Fraction <T >, T : Into <$Uint>>(
101
141
self ,
102
142
rhs: F ,
@@ -114,6 +154,14 @@ macro_rules! impl_mul_fraction {
114
154
Ok ( floor_result)
115
155
}
116
156
}
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
+ }
117
165
}
118
166
} ;
119
167
}
0 commit comments