Add abs_sub method to Signed trait

This commit is contained in:
Brendan Zabarauskas 2013-05-07 14:36:32 +10:00
parent 314b485c9c
commit a9ac2b95f4
6 changed files with 140 additions and 5 deletions

View file

@ -831,6 +831,11 @@ impl Signed for BigInt {
}
}
#[inline(always)]
fn abs_sub(&self, other: &BigInt) -> BigInt {
if *self <= *other { Zero::zero() } else { *self - *other }
}
#[inline(always)]
fn signum(&self) -> BigInt {
match self.sign {
@ -1920,6 +1925,15 @@ mod bigint_tests {
check(11, 5, 55);
}
#[test]
fn test_abs_sub() {
assert_eq!((-One::one::<BigInt>()).abs_sub(&One::one()), Zero::zero());
assert_eq!(One::one::<BigInt>().abs_sub(&One::one()), Zero::zero());
assert_eq!(One::one::<BigInt>().abs_sub(&Zero::zero()), One::one());
assert_eq!(One::one::<BigInt>().abs_sub(&-One::one::<BigInt>()),
IntConvertible::from_int(2));
}
#[test]
fn test_to_str_radix() {
fn check(n: int, ans: &str) {