1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use crate::{
core::{CheckedAdd, CheckedDiv, CheckedMul, CheckedRem, CheckedSub, DecimalOperationError},
impl_checked_arithmetic,
};
impl_checked_arithmetic! { u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 usize isize }
/// A trait for performing checked decimal operations.
pub trait CheckedDecimalOperations {
/// Adds two values with decimals and returns the result along with the number of decimals in the result.
///
/// # Arguments
///
/// * `self` - The first value to add.
/// * `other` - The second value to add.
/// * `self_decimals` - The number of decimals in the first value.
/// * `other_decimals` - The number of decimals in the second value.
///
/// # Returns
///
/// Returns a `Result` containing the sum of the values and the number of decimals in the result,
/// or a `DecimalOperationError` if the operation fails.
fn add_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError>
where
Self: Sized;
/// Subtracts two values with decimals and returns the result along with the number of decimals in the result.
///
/// # Arguments
///
/// * `self` - The value to subtract from.
/// * `other` - The value to subtract.
/// * `self_decimals` - The number of decimals in the first value.
/// * `other_decimals` - The number of decimals in the second value.
///
/// # Returns
///
/// Returns a `Result` containing the difference of the values and the number of decimals in the result,
/// or a `DecimalOperationError` if the operation fails.
fn sub_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError>
where
Self: Sized;
/// Multiplies two values with decimals and returns the result along with the number of decimals in the result.
///
/// # Arguments
///
/// * `self` - The first value to multiply.
/// * `other` - The second value to multiply.
/// * `self_decimals` - The number of decimals in the first value.
/// * `other_decimals` - The number of decimals in the second value.
///
/// # Returns
///
/// Returns a `Result` containing the product of the values and the number of decimals in the result,
/// or a `DecimalOperationError` if the operation fails.
fn multiply_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError>
where
Self: Sized;
/// Divides two values with decimals and returns the result along with the number of decimals in the result.
///
/// # Arguments
///
/// * `self` - The value to divide.
/// * `other` - The value to divide by.
/// * `self_decimals` - The number of decimals in the first value.
/// * `other_decimals` - The number of decimals in the second value.
///
/// # Returns
///
/// Returns a `Result` containing the quotient of the values and the number of decimals in the result,
/// or a `DecimalOperationError` if the operation fails.
fn divide_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError>
where
Self: Sized;
/// Calculates the remainder of dividing two values with decimals and returns the result along with the number of decimals in the result.
///
/// # Arguments
///
/// * `self` - The value to calculate the remainder for.
/// * `other` - The value to divide by.
/// * `self_decimals` - The number of decimals in the first value.
/// * `other_decimals` - The number of decimals in the second value.
///
/// # Returns
///
/// Returns a `Result` containing the remainder of the division and the number of decimals in the result,
/// or a `DecimalOperationError` if the operation fails.
fn rem_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError>
where
Self: Sized;
}
// Blanket implementation of the DecimalOps trait for all types implementing numeric operations
impl<T> CheckedDecimalOperations for T
where
T: CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + CheckedRem + From<u32>,
{
fn add_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError> {
if self_decimals > other_decimals {
let factor = T::from(10u32.pow(self_decimals - other_decimals));
match self.checked_add(
&other
.checked_mul(&factor)
.ok_or(DecimalOperationError::Overflow)?,
) {
Some(value) => Ok((value, self_decimals)),
None => Err(DecimalOperationError::Overflow),
}
} else {
let factor = T::from(10u32.pow(other_decimals - self_decimals));
match self
.checked_mul(&factor)
.and_then(|x| x.checked_add(&other))
{
Some(value) => Ok((value, other_decimals)),
None => Err(DecimalOperationError::Overflow),
}
}
}
fn sub_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError> {
if self_decimals > other_decimals {
let factor = T::from(10u32.pow(self_decimals - other_decimals));
match self.checked_sub(
&other
.checked_mul(&factor)
.ok_or(DecimalOperationError::Overflow)?,
) {
Some(value) => Ok((value, self_decimals)),
None => Err(DecimalOperationError::Overflow),
}
} else {
let factor = T::from(10u32.pow(other_decimals - self_decimals));
match self
.checked_mul(&factor)
.and_then(|x| x.checked_sub(&other))
{
Some(value) => Ok((value, other_decimals)),
None => Err(DecimalOperationError::Overflow),
}
}
}
fn multiply_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError> {
match self.checked_mul(&other) {
Some(value) => Ok((value, self_decimals + other_decimals)),
None => Err(DecimalOperationError::Overflow),
}
}
fn divide_decimals_checked(
self,
other: Self,
self_decimals: u32,
other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError> {
let factor = T::from(10u32.pow(other_decimals));
let adjusted_value = self
.checked_mul(&factor)
.ok_or(DecimalOperationError::Overflow)?;
match adjusted_value.checked_div(&other) {
Some(value) => Ok((value, self_decimals)),
None => Err(DecimalOperationError::DivisionByZero),
}
}
fn rem_decimals_checked(
self,
other: Self,
self_decimals: u32,
_other_decimals: u32,
) -> Result<(Self, u32), DecimalOperationError> {
let factor = T::from(10u32.pow(self_decimals));
let adjusted_value = self
.checked_mul(&factor)
.ok_or(DecimalOperationError::Overflow)?;
match adjusted_value.checked_rem(&other) {
Some(value) => Ok((value, self_decimals)),
None => Err(DecimalOperationError::DivisionByZero),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_decimals() -> Result<(), Box<dyn std::error::Error>> {
let a: u64 = 1_0000;
let a_decimals = 4;
let b: u64 = 2_00;
let b_decimals = 2;
let (result, decimals) = a.add_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 3_0000);
assert_eq!(decimals, 4);
let a: u32 = 123_45;
let a_decimals = 2;
let b: u32 = 0_45;
let b_decimals = 2;
let (result, decimals) = a.add_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 123_90);
assert_eq!(decimals, 2);
Ok(())
}
#[test]
fn test_sub_decimals() -> Result<(), Box<dyn std::error::Error>> {
let a: u64 = 3_0000;
let a_decimals = 4;
let b: u64 = 2_00;
let b_decimals = 2;
let (result, decimals) = a.sub_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 1_0000);
assert_eq!(decimals, 4);
let a: u32 = 123_45;
let a_decimals = 2;
let b: u32 = 0_45;
let b_decimals = 2;
let (result, decimals) = a.sub_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 123_00);
assert_eq!(decimals, 2);
Ok(())
}
#[test]
fn test_mul_decimals() -> Result<(), Box<dyn std::error::Error>> {
let a: u64 = 3_0000;
let a_decimals = 4;
let b: u64 = 2_00;
let b_decimals = 2;
let (result, decimals) = a.multiply_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 6_000000);
assert_eq!(decimals, 6);
let a: u32 = 12345;
let a_decimals = 2;
let b: u32 = 45;
let b_decimals = 2;
let (result, decimals) = a.multiply_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 555525);
assert_eq!(decimals, 4);
Ok(())
}
#[test]
fn test_div_decimals() -> Result<(), Box<dyn std::error::Error>> {
let a: u64 = 6_0000;
let a_decimals = 4;
let b: u64 = 2_00;
let b_decimals = 2;
let (result, decimals) = a.divide_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 3_0000);
assert_eq!(decimals, 4);
let a: u32 = 123_45;
let a_decimals = 2;
let b: u32 = 0_45;
let b_decimals = 2;
let (result, decimals) = a.divide_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 27433);
assert_eq!(decimals, 2);
Ok(())
}
#[test]
fn test_rem_decimals() -> Result<(), Box<dyn std::error::Error>> {
let a: u64 = 6_0000;
let a_decimals = 4;
let b: u64 = 2_00;
let b_decimals = 2;
let (result, decimals) = a.rem_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 0);
assert_eq!(decimals, 4);
let a: u32 = 123_45;
let a_decimals = 2;
let b: u32 = 0_45;
let b_decimals = 2;
let (result, decimals) = a.rem_decimals_checked(b, a_decimals, b_decimals)?;
assert_eq!(result, 15);
assert_eq!(decimals, 2);
Ok(())
}
}