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::{Zero, One, Signed};
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
13 fn div_floor(&self, other:&Self) -> Self{
14 let quo: BigInt = self / other;
15 if self.sign() == Sign::Minus {
16 return quo - BigInt::one()
17 }
18 return quo
19 }
20
21 fn div_round(&self, other:&Self) -> Self{
22 let (quo, mut rem) = self.div_rem(other);
23 rem <<= 1;
24 if rem != BigInt::zero() && &rem.abs() > other{
25 if self.sign() == other.sign(){
26 return quo + BigInt::one()
27 }else{
28 return quo - BigInt::one()
29 }
30 }
31 return quo
32 }
33}
34