diff options
Diffstat (limited to 'src/libstd/num/bigint.rs')
| -rw-r--r-- | src/libstd/num/bigint.rs | 14 | 
1 files changed, 14 insertions, 0 deletions
| diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs index cd347098e25..a5cf929ed93 100644 --- a/src/libstd/num/bigint.rs +++ b/src/libstd/num/bigint.rs @@ -832,6 +832,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 { Plus => BigInt::from_biguint(Plus, One::one()), @@ -1921,6 +1926,15 @@ mod bigint_tests { } #[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) { assert!(ans == IntConvertible::from_int::<BigInt>(n).to_str_radix(10)); | 
