diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2013-05-07 14:36:32 +1000 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2013-05-07 19:16:03 +1000 |
| commit | a9ac2b95f485c31e40273dee7f09dcfff8340777 (patch) | |
| tree | 1df12ee707e16ff8cda02af66ea929cc51ccfc15 /src/libstd/num | |
| parent | 314b485c9c480c4a7f47c28716e629d642e2d412 (diff) | |
| download | rust-a9ac2b95f485c31e40273dee7f09dcfff8340777.tar.gz rust-a9ac2b95f485c31e40273dee7f09dcfff8340777.zip | |
Add abs_sub method to Signed trait
Diffstat (limited to 'src/libstd/num')
| -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)); |
