malachite_nz/integer/comparison/cmp_abs.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use core::cmp::Ordering;
11use malachite_base::num::comparison::traits::{
12 OrdAbs, OrdAbsDouble, OrdDouble, PartialOrdAbs, PartialOrdAbsDouble,
13};
14
15impl PartialOrdAbs for Integer {
16 /// Compares the absolute values of two [`Integer`]s.
17 ///
18 /// See the documentation for the [`OrdAbs`] implementation.
19 #[inline]
20 fn partial_cmp_abs(&self, other: &Self) -> Option<Ordering> {
21 Some(self.cmp_abs(other))
22 }
23}
24
25impl OrdAbs for Integer {
26 /// Compares the absolute values of two [`Integer`]s.
27 ///
28 /// # Worst-case complexity
29 /// $T(n) = O(n)$
30 ///
31 /// $M(n) = O(1)$
32 ///
33 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
34 /// other.significant_bits())`.
35 ///
36 /// # Examples
37 /// ```
38 /// use malachite_base::num::comparison::traits::PartialOrdAbs;
39 /// use malachite_nz::integer::Integer;
40 ///
41 /// assert!(Integer::from(-123).lt_abs(&Integer::from(-124)));
42 /// assert!(Integer::from(-123).le_abs(&Integer::from(-124)));
43 /// assert!(Integer::from(-124).gt_abs(&Integer::from(-123)));
44 /// assert!(Integer::from(-124).ge_abs(&Integer::from(-123)));
45 /// ```
46 #[inline]
47 fn cmp_abs(&self, other: &Self) -> Ordering {
48 self.abs.cmp(&other.abs)
49 }
50}
51
52impl OrdAbsDouble for Integer {
53 /// Compares the absolute value of an [`Integer`] with twice the absolute value of another
54 /// [`Integer`].
55 ///
56 /// The doubling is not actually performed, so no memory is allocated. This is the shape of a
57 /// round-to-nearest decision, where a remainder is weighed against half a divisor.
58 ///
59 /// $$
60 /// f(x, y) = \operatorname{cmp}(|x|, 2|y|).
61 /// $$
62 ///
63 /// # Worst-case complexity
64 /// $T(n) = O(n)$
65 ///
66 /// $M(n) = O(1)$
67 ///
68 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
69 ///
70 /// # Examples
71 /// ```
72 /// use malachite_base::num::basic::traits::Two;
73 /// use malachite_base::num::comparison::traits::OrdAbsDouble;
74 /// use malachite_nz::integer::Integer;
75 /// use std::cmp::Ordering::*;
76 ///
77 /// assert_eq!(Integer::from(4).cmp_abs_double(&Integer::TWO), Equal);
78 /// assert_eq!(Integer::from(-4).cmp_abs_double(&Integer::from(-2)), Equal);
79 /// assert_eq!(Integer::from(3).cmp_abs_double(&Integer::from(-2)), Less);
80 /// assert_eq!(Integer::from(-5).cmp_abs_double(&Integer::TWO), Greater);
81 /// ```
82 #[inline]
83 fn cmp_abs_double(&self, other: &Self) -> Ordering {
84 self.unsigned_abs_ref().cmp_double(other.unsigned_abs_ref())
85 }
86}
87
88impl PartialOrdAbsDouble for Integer {
89 /// Compares the absolute value of an [`Integer`] with twice the absolute value of another
90 /// [`Integer`].
91 ///
92 /// See the documentation for the [`OrdAbsDouble`] implementation.
93 #[inline]
94 fn partial_cmp_abs_double(&self, other: &Self) -> Option<Ordering> {
95 Some(self.cmp_abs_double(other))
96 }
97}