Handle signs and carry overflow addition for multiplication
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
This commit is contained in:
parent
faa1f548a0
commit
b8ab76bbf6
@ -260,19 +260,14 @@ impl Mul for BigInt {
|
|||||||
if overflowed {
|
if overflowed {
|
||||||
todo!()
|
todo!()
|
||||||
} else {
|
} else {
|
||||||
match res.checked_add(carry) {
|
let (res, overflowed) = res.overflowing_add(carry);
|
||||||
Some(res) => {
|
|
||||||
out.inner.push(res);
|
out.inner.push(res);
|
||||||
carry = 0;
|
carry = if overflowed { 1 } else { 0 };
|
||||||
}
|
|
||||||
None => {
|
|
||||||
// Well, we have to deal with overflow again
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out.sign = Self::get_sign(self, rhs, FracOp::Other);
|
||||||
out.shrink_to_fit();
|
out.shrink_to_fit();
|
||||||
|
|
||||||
out
|
out
|
||||||
@ -568,6 +563,33 @@ mod tests {
|
|||||||
assert_eq!(product.inner[0], 65536usize * 4);
|
assert_eq!(product.inner[0], 65536usize * 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mul_signs() {
|
||||||
|
let a = BigInt::from(2);
|
||||||
|
let b = BigInt::from(-2);
|
||||||
|
let product = a * b;
|
||||||
|
assert_eq!(product.inner[0], 4usize);
|
||||||
|
assert_eq!(product.sign, Negative);
|
||||||
|
|
||||||
|
let a = -BigInt::from(2);
|
||||||
|
let b = BigInt::from(2);
|
||||||
|
let product = a * b;
|
||||||
|
assert_eq!(product.inner[0], 4usize);
|
||||||
|
assert_eq!(product.sign, Negative);
|
||||||
|
|
||||||
|
let a = BigInt::from(-2);
|
||||||
|
let b = BigInt::from(-2);
|
||||||
|
let product = a * b;
|
||||||
|
assert_eq!(product.inner[0], 4usize);
|
||||||
|
assert_eq!(product.sign, Positive);
|
||||||
|
|
||||||
|
let a = BigInt::from(2);
|
||||||
|
let b = BigInt::from(2);
|
||||||
|
let product = a * b;
|
||||||
|
assert_eq!(product.inner[0], 4usize);
|
||||||
|
assert_eq!(product.sign, Positive);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_mul_overflow() {
|
fn test_mul_overflow() {
|
||||||
|
Loading…
Reference in New Issue
Block a user