From 74744095f1666e08441b3aeb1d16ebbdf3eff600 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 11 Mar 2020 14:01:45 -0400 Subject: [PATCH] Fix zero subtraction --- src/bigint.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index c47bf88..1e6cf67 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -175,15 +175,9 @@ impl Sub for BigInt { let a = *self.inner.get(i).unwrap_or(&0usize); let b = *rhs.inner.get(i).unwrap_or(&0usize); - if a > 0 && (a - borrow) >= b { + if a >= borrow && (a - borrow) >= b { let res = a - b - borrow; - // Don't add an extra zero if you borrowed everything - // from the most significant digit - if res == 0 && digits > 0 && i == digits -1 { - return out; - } - out.inner.push(res); borrow = 0; } else { @@ -462,9 +456,9 @@ mod tests { let a = BigInt::new(); let b = BigInt::new(); - // let c = a.clone() - b.clone(); - // assert_eq!(a.clone(), b.clone()); - // assert_eq!(c, a.clone()); + let c = a.clone() - b.clone(); + assert_eq!(a.clone(), b.clone()); + assert_eq!(c, a.clone()); let c = a.clone() + b.clone(); assert_eq!(a.clone(), b.clone());