1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::Arbi;
impl Arbi {
/// Computes the absolute difference between `self` and `other`.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// assert_eq!(Arbi::from(8000).abs_diff(Arbi::from(10000)), 2000);
/// assert_eq!(Arbi::from(-10000).abs_diff(Arbi::from(8000)), 18000);
/// assert_eq!(Arbi::from(-10000).abs_diff(Arbi::from(-11000)), 1000);
/// ```
///
/// # Complexity
/// \\( O(n) \\)
pub fn abs_diff(self, other: Self) -> Self {
let mut ret = if self.size() > other.size() {
self - other
} else {
other - self
};
ret.abs_mut();
ret
}
/// Computes the absolute difference between `self` and `other`.
///
/// # Examples
/// ```
/// use arbi::{Arbi, Assign};
///
/// let mut a = Arbi::from(8000);
///
/// let b = Arbi::from(10000);
/// a.abs_diff_mut(&b);
/// assert_eq!(a, 2000);
///
/// a.assign(-10000);
/// let b = Arbi::from(8000);
/// a.abs_diff_mut(&b);
/// assert_eq!(a, 18000);
///
/// a.assign(-10000);
/// let b = Arbi::from(-11000);
/// a.abs_diff_mut(&b);
/// assert_eq!(a, 1000);
/// ```
///
/// # Complexity
/// \\( O(n) \\)
pub fn abs_diff_mut(&mut self, other: &Self) {
*self -= other;
self.abs_mut();
}
/// Computes the absolute difference between `self` and `other`.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// assert_eq!(Arbi::from(8000).abs_diff_ref(&Arbi::from(10000)), 2000);
/// assert_eq!(Arbi::from(-10000).abs_diff_ref(&Arbi::from(8000)), 18000);
/// assert_eq!(Arbi::from(-10000).abs_diff_ref(&Arbi::from(-11000)), 1000);
/// ```
///
/// # Complexity
/// \\( O(n) \\)
pub fn abs_diff_ref(&self, other: &Self) -> Self {
let clone: Arbi;
let mut ret: Arbi;
if self.size() > other.size() {
clone = self.clone();
ret = clone - other;
} else {
clone = other.clone();
ret = clone - self;
}
ret.abs_mut();
ret
}
}