poulpy/rns/src/num_bigint.rs
rns/src/num_bigint.rsRaw
1use num_bigint::BigInt;
2use num_integer::Integer;
3use num_traits::{One, Signed, Zero};
4
5pub trait Div {
6 fn div_floor(&self, other: &Self) -> Self;
7 fn div_round(&self, other: &Self) -> Self;
8}
9
10impl Div for BigInt {
11 fn div_floor(&self, other: &Self) -> Self {
12 let quo: BigInt = self / other;
13 if self.sign() != other.sign() {
14 return quo - BigInt::one();
15 }
16 return quo;
17 }
18
19 fn div_round(&self, other: &Self) -> Self {
20 let (quo, mut rem) = self.div_rem(other);
21 rem <<= 1;
22 if rem != BigInt::zero() && &rem.abs() > other {
23 if self.sign() == other.sign() {
24 return quo + BigInt::one();
25 } else {
26 return quo - BigInt::one();
27 }
28 }
29 return quo;
30 }
31}