malachite_nz/integer/arithmetic/div_exact.rs
1// Copyright © 2025 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 malachite_base::num::arithmetic::traits::{DivExact, DivExactAssign};
11
12impl DivExact<Self> for Integer {
13 type Output = Self;
14
15 /// Divides an [`Integer`] by another [`Integer`], taking both by value. The first [`Integer`]
16 /// must be exactly divisible by the second. If it isn't, this function may panic or return a
17 /// meaningless result.
18 ///
19 /// $$
20 /// f(x, y) = \frac{x}{y}.
21 /// $$
22 ///
23 /// If you are unsure whether the division will be exact, use `self / other` instead. If you're
24 /// unsure and you want to know, use `self.div_mod(other)` and check whether the remainder is
25 /// zero. If you want a function that panics if the division is not exact, use
26 /// `self.div_round(other, Exact)`.
27 ///
28 /// # Worst-case complexity
29 /// $T(n) = O(n \log n \log \log n)$
30 ///
31 /// $M(n) = O(n \log n)$
32 ///
33 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
34 ///
35 /// # Panics
36 /// Panics if `other` is zero. May panic if `self` is not divisible by `other`.
37 ///
38 /// # Examples
39 /// ```
40 /// use core::str::FromStr;
41 /// use malachite_base::num::arithmetic::traits::DivExact;
42 /// use malachite_nz::integer::Integer;
43 ///
44 /// // -123 * 456 = -56088
45 /// assert_eq!(Integer::from(-56088).div_exact(Integer::from(456)), -123);
46 ///
47 /// // -123456789000 * -987654321000 = 121932631112635269000000
48 /// assert_eq!(
49 /// Integer::from_str("121932631112635269000000")
50 /// .unwrap()
51 /// .div_exact(Integer::from_str("-987654321000").unwrap()),
52 /// -123456789000i64
53 /// );
54 /// ```
55 #[inline]
56 fn div_exact(mut self, other: Self) -> Self {
57 self.div_exact_assign(other);
58 self
59 }
60}
61
62impl DivExact<&Self> for Integer {
63 type Output = Self;
64
65 /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
66 /// reference. The first [`Integer`] must be exactly divisible by the second. If it isn't, this
67 /// function may panic or return a meaningless result.
68 ///
69 /// $$
70 /// f(x, y) = \frac{x}{y}.
71 /// $$
72 ///
73 /// If you are unsure whether the division will be exact, use `self / &other` instead. If you're
74 /// unsure and you want to know, use `self.div_mod(&other)` and check whether the remainder is
75 /// zero. If you want a function that panics if the division is not exact, use
76 /// `self.div_round(&other, Exact)`.
77 ///
78 /// # Worst-case complexity
79 /// $T(n) = O(n \log n \log \log n)$
80 ///
81 /// $M(n) = O(n \log n)$
82 ///
83 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
84 ///
85 /// # Panics
86 /// Panics if `other` is zero. May panic if `self` is not divisible by `other`.
87 ///
88 /// # Examples
89 /// ```
90 /// use core::str::FromStr;
91 /// use malachite_base::num::arithmetic::traits::DivExact;
92 /// use malachite_nz::integer::Integer;
93 ///
94 /// // -123 * 456 = -56088
95 /// assert_eq!(Integer::from(-56088).div_exact(&Integer::from(456)), -123);
96 ///
97 /// // -123456789000 * -987654321000 = 121932631112635269000000
98 /// assert_eq!(
99 /// Integer::from_str("121932631112635269000000")
100 /// .unwrap()
101 /// .div_exact(&Integer::from_str("-987654321000").unwrap()),
102 /// -123456789000i64
103 /// );
104 /// ```
105 #[inline]
106 fn div_exact(mut self, other: &Self) -> Self {
107 self.div_exact_assign(other);
108 self
109 }
110}
111
112impl DivExact<Integer> for &Integer {
113 type Output = Integer;
114
115 /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
116 /// by value. The first [`Integer`] must be exactly divisible by the second. If it isn't, this
117 /// function may panic or return a meaningless result.
118 ///
119 /// $$
120 /// f(x, y) = \frac{x}{y}.
121 /// $$
122 ///
123 /// If you are unsure whether the division will be exact, use `&self / other` instead. If you're
124 /// unsure and you want to know, use `self.div_mod(other)` and check whether the remainder is
125 /// zero. If you want a function that panics if the division is not exact, use
126 /// `(&self).div_round(other, Exact)`.
127 ///
128 /// # Worst-case complexity
129 /// $T(n) = O(n \log n \log \log n)$
130 ///
131 /// $M(n) = O(n \log n)$
132 ///
133 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
134 ///
135 /// # Panics
136 /// Panics if `other` is zero. May panic if `self` is not divisible by `other`.
137 ///
138 /// # Examples
139 /// ```
140 /// use core::str::FromStr;
141 /// use malachite_base::num::arithmetic::traits::DivExact;
142 /// use malachite_nz::integer::Integer;
143 ///
144 /// // -123 * 456 = -56088
145 /// assert_eq!((&Integer::from(-56088)).div_exact(Integer::from(456)), -123);
146 ///
147 /// // -123456789000 * -987654321000 = 121932631112635269000000
148 /// assert_eq!(
149 /// (&Integer::from_str("121932631112635269000000").unwrap())
150 /// .div_exact(Integer::from_str("-987654321000").unwrap()),
151 /// -123456789000i64
152 /// );
153 /// ```
154 fn div_exact(self, other: Integer) -> Integer {
155 let q_abs = (&self.abs).div_exact(other.abs);
156 Integer {
157 sign: self.sign == other.sign || q_abs == 0,
158 abs: q_abs,
159 }
160 }
161}
162
163impl DivExact<&Integer> for &Integer {
164 type Output = Integer;
165
166 /// Divides an [`Integer`] by another [`Integer`], taking both by reference. The first
167 /// [`Integer`] must be exactly divisible by the second. If it isn't, this function may panic or
168 /// return a meaningless result.
169 ///
170 /// $$
171 /// f(x, y) = \frac{x}{y}.
172 /// $$
173 ///
174 /// If you are unsure whether the division will be exact, use `&self / &other` instead. If
175 /// you're unsure and you want to know, use `(&self).div_mod(&other)` and check whether the
176 /// remainder is zero. If you want a function that panics if the division is not exact, use
177 /// `(&self).div_round(&other, Exact)`.
178 ///
179 /// # Panics
180 /// Panics if `other` is zero. May panic if `self` is not divisible by `other`.
181 ///
182 /// # Examples
183 /// ```
184 /// use core::str::FromStr;
185 /// use malachite_base::num::arithmetic::traits::DivExact;
186 /// use malachite_nz::integer::Integer;
187 ///
188 /// // -123 * 456 = -56088
189 /// assert_eq!(
190 /// (&Integer::from(-56088)).div_exact(&Integer::from(456)),
191 /// -123
192 /// );
193 ///
194 /// // -123456789000 * -987654321000 = 121932631112635269000000
195 /// assert_eq!(
196 /// (&Integer::from_str("121932631112635269000000").unwrap())
197 /// .div_exact(&Integer::from_str("-987654321000").unwrap()),
198 /// -123456789000i64
199 /// );
200 /// ```
201 fn div_exact(self, other: &Integer) -> Integer {
202 let q_abs = (&self.abs).div_exact(&other.abs);
203 Integer {
204 sign: self.sign == other.sign || q_abs == 0,
205 abs: q_abs,
206 }
207 }
208}
209
210impl DivExactAssign<Self> for Integer {
211 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
212 /// right-hand side by value. The first [`Integer`] must be exactly divisible by the second. If
213 /// it isn't, this function may panic or return a meaningless result.
214 ///
215 /// $$
216 /// x \gets \frac{x}{y}.
217 /// $$
218 ///
219 /// If you are unsure whether the division will be exact, use `self /= other` instead. If you're
220 /// unsure and you want to know, use `self.div_assign_mod(other)` and check whether the
221 /// remainder is zero. If you want a function that panics if the division is not exact, use
222 /// `self.div_round_assign(other, Exact)`.
223 ///
224 /// # Worst-case complexity
225 /// $T(n) = O(n \log n \log \log n)$
226 ///
227 /// $M(n) = O(n \log n)$
228 ///
229 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
230 ///
231 /// # Panics
232 /// Panics if `other` is zero. May panic if `self` is not divisible by `other`.
233 ///
234 /// # Examples
235 /// ```
236 /// use core::str::FromStr;
237 /// use malachite_base::num::arithmetic::traits::DivExactAssign;
238 /// use malachite_nz::integer::Integer;
239 ///
240 /// // -123 * 456 = -56088
241 /// let mut x = Integer::from(-56088);
242 /// x.div_exact_assign(Integer::from(456));
243 /// assert_eq!(x, -123);
244 ///
245 /// // -123456789000 * -987654321000 = 121932631112635269000000
246 /// let mut x = Integer::from_str("121932631112635269000000").unwrap();
247 /// x.div_exact_assign(Integer::from_str("-987654321000").unwrap());
248 /// assert_eq!(x, -123456789000i64);
249 /// ```
250 fn div_exact_assign(&mut self, other: Self) {
251 self.abs.div_exact_assign(other.abs);
252 self.sign = self.sign == other.sign || self.abs == 0;
253 }
254}
255
256impl DivExactAssign<&Self> for Integer {
257 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
258 /// right-hand side by reference. The first [`Integer`] must be exactly divisible by the second.
259 /// If it isn't, this function may panic or return a meaningless result.
260 ///
261 /// $$
262 /// x \gets \frac{x}{y}.
263 /// $$
264 ///
265 /// If you are unsure whether the division will be exact, use `self /= &other` instead. If
266 /// you're unsure and you want to know, use `self.div_assign_mod(&other)` and check whether the
267 /// remainder is zero. If you want a function that panics if the division is not exact, use
268 /// `self.div_round_assign(&other, Exact)`.
269 ///
270 /// # Worst-case complexity
271 /// $T(n) = O(n \log n \log \log n)$
272 ///
273 /// $M(n) = O(n \log n)$
274 ///
275 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
276 ///
277 /// # Panics
278 /// Panics if `other` is zero. May panic if `self` is not divisible by `other`.
279 ///
280 /// # Examples
281 /// ```
282 /// use core::str::FromStr;
283 /// use malachite_base::num::arithmetic::traits::DivExactAssign;
284 /// use malachite_nz::integer::Integer;
285 ///
286 /// // -123 * 456 = -56088
287 /// let mut x = Integer::from(-56088);
288 /// x.div_exact_assign(&Integer::from(456));
289 /// assert_eq!(x, -123);
290 ///
291 /// // -123456789000 * -987654321000 = 121932631112635269000000
292 /// let mut x = Integer::from_str("121932631112635269000000").unwrap();
293 /// x.div_exact_assign(&Integer::from_str("-987654321000").unwrap());
294 /// assert_eq!(x, -123456789000i64);
295 /// ```
296 fn div_exact_assign(&mut self, other: &Self) {
297 self.abs.div_exact_assign(&other.abs);
298 self.sign = self.sign == other.sign || self.abs == 0;
299 }
300}