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