malachite_base/num/comparison/traits.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 core::cmp::Ordering::{self, *};
10
11/// Determines equality between the absolute values of two numbers.
12pub trait EqAbs<Rhs: ?Sized = Self> {
13 /// Compares the absolute values of two numbers for equality, taking both by reference.
14 fn eq_abs(&self, other: &Rhs) -> bool;
15
16 /// Compares the absolute values of two numbers for inequality, taking both by reference.
17 ///
18 /// # Worst-case complexity
19 /// Same as the time and additional memory complexity of [`eq_abs`](Self::eq_abs).
20 #[inline]
21 fn ne_abs(&self, other: &Rhs) -> bool {
22 !self.eq_abs(other)
23 }
24}
25
26/// Determines equality between the absolute values of two numbers, where some pairs of numbers may
27/// not be comparable.
28pub trait PartialOrdAbs<Rhs: ?Sized = Self> {
29 /// Compares the absolute values of two numbers, taking both by reference.
30 ///
31 /// If the two values are not comparable, `None` is returned.
32 fn partial_cmp_abs(&self, other: &Rhs) -> Option<Ordering>;
33
34 /// Determines whether the absolute value of one number is less than the absolute value of
35 /// another.
36 ///
37 /// # Worst-case complexity
38 /// Same as the time and additional memory complexity of
39 /// [`partial_cmp_abs`](Self::partial_cmp_abs).
40 #[inline]
41 fn lt_abs(&self, other: &Rhs) -> bool {
42 matches!(self.partial_cmp_abs(other), Some(Less))
43 }
44
45 /// Determines whether the absolute value of one number is less than or equal to the absolute
46 /// value of another.
47 ///
48 /// # Worst-case complexity
49 /// Same as the time and additional memory complexity of
50 /// [`partial_cmp_abs`](Self::partial_cmp_abs).
51 #[inline]
52 fn le_abs(&self, other: &Rhs) -> bool {
53 matches!(self.partial_cmp_abs(other), Some(Less | Equal))
54 }
55
56 /// Determines whether the absolute value of one number is greater than the absolute value of
57 /// another.
58 ///
59 /// # Worst-case complexity
60 /// Same as the time and additional memory complexity of
61 /// [`partial_cmp_abs`](Self::partial_cmp_abs).
62 #[inline]
63 fn gt_abs(&self, other: &Rhs) -> bool {
64 matches!(self.partial_cmp_abs(other), Some(Greater))
65 }
66
67 /// Determines whether the absolute value of one number is greater than or equal to the absolute
68 /// value of another.
69 ///
70 /// # Worst-case complexity
71 /// Same as the time and additional memory complexity of
72 /// [`partial_cmp_abs`](Self::partial_cmp_abs).
73 #[inline]
74 fn ge_abs(&self, other: &Rhs) -> bool {
75 matches!(self.partial_cmp_abs(other), Some(Greater | Equal))
76 }
77}
78
79/// Compares a number with twice another number, without computing the doubled value.
80///
81/// If the two values are not comparable, `None` is returned.
82pub trait PartialOrdDouble<Rhs: ?Sized = Self> {
83 /// Compares a number with twice another number, taking both by reference.
84 fn partial_cmp_double(&self, other: &Rhs) -> Option<Ordering>;
85}
86
87/// Compares the absolute value of a number with twice the absolute value of another number, without
88/// computing the doubled value.
89///
90/// If the two values are not comparable, `None` is returned.
91pub trait PartialOrdAbsDouble<Rhs: ?Sized = Self> {
92 /// Compares the absolute value of a number with twice the absolute value of another number,
93 /// taking both by reference.
94 fn partial_cmp_abs_double(&self, other: &Rhs) -> Option<Ordering>;
95}
96
97/// Compares a number with twice another number, without computing the doubled value.
98///
99/// This is the shape of a round-to-nearest decision, where a remainder is weighed against half a
100/// divisor, so it is worth doing without the allocation or the overflow that doubling would cost.
101pub trait OrdDouble<Rhs: ?Sized = Self>: PartialOrdDouble<Rhs> {
102 /// Compares a number with twice another number, taking both by reference.
103 fn cmp_double(&self, other: &Rhs) -> Ordering;
104}
105
106/// Compares the absolute value of a number with twice the absolute value of another number, without
107/// computing the doubled value.
108///
109/// This is the shape of a round-to-nearest decision, where a remainder is weighed against half a
110/// divisor, so it is worth doing without the allocation or the overflow that doubling would cost.
111pub trait OrdAbsDouble<Rhs: ?Sized = Self>: PartialOrdAbsDouble<Rhs> {
112 /// Compares the absolute value of a number with twice the absolute value of another number,
113 /// taking both by reference.
114 fn cmp_abs_double(&self, other: &Rhs) -> Ordering;
115}
116
117/// Compares the product of two numbers with a third number, without computing the product unless it
118/// is needed.
119///
120/// `T::cmp_product(&a, &b, &c)` compares $ab$ with $c$. Most comparisons are decided by cheap
121/// checks, such as comparing bit lengths, and the product is computed only when they cannot decide.
122pub trait OrdProduct {
123 fn cmp_product(a: &Self, b: &Self, c: &Self) -> Ordering;
124}
125
126/// Compares the product of two numbers with the negative of a third number, without computing the
127/// product unless it is needed and without computing the negative.
128///
129/// `T::cmp_product_neg(&a, &b, &c)` compares $ab$ with $-c$; equivalently, it gives the sign of $ab
130/// + c$.
131pub trait OrdProductNeg {
132 fn cmp_product_neg(a: &Self, b: &Self, c: &Self) -> Ordering;
133}
134
135/// Compares the products of two pairs of numbers, without computing the products unless they are
136/// needed.
137///
138/// `T::cmp_products(&a, &b, &c, &d)` compares $ab$ with $cd$. Most comparisons are decided by cheap
139/// checks, such as comparing the factors pairwise or comparing bit lengths, and the products are
140/// computed only when they cannot decide.
141pub trait OrdProducts {
142 fn cmp_products(a: &Self, b: &Self, c: &Self, d: &Self) -> Ordering;
143}
144
145/// Compares the product of two numbers with the negative of the product of two others, without
146/// computing the products unless they are needed and without computing the negative.
147///
148/// `T::cmp_products_neg(&a, &b, &c, &d)` compares $ab$ with $-cd$; equivalently, it gives the sign
149/// of $ab + cd$.
150pub trait OrdProductsNeg {
151 fn cmp_products_neg(a: &Self, b: &Self, c: &Self, d: &Self) -> Ordering;
152}
153
154/// Compares the absolute values of two numbers.
155pub trait OrdAbs: Eq + PartialOrdAbs<Self> {
156 fn cmp_abs(&self, other: &Self) -> Ordering;
157}