malachite_nz/integer/arithmetic/
abs_diff.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MP Library.
4//
5//      Copyright © 1991-2018, 2020 Free Software Foundation, Inc.
6//
7// This file is part of Malachite.
8//
9// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
10// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
11// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
12
13use crate::integer::Integer;
14use crate::natural::Natural;
15use malachite_base::num::arithmetic::traits::{AbsAssign, AbsDiff, AbsDiffAssign, UnsignedAbs};
16
17impl AbsDiff<Self> for Integer {
18    type Output = Natural;
19
20    /// Computes the absolute value of the difference between two [`Integer`]s, taking both by
21    /// value. A [`Natural`] is returned.
22    ///
23    /// $$
24    /// f(x, y) = |x - y|.
25    /// $$
26    ///
27    /// # Worst-case complexity
28    /// $T(n) = O(n)$
29    ///
30    /// $M(n) = O(1)$
31    ///
32    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
33    ///
34    /// # Examples
35    /// ```
36    /// use malachite_base::num::arithmetic::traits::{AbsDiff, Pow};
37    /// use malachite_base::num::basic::traits::Zero;
38    /// use malachite_nz::integer::Integer;
39    ///
40    /// assert_eq!(Integer::from(123).abs_diff(Integer::ZERO), 123);
41    /// assert_eq!(Integer::ZERO.abs_diff(Integer::from(123)), 123);
42    /// assert_eq!(Integer::from(456).abs_diff(Integer::from(-123)), 579);
43    /// assert_eq!(Integer::from(123).abs_diff(Integer::from(-456)), 579);
44    /// assert_eq!(
45    ///     (Integer::from(10).pow(12) * Integer::from(3)).abs_diff(Integer::from(10).pow(12)),
46    ///     2000000000000u64
47    /// );
48    /// assert_eq!(
49    ///     (-Integer::from(10).pow(12)).abs_diff(-Integer::from(10).pow(12) * Integer::from(3)),
50    ///     2000000000000u64
51    /// );
52    /// ```
53    #[inline]
54    fn abs_diff(self, other: Self) -> Natural {
55        (self - other).unsigned_abs()
56    }
57}
58
59impl AbsDiff<&Self> for Integer {
60    type Output = Natural;
61
62    /// Computes the absolute value of the difference between two [`Integer`]s, taking the first by
63    /// value and the second by reference. A [`Natural`] is returned.
64    ///
65    /// $$
66    /// f(x, y) = |x - y|.
67    /// $$
68    ///
69    /// # Worst-case complexity
70    /// $T(n) = O(n)$
71    ///
72    /// $M(n) = O(1)$
73    ///
74    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
75    /// other.significant_bits())`.
76    ///
77    /// # Examples
78    /// ```
79    /// use malachite_base::num::arithmetic::traits::{AbsDiff, Pow};
80    /// use malachite_base::num::basic::traits::Zero;
81    /// use malachite_nz::integer::Integer;
82    ///
83    /// assert_eq!(Integer::from(123).abs_diff(&Integer::ZERO), 123);
84    /// assert_eq!(Integer::ZERO.abs_diff(&Integer::from(123)), 123);
85    /// assert_eq!(Integer::from(456).abs_diff(&Integer::from(-123)), 579);
86    /// assert_eq!(Integer::from(123).abs_diff(&Integer::from(-456)), 579);
87    /// assert_eq!(
88    ///     (Integer::from(10).pow(12) * Integer::from(3)).abs_diff(&Integer::from(10).pow(12)),
89    ///     2000000000000u64
90    /// );
91    /// assert_eq!(
92    ///     (-Integer::from(10).pow(12)).abs_diff(&(-Integer::from(10).pow(12) * Integer::from(3))),
93    ///     2000000000000u64
94    /// );
95    /// ```
96    #[inline]
97    fn abs_diff(self, other: &Self) -> Natural {
98        (self - other).unsigned_abs()
99    }
100}
101
102impl AbsDiff<Integer> for &Integer {
103    type Output = Natural;
104
105    /// Computes the absolute value of the difference between two [`Integer`]s, taking the first by
106    /// reference and the second by value. A [`Natural`] is returned.
107    ///
108    /// $$
109    /// f(x, y) = |x - y|.
110    /// $$
111    ///
112    /// # Worst-case complexity
113    /// $T(n) = O(n)$
114    ///
115    /// $M(n) = O(n)$
116    ///
117    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
118    /// other.significant_bits())`.
119    ///
120    /// # Examples
121    /// ```
122    /// use malachite_base::num::arithmetic::traits::{AbsDiff, Pow};
123    /// use malachite_base::num::basic::traits::Zero;
124    /// use malachite_nz::integer::Integer;
125    ///
126    /// assert_eq!((&Integer::from(123)).abs_diff(Integer::ZERO), 123);
127    /// assert_eq!((&Integer::ZERO).abs_diff(Integer::from(123)), 123);
128    /// assert_eq!((&Integer::from(456)).abs_diff(Integer::from(-123)), 579);
129    /// assert_eq!((&Integer::from(123)).abs_diff(Integer::from(-456)), 579);
130    /// assert_eq!(
131    ///     (&(Integer::from(10).pow(12) * Integer::from(3))).abs_diff(Integer::from(10).pow(12)),
132    ///     2000000000000u64
133    /// );
134    /// assert_eq!(
135    ///     (&(-Integer::from(10).pow(12))).abs_diff(-Integer::from(10).pow(12) * Integer::from(3)),
136    ///     2000000000000u64
137    /// );
138    /// ```
139    #[inline]
140    fn abs_diff(self, other: Integer) -> Natural {
141        (self - other).unsigned_abs()
142    }
143}
144
145impl AbsDiff<&Integer> for &Integer {
146    type Output = Natural;
147
148    /// Computes the absolute value of the difference between two [`Integer`]s, taking both by
149    /// reference. A [`Natural`] is returned.
150    ///
151    /// $$
152    /// f(x, y) = |x - y|.
153    /// $$
154    ///
155    /// # Worst-case complexity
156    /// $T(n) = O(n)$
157    ///
158    /// $M(n) = O(n)$
159    ///
160    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
161    /// other.significant_bits())`.
162    ///
163    /// # Examples
164    /// ```
165    /// use malachite_base::num::arithmetic::traits::{AbsDiff, Pow};
166    /// use malachite_base::num::basic::traits::Zero;
167    /// use malachite_nz::integer::Integer;
168    ///
169    /// assert_eq!((&Integer::from(123)).abs_diff(&Integer::ZERO), 123);
170    /// assert_eq!((&Integer::ZERO).abs_diff(&Integer::from(123)), 123);
171    /// assert_eq!((&Integer::from(456)).abs_diff(&Integer::from(-123)), 579);
172    /// assert_eq!((&Integer::from(123)).abs_diff(&Integer::from(-456)), 579);
173    /// assert_eq!(
174    ///     (&(Integer::from(10).pow(12) * Integer::from(3))).abs_diff(&Integer::from(10).pow(12)),
175    ///     2000000000000u64
176    /// );
177    /// assert_eq!(
178    ///     (&(-Integer::from(10).pow(12)))
179    ///         .abs_diff(&(-Integer::from(10).pow(12) * Integer::from(3))),
180    ///     2000000000000u64
181    /// );
182    /// ```
183    #[inline]
184    fn abs_diff(self, other: &Integer) -> Natural {
185        (self - other).unsigned_abs()
186    }
187}
188
189impl AbsDiffAssign<Self> for Integer {
190    /// Subtracts an [`Integer`] by another [`Integer`] in place and takes the absolute value,
191    /// taking the [`Integer`] on the right-hand side by value.
192    ///
193    /// $$
194    /// x \gets |x - y|.
195    /// $$
196    ///
197    /// # Worst-case complexity
198    /// $T(n) = O(n)$
199    ///
200    /// $M(n) = O(1)$
201    ///
202    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
203    /// other.significant_bits())`.
204    ///
205    /// # Panics
206    /// Panics if `other` is greater than `self`.
207    ///
208    /// # Examples
209    /// ```
210    /// use malachite_base::num::arithmetic::traits::{AbsDiffAssign, Pow};
211    /// use malachite_base::num::basic::traits::Zero;
212    /// use malachite_nz::integer::Integer;
213    ///
214    /// let mut x = Integer::from(123);
215    /// x.abs_diff_assign(Integer::ZERO);
216    /// assert_eq!(x, 123);
217    ///
218    /// let mut x = Integer::ZERO;
219    /// x.abs_diff_assign(Integer::from(123));
220    /// assert_eq!(x, 123);
221    ///
222    /// let mut x = Integer::from(456);
223    /// x.abs_diff_assign(Integer::from(-123));
224    /// assert_eq!(x, 579);
225    ///
226    /// let mut x = Integer::from(-123);
227    /// x.abs_diff_assign(Integer::from(456));
228    /// assert_eq!(x, 579);
229    ///
230    /// let mut x = Integer::from(10).pow(12) * Integer::from(3);
231    /// x.abs_diff_assign(Integer::from(10u32).pow(12));
232    /// assert_eq!(x, 2000000000000u64);
233    ///
234    /// let mut x = -Integer::from(10u32).pow(12);
235    /// x.abs_diff_assign(-(Integer::from(10).pow(12) * Integer::from(3)));
236    /// assert_eq!(x, 2000000000000u64);
237    /// ```
238    #[inline]
239    fn abs_diff_assign(&mut self, other: Self) {
240        *self -= other;
241        self.abs_assign();
242    }
243}
244
245impl<'a> AbsDiffAssign<&'a Self> for Integer {
246    /// Subtracts an [`Integer`] by another [`Integer`] in place and takes the absolute value,
247    /// taking the [`Integer`] on the right-hand side by reference.
248    ///
249    /// $$
250    /// x \gets |x - y|.
251    /// $$
252    ///
253    /// # Worst-case complexity
254    /// $T(n) = O(n)$
255    ///
256    /// $M(n) = O(1)$
257    ///
258    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
259    /// other.significant_bits())`.
260    ///
261    /// # Panics
262    /// Panics if `other` is greater than `self`.
263    ///
264    /// # Examples
265    /// ```
266    /// use malachite_base::num::arithmetic::traits::{AbsDiffAssign, Pow};
267    /// use malachite_base::num::basic::traits::Zero;
268    /// use malachite_nz::integer::Integer;
269    ///
270    /// let mut x = Integer::from(123);
271    /// x.abs_diff_assign(&Integer::ZERO);
272    /// assert_eq!(x, 123);
273    ///
274    /// let mut x = Integer::ZERO;
275    /// x.abs_diff_assign(&Integer::from(123));
276    /// assert_eq!(x, 123);
277    ///
278    /// let mut x = Integer::from(456);
279    /// x.abs_diff_assign(&Integer::from(-123));
280    /// assert_eq!(x, 579);
281    ///
282    /// let mut x = Integer::from(-123);
283    /// x.abs_diff_assign(&Integer::from(456));
284    /// assert_eq!(x, 579);
285    ///
286    /// let mut x = Integer::from(10).pow(12) * Integer::from(3);
287    /// x.abs_diff_assign(&Integer::from(10u32).pow(12));
288    /// assert_eq!(x, 2000000000000u64);
289    ///
290    /// let mut x = -Integer::from(10u32).pow(12);
291    /// x.abs_diff_assign(&(-(Integer::from(10).pow(12) * Integer::from(3))));
292    /// assert_eq!(x, 2000000000000u64);
293    /// ```
294    #[inline]
295    fn abs_diff_assign(&mut self, other: &'a Self) {
296        *self -= other;
297        self.abs_assign();
298    }
299}