Skip to content

Commit 135912c

Browse files
committed
Move impl Rem into impl_ops_rem.rs
1 parent c9c860a commit 135912c

File tree

2 files changed

+79
-75
lines changed

2 files changed

+79
-75
lines changed

src/impl_ops_rem.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! Remainder implementations
2+
3+
use super::*;
4+
5+
impl Rem<BigDecimal> for BigDecimal {
6+
type Output = BigDecimal;
7+
8+
#[inline]
9+
fn rem(self, other: BigDecimal) -> BigDecimal {
10+
let scale = cmp::max(self.scale, other.scale);
11+
12+
let num = self.take_and_scale(scale).int_val;
13+
let den = other.take_and_scale(scale).int_val;
14+
15+
BigDecimal::new(num % den, scale)
16+
}
17+
}
18+
19+
impl<'a> Rem<&'a BigDecimal> for BigDecimal {
20+
type Output = BigDecimal;
21+
22+
#[inline]
23+
fn rem(self, other: &BigDecimal) -> BigDecimal {
24+
let scale = cmp::max(self.scale, other.scale);
25+
let num = self.take_and_scale(scale).int_val;
26+
let den = &other.int_val;
27+
28+
let result = if scale == other.scale {
29+
num % den
30+
} else {
31+
num % (den * ten_to_the((scale - other.scale) as u64))
32+
};
33+
BigDecimal::new(result, scale)
34+
}
35+
}
36+
impl<'a> Rem<BigDecimal> for &'a BigDecimal {
37+
type Output = BigDecimal;
38+
39+
#[inline]
40+
fn rem(self, other: BigDecimal) -> BigDecimal {
41+
let scale = cmp::max(self.scale, other.scale);
42+
let num = &self.int_val;
43+
let den = other.take_and_scale(scale).int_val;
44+
45+
let result = if scale == self.scale {
46+
num % den
47+
} else {
48+
let scaled_num = num * ten_to_the((scale - self.scale) as u64);
49+
scaled_num % den
50+
};
51+
52+
BigDecimal::new(result, scale)
53+
}
54+
}
55+
56+
impl<'a, 'b> Rem<&'b BigDecimal> for &'a BigDecimal {
57+
type Output = BigDecimal;
58+
59+
#[inline]
60+
fn rem(self, other: &BigDecimal) -> BigDecimal {
61+
let scale = cmp::max(self.scale, other.scale);
62+
let num = &self.int_val;
63+
let den = &other.int_val;
64+
65+
let result = match self.scale.cmp(&other.scale) {
66+
Ordering::Equal => num % den,
67+
Ordering::Less => {
68+
let scaled_num = num * ten_to_the((scale - self.scale) as u64);
69+
scaled_num % den
70+
}
71+
Ordering::Greater => {
72+
let scaled_den = den * ten_to_the((scale - other.scale) as u64);
73+
num % scaled_den
74+
}
75+
};
76+
BigDecimal::new(result, scale)
77+
}
78+
}

src/lib.rs

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ mod impl_ops_add;
9797
mod impl_ops_sub;
9898
mod impl_ops_mul;
9999
mod impl_ops_div;
100+
mod impl_ops_rem;
100101

101102
// PartialEq
102103
mod impl_cmp;
@@ -1233,81 +1234,6 @@ fn impl_division(mut num: BigInt, den: &BigInt, mut scale: i64, max_precision: u
12331234
}
12341235

12351236

1236-
impl Rem<BigDecimal> for BigDecimal {
1237-
type Output = BigDecimal;
1238-
1239-
#[inline]
1240-
fn rem(self, other: BigDecimal) -> BigDecimal {
1241-
let scale = cmp::max(self.scale, other.scale);
1242-
1243-
let num = self.take_and_scale(scale).int_val;
1244-
let den = other.take_and_scale(scale).int_val;
1245-
1246-
BigDecimal::new(num % den, scale)
1247-
}
1248-
}
1249-
1250-
impl<'a> Rem<&'a BigDecimal> for BigDecimal {
1251-
type Output = BigDecimal;
1252-
1253-
#[inline]
1254-
fn rem(self, other: &BigDecimal) -> BigDecimal {
1255-
let scale = cmp::max(self.scale, other.scale);
1256-
let num = self.take_and_scale(scale).int_val;
1257-
let den = &other.int_val;
1258-
1259-
let result = if scale == other.scale {
1260-
num % den
1261-
} else {
1262-
num % (den * ten_to_the((scale - other.scale) as u64))
1263-
};
1264-
BigDecimal::new(result, scale)
1265-
}
1266-
}
1267-
impl<'a> Rem<BigDecimal> for &'a BigDecimal {
1268-
type Output = BigDecimal;
1269-
1270-
#[inline]
1271-
fn rem(self, other: BigDecimal) -> BigDecimal {
1272-
let scale = cmp::max(self.scale, other.scale);
1273-
let num = &self.int_val;
1274-
let den = other.take_and_scale(scale).int_val;
1275-
1276-
let result = if scale == self.scale {
1277-
num % den
1278-
} else {
1279-
let scaled_num = num * ten_to_the((scale - self.scale) as u64);
1280-
scaled_num % den
1281-
};
1282-
1283-
BigDecimal::new(result, scale)
1284-
}
1285-
}
1286-
1287-
impl<'a, 'b> Rem<&'b BigDecimal> for &'a BigDecimal {
1288-
type Output = BigDecimal;
1289-
1290-
#[inline]
1291-
fn rem(self, other: &BigDecimal) -> BigDecimal {
1292-
let scale = cmp::max(self.scale, other.scale);
1293-
let num = &self.int_val;
1294-
let den = &other.int_val;
1295-
1296-
let result = match self.scale.cmp(&other.scale) {
1297-
Ordering::Equal => num % den,
1298-
Ordering::Less => {
1299-
let scaled_num = num * ten_to_the((scale - self.scale) as u64);
1300-
scaled_num % den
1301-
}
1302-
Ordering::Greater => {
1303-
let scaled_den = den * ten_to_the((scale - other.scale) as u64);
1304-
num % scaled_den
1305-
}
1306-
};
1307-
BigDecimal::new(result, scale)
1308-
}
1309-
}
1310-
13111237

13121238
impl Signed for BigDecimal {
13131239
#[inline]

0 commit comments

Comments
 (0)