This commit is contained in:
Jean-Philippe Bossuat
2025-01-08 11:07:04 +01:00
parent bdd57b91ed
commit 160e7a33da
9 changed files with 383 additions and 207 deletions

View File

@@ -1,34 +1,32 @@
use num_bigint::BigInt;
use num_bigint::Sign;
use num_integer::Integer;
use num_traits::{Zero, One, Signed};
use num_traits::{One, Signed, Zero};
pub trait Div{
pub trait Div {
fn div_floor(&self, other: &Self) -> Self;
fn div_round(&self, other: &Self) -> Self;
}
impl Div for BigInt{
fn div_floor(&self, other:&Self) -> Self{
impl Div for BigInt {
fn div_floor(&self, other: &Self) -> Self {
let quo: BigInt = self / other;
if self.sign() == Sign::Minus {
return quo - BigInt::one()
return quo - BigInt::one();
}
return quo
return quo;
}
fn div_round(&self, other:&Self) -> Self{
fn div_round(&self, other: &Self) -> Self {
let (quo, mut rem) = self.div_rem(other);
rem <<= 1;
if rem != BigInt::zero() && &rem.abs() > other{
if self.sign() == other.sign(){
return quo + BigInt::one()
}else{
return quo - BigInt::one()
if rem != BigInt::zero() && &rem.abs() > other {
if self.sign() == other.sign() {
return quo + BigInt::one();
} else {
return quo - BigInt::one();
}
}
return quo
return quo;
}
}