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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::integer::Integer;
use crate::natural::Natural;
use core::cmp::Ordering::Less;
use malachite_base::num::arithmetic::traits::{BalancedMod, Mod};
use malachite_base::num::comparison::traits::OrdDouble;
// The balanced remainder is the ordinary one when it is at most half the modulus, and that
// remainder less the modulus otherwise. A remainder of exactly half the modulus stays positive,
// which is what puts the endpoint at the top of the range rather than the bottom.
fn balanced_mod_helper(x: &Natural, m: &Natural) -> Integer {
let r = x.mod_op(m);
// `r <= m >> 1` is exactly `2r <= m`: for an integer `r`, `r <= floor(x)` iff `r <= x`.
// Phrasing it as the latter lets `cmp_double` answer it without building either value.
if m.cmp_double(&r) == Less {
Integer::from(r) - Integer::from(m)
} else {
Integer::from(r)
}
}
impl BalancedMod<Self> for Natural {
type Output = Integer;
/// Divides a [`Natural`] by another [`Natural`], returning the balanced remainder: the
/// representative of `self` modulo `other` that is closest to zero. Both [`Natural`]s are taken
/// by value.
///
/// The remainder $r$ satisfies $-y/2 < r \leq y/2$ and $r \equiv x \bmod y$, which determine it
/// uniquely. A remainder of exactly $y/2$ is positive, so the result may be negative and is
/// returned as an [`Integer`].
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Panics
/// Panics if `other` is zero.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::BalancedMod;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(Natural::from(23u32).balanced_mod(Natural::from(10u32)), 3);
/// // 7 is more than half of 10, so the representative closest to zero is negative
/// assert_eq!(Natural::from(27u32).balanced_mod(Natural::from(10u32)), -3);
/// // exactly half the modulus stays positive
/// assert_eq!(Natural::from(25u32).balanced_mod(Natural::from(10u32)), 5);
/// ```
#[inline]
fn balanced_mod(self, other: Self) -> Integer {
balanced_mod_helper(&self, &other)
}
}
impl BalancedMod<&Self> for Natural {
type Output = Integer;
/// Divides a [`Natural`] by another [`Natural`], returning the balanced remainder: the
/// representative of `self` modulo `other` that is closest to zero. The first [`Natural`] is
/// taken by value and the second by reference.
///
/// See the [`BalancedMod`] documentation for details.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Panics
/// Panics if `other` is zero.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::BalancedMod;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(Natural::from(27u32).balanced_mod(&Natural::from(10u32)), -3);
/// ```
#[inline]
fn balanced_mod(self, other: &Self) -> Integer {
balanced_mod_helper(&self, other)
}
}
impl BalancedMod<Natural> for &Natural {
type Output = Integer;
/// Divides a [`Natural`] by another [`Natural`], returning the balanced remainder: the
/// representative of `self` modulo `other` that is closest to zero. The first [`Natural`] is
/// taken by reference and the second by value.
///
/// See the [`BalancedMod`] documentation for details.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Panics
/// Panics if `other` is zero.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::BalancedMod;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// (&Natural::from(27u32)).balanced_mod(Natural::from(10u32)),
/// -3
/// );
/// ```
#[inline]
fn balanced_mod(self, other: Natural) -> Integer {
balanced_mod_helper(self, &other)
}
}
impl BalancedMod<&Natural> for &Natural {
type Output = Integer;
/// Divides a [`Natural`] by another [`Natural`], returning the balanced remainder: the
/// representative of `self` modulo `other` that is closest to zero. Both [`Natural`]s are taken
/// by reference.
///
/// See the [`BalancedMod`] documentation for details.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Panics
/// Panics if `other` is zero.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::BalancedMod;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// (&Natural::from(27u32)).balanced_mod(&Natural::from(10u32)),
/// -3
/// );
/// ```
#[inline]
fn balanced_mod(self, other: &Natural) -> Integer {
balanced_mod_helper(self, other)
}
}