Partially implement subtraction
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-03-10 20:46:51 -04:00
parent d0ac326944
commit ae77ede1c0

View File

@ -137,7 +137,33 @@ impl Sub for BigInt {
type Output = Self; type Output = Self;
fn sub(self, rhs: Self) -> Self::Output { fn sub(self, rhs: Self) -> Self::Output {
todo!() let mut out = BigInt::new_empty();
let u_digits = self.inner.len();
let v_digits = rhs.inner.len();
let digits = if v_digits > u_digits {
v_digits
} else {
u_digits
};
let mut borrow = 0usize;
for i in 0..digits {
let a = *self.inner.get(i).unwrap_or(&0usize);
let b = *rhs.inner.get(i).unwrap_or(&0usize);
if a > b {
out.inner.push(a - b - borrow);
borrow = 0;
} else if a < b {
todo!();
} else {
todo!();
}
}
out
} }
} }
@ -335,6 +361,18 @@ mod tests {
assert_eq!(sum.inner[1], 1usize); assert_eq!(sum.inner[1], 1usize);
} }
#[test]
fn test_sub() {
let a = BigInt::from(core::usize::MAX);
let b = BigInt::from(core::u16::MAX);
let diff = a - b;
assert_eq!(
diff.inner[0],
core::usize::MAX - core::u16::MAX as usize
);
}
#[test] #[test]
fn test_not() { fn test_not() {
let a = BigInt::from(0u8); let a = BigInt::from(0u8);