Scalar division of a BigUint by a BigDigit

A BigUint can be divided by a BigDigit - this is one of several
operations being implemented to allow scalar operations on BigInt and
BigUint across the board.
This commit is contained in:
Sam Cappleman-Lynes 2017-06-28 16:59:35 +01:00
parent 530e2f6022
commit 784d26bbf8
2 changed files with 57 additions and 2 deletions

View File

@ -479,7 +479,17 @@ impl<'a, 'b> Div<&'b BigUint> for &'a BigUint {
#[inline]
fn div(self, other: &BigUint) -> BigUint {
let (q, _) = self.div_rem(other);
return q;
q
}
}
impl Div<BigDigit> for BigUint {
type Output = BigUint;
#[inline]
fn div(self, other: BigDigit) -> BigUint {
let (q, _) = div_rem_digit(self, other);
q
}
}
@ -491,7 +501,17 @@ impl<'a, 'b> Rem<&'b BigUint> for &'a BigUint {
#[inline]
fn rem(self, other: &BigUint) -> BigUint {
let (_, r) = self.div_rem(other);
return r;
r
}
}
impl Rem<BigDigit> for BigUint {
type Output = BigDigit;
#[inline]
fn rem(self, other: BigDigit) -> BigDigit {
let (_, r) = div_rem_digit(self, other);
r
}
}

View File

@ -866,6 +866,41 @@ fn test_div_rem() {
}
}
#[test]
fn test_scalar_div_rem() {
for elm in MUL_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let c = BigUint::from_slice(c_vec);
if a_vec.len() == 1 && a_vec[0] != 0 {
let a = a_vec[0];
let b = BigUint::from_slice(b_vec);
assert!(c.clone() / a == b);
assert!(c.clone() % a == Zero::zero());
}
if b_vec.len() == 1 && b_vec[0] != 0 {
let a = BigUint::from_slice(a_vec);
let b = b_vec[0];
assert!(c.clone() / b == a);
assert!(c.clone() % b == Zero::zero());
}
}
for elm in DIV_REM_QUADRUPLES.iter() {
let (a_vec, b_vec, c_vec, d_vec) = *elm;
let a = BigUint::from_slice(a_vec);
let c = BigUint::from_slice(c_vec);
if b_vec.len() == 1 && b_vec[0] != 0 {
let b = b_vec[0];
let d = d_vec[0];
assert!(a.clone() / b == c);
assert!(a.clone() % b == d);
}
}
}
#[test]
fn test_checked_add() {
for elm in SUM_TRIPLES.iter() {