malachite_nz/integer/arithmetic/div.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 crate::natural::Natural;
11use core::ops::{Div, DivAssign};
12use malachite_base::num::arithmetic::traits::CheckedDiv;
13use malachite_base::num::basic::traits::Zero;
14
15impl Div<Self> for Integer {
16 type Output = Self;
17
18 /// Divides an [`Integer`] by another [`Integer`], taking both by value. The quotient is rounded
19 /// towards zero. The quotient and remainder (which is not computed) satisfy $x = qy + r$ and $0
20 /// \leq |r| < |y|$.
21 ///
22 /// $$
23 /// f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
24 /// $$
25 ///
26 /// # Worst-case complexity
27 /// $T(n) = O(n \log n \log\log n)$
28 ///
29 /// $M(n) = O(n \log n)$
30 ///
31 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
32 ///
33 /// # Panics
34 /// Panics if `other` is zero.
35 ///
36 /// # Examples
37 /// ```
38 /// use malachite_nz::integer::Integer;
39 ///
40 /// // 2 * 10 + 3 = 23
41 /// assert_eq!(Integer::from(23) / Integer::from(10), 2);
42 ///
43 /// // -2 * -10 + 3 = 23
44 /// assert_eq!(Integer::from(23) / Integer::from(-10), -2);
45 ///
46 /// // -2 * 10 + -3 = -23
47 /// assert_eq!(Integer::from(-23) / Integer::from(10), -2);
48 ///
49 /// // 2 * -10 + -3 = -23
50 /// assert_eq!(Integer::from(-23) / Integer::from(-10), 2);
51 /// ```
52 #[inline]
53 fn div(mut self, other: Self) -> Self {
54 self /= other;
55 self
56 }
57}
58
59impl Div<&Self> for Integer {
60 type Output = Self;
61
62 /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
63 /// reference. The quotient is rounded towards zero. The quotient and remainder (which is not
64 /// computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
65 ///
66 /// $$
67 /// f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
68 /// $$
69 ///
70 /// # Worst-case complexity
71 /// $T(n) = O(n \log n \log\log n)$
72 ///
73 /// $M(n) = O(n \log n)$
74 ///
75 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
76 ///
77 /// # Panics
78 /// Panics if `other` is zero.
79 ///
80 /// # Examples
81 /// ```
82 /// use malachite_nz::integer::Integer;
83 ///
84 /// // 2 * 10 + 3 = 23
85 /// assert_eq!(Integer::from(23) / &Integer::from(10), 2);
86 ///
87 /// // -2 * -10 + 3 = 23
88 /// assert_eq!(Integer::from(23) / &Integer::from(-10), -2);
89 ///
90 /// // -2 * 10 + -3 = -23
91 /// assert_eq!(Integer::from(-23) / &Integer::from(10), -2);
92 ///
93 /// // 2 * -10 + -3 = -23
94 /// assert_eq!(Integer::from(-23) / &Integer::from(-10), 2);
95 /// ```
96 #[inline]
97 fn div(mut self, other: &Self) -> Self {
98 self /= other;
99 self
100 }
101}
102
103impl Div<Integer> for &Integer {
104 type Output = Integer;
105
106 /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
107 /// by value. The quotient is rounded towards zero. The quotient and remainder (which is not
108 /// computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
109 ///
110 /// $$
111 /// f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
112 /// $$
113 ///
114 /// # Worst-case complexity
115 /// $T(n) = O(n \log n \log\log n)$
116 ///
117 /// $M(n) = O(n \log n)$
118 ///
119 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
120 ///
121 /// # Panics
122 /// Panics if `other` is zero.
123 ///
124 /// # Examples
125 /// ```
126 /// use malachite_nz::integer::Integer;
127 ///
128 /// // 2 * 10 + 3 = 23
129 /// assert_eq!(&Integer::from(23) / Integer::from(10), 2);
130 ///
131 /// // -2 * -10 + 3 = 23
132 /// assert_eq!(&Integer::from(23) / Integer::from(-10), -2);
133 ///
134 /// // -2 * 10 + -3 = -23
135 /// assert_eq!(&Integer::from(-23) / Integer::from(10), -2);
136 ///
137 /// // 2 * -10 + -3 = -23
138 /// assert_eq!(&Integer::from(-23) / Integer::from(-10), 2);
139 /// ```
140 #[inline]
141 fn div(self, other: Integer) -> Integer {
142 Integer::from_sign_and_abs(self.sign == other.sign, &self.abs / other.abs)
143 }
144}
145
146impl Div<&Integer> for &Integer {
147 type Output = Integer;
148
149 /// Divides an [`Integer`] by another [`Integer`], taking both by reference. The quotient is
150 /// rounded towards zero. The quotient and remainder (which is not computed) satisfy $x = qy +
151 /// r$ and $0 \leq |r| < |y|$.
152 ///
153 /// $$
154 /// f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
155 /// $$
156 ///
157 /// # Worst-case complexity
158 /// $T(n) = O(n \log n \log\log n)$
159 ///
160 /// $M(n) = O(n \log n)$
161 ///
162 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
163 ///
164 /// # Panics
165 /// Panics if `other` is zero.
166 ///
167 /// # Examples
168 /// ```
169 /// use malachite_nz::integer::Integer;
170 ///
171 /// // 2 * 10 + 3 = 23
172 /// assert_eq!(&Integer::from(23) / &Integer::from(10), 2);
173 ///
174 /// // -2 * -10 + 3 = 23
175 /// assert_eq!(&Integer::from(23) / &Integer::from(-10), -2);
176 ///
177 /// // -2 * 10 + -3 = -23
178 /// assert_eq!(&Integer::from(-23) / &Integer::from(10), -2);
179 ///
180 /// // 2 * -10 + -3 = -23
181 /// assert_eq!(&Integer::from(-23) / &Integer::from(-10), 2);
182 /// ```
183 #[inline]
184 fn div(self, other: &Integer) -> Integer {
185 Integer::from_sign_and_abs(self.sign == other.sign, &self.abs / &other.abs)
186 }
187}
188
189impl DivAssign<Self> for Integer {
190 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
191 /// right-hand side by value. The quotient is rounded towards zero. The quotient and remainder
192 /// (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
193 ///
194 /// $$
195 /// x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
196 /// $$
197 ///
198 /// # Worst-case complexity
199 /// $T(n) = O(n \log n \log\log n)$
200 ///
201 /// $M(n) = O(n \log n)$
202 ///
203 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
204 ///
205 /// # Panics
206 /// Panics if `other` is zero.
207 ///
208 /// # Examples
209 /// ```
210 /// use malachite_nz::integer::Integer;
211 ///
212 /// // 2 * 10 + 3 = 23
213 /// let mut x = Integer::from(23);
214 /// x /= Integer::from(10);
215 /// assert_eq!(x, 2);
216 ///
217 /// // -2 * -10 + 3 = 23
218 /// let mut x = Integer::from(23);
219 /// x /= Integer::from(-10);
220 /// assert_eq!(x, -2);
221 ///
222 /// // -2 * 10 + -3 = -23
223 /// let mut x = Integer::from(-23);
224 /// x /= Integer::from(10);
225 /// assert_eq!(x, -2);
226 ///
227 /// // 2 * -10 + -3 = -23
228 /// let mut x = Integer::from(-23);
229 /// x /= Integer::from(-10);
230 /// assert_eq!(x, 2);
231 /// ```
232 #[inline]
233 fn div_assign(&mut self, other: Self) {
234 self.abs /= other.abs;
235 self.sign = self.sign == other.sign || self.abs == 0;
236 }
237}
238
239impl DivAssign<&Self> for Integer {
240 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
241 /// right-hand side by reference. The quotient is rounded towards zero. The quotient and
242 /// remainder (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
243 ///
244 /// $$
245 /// x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
246 /// $$
247 ///
248 /// # Worst-case complexity
249 /// $T(n) = O(n \log n \log\log n)$
250 ///
251 /// $M(n) = O(n \log n)$
252 ///
253 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
254 ///
255 /// # Panics
256 /// Panics if `other` is zero.
257 ///
258 /// # Examples
259 /// ```
260 /// use malachite_nz::integer::Integer;
261 ///
262 /// // 2 * 10 + 3 = 23
263 /// let mut x = Integer::from(23);
264 /// x /= &Integer::from(10);
265 /// assert_eq!(x, 2);
266 ///
267 /// // -2 * -10 + 3 = 23
268 /// let mut x = Integer::from(23);
269 /// x /= &Integer::from(-10);
270 /// assert_eq!(x, -2);
271 ///
272 /// // -2 * 10 + -3 = -23
273 /// let mut x = Integer::from(-23);
274 /// x /= &Integer::from(10);
275 /// assert_eq!(x, -2);
276 ///
277 /// // 2 * -10 + -3 = -23
278 /// let mut x = Integer::from(-23);
279 /// x /= &Integer::from(-10);
280 /// assert_eq!(x, 2);
281 /// ```
282 #[inline]
283 fn div_assign(&mut self, other: &Self) {
284 self.abs /= &other.abs;
285 self.sign = self.sign == other.sign || self.abs == 0;
286 }
287}
288
289impl CheckedDiv<Self> for Integer {
290 type Output = Self;
291
292 /// Divides an [`Integer`] by another [`Integer`], taking both by value. The quotient is rounded
293 /// towards negative infinity. The quotient and remainder (which is not computed) satisfy $x =
294 /// qy + r$ and $0 \leq r < y$. Returns `None` when the second [`Integer`] is zero, `Some`
295 /// otherwise.
296 ///
297 /// $$
298 /// f(x, y) = \begin{cases}
299 /// \operatorname{Some}\left ( \left \lfloor \frac{x}{y} \right \rfloor \right ) &
300 /// \text{if} \\quad y \neq 0 \\\\
301 /// \text{None} & \text{otherwise}
302 /// \end{cases}
303 /// $$
304 ///
305 /// # Worst-case complexity
306 /// $T(n) = O(n \log n \log\log n)$
307 ///
308 /// $M(n) = O(n \log n)$
309 ///
310 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
311 ///
312 /// # Panics
313 /// Panics if `other` is zero.
314 ///
315 /// # Examples
316 /// ```
317 /// use malachite_base::num::arithmetic::traits::CheckedDiv;
318 /// use malachite_base::num::basic::traits::{One, Zero};
319 /// use malachite_base::strings::ToDebugString;
320 /// use malachite_nz::integer::Integer;
321 ///
322 /// // -2 * -10 + 3 = 23
323 /// assert_eq!(
324 /// Integer::from(23)
325 /// .checked_div(Integer::from(-10))
326 /// .to_debug_string(),
327 /// "Some(-2)"
328 /// );
329 /// assert_eq!(Integer::ONE.checked_div(Integer::ZERO), None);
330 /// ```
331 #[inline]
332 fn checked_div(self, other: Self) -> Option<Self> {
333 match (self, other) {
334 (_, integer_zero!()) => None,
335 (x, y) => Some(x / y),
336 }
337 }
338}
339
340impl CheckedDiv<&Self> for Integer {
341 type Output = Self;
342
343 /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
344 /// reference. The quotient is rounded towards negative infinity. The quotient and remainder
345 /// (which is not computed) satisfy $x = qy + r$ and $0 \leq r < y$. Returns `None` when the
346 /// second [`Integer`] is zero, `Some` otherwise.
347 ///
348 /// $$
349 /// f(x, y) = \begin{cases}
350 /// \operatorname{Some}\left ( \left \lfloor \frac{x}{y} \right \rfloor \right ) &
351 /// \text{if} \\quad y \neq 0 \\\\
352 /// \text{None} & \text{otherwise}
353 /// \end{cases}
354 /// $$
355 ///
356 /// # Worst-case complexity
357 /// $T(n) = O(n \log n \log\log n)$
358 ///
359 /// $M(n) = O(n \log n)$
360 ///
361 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
362 ///
363 /// # Panics
364 /// Panics if `other` is zero.
365 ///
366 /// # Examples
367 /// ```
368 /// use malachite_base::num::arithmetic::traits::CheckedDiv;
369 /// use malachite_base::num::basic::traits::{One, Zero};
370 /// use malachite_base::strings::ToDebugString;
371 /// use malachite_nz::integer::Integer;
372 ///
373 /// // -2 * -10 + 3 = 23
374 /// assert_eq!(
375 /// Integer::from(23)
376 /// .checked_div(&Integer::from(-10))
377 /// .to_debug_string(),
378 /// "Some(-2)"
379 /// );
380 /// assert_eq!(Integer::ONE.checked_div(&Integer::ZERO), None);
381 /// ```
382 #[inline]
383 fn checked_div(self, other: &Self) -> Option<Self> {
384 match (self, other) {
385 (_, &integer_zero!()) => None,
386 (x, y) => Some(x / y),
387 }
388 }
389}
390
391impl CheckedDiv<Integer> for &Integer {
392 type Output = Integer;
393
394 /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
395 /// by value. The quotient is rounded towards negative infinity. The quotient and remainder
396 /// (which is not computed) satisfy $x = qy + r$ and $0 \leq r < y$. Returns `None` when the
397 /// second [`Integer`] is zero, `Some` otherwise.
398 ///
399 /// $$
400 /// f(x, y) = \begin{cases}
401 /// \operatorname{Some}\left ( \left \lfloor \frac{x}{y} \right \rfloor \right ) &
402 /// \text{if} \\quad y \neq 0 \\\\
403 /// \text{None} & \text{otherwise}
404 /// \end{cases}
405 /// $$
406 ///
407 /// # Worst-case complexity
408 /// $T(n) = O(n \log n \log\log n)$
409 ///
410 /// $M(n) = O(n \log n)$
411 ///
412 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
413 ///
414 /// # Panics
415 /// Panics if `other` is zero.
416 ///
417 /// # Examples
418 /// ```
419 /// use malachite_base::num::arithmetic::traits::CheckedDiv;
420 /// use malachite_base::num::basic::traits::{One, Zero};
421 /// use malachite_base::strings::ToDebugString;
422 /// use malachite_nz::integer::Integer;
423 ///
424 /// // -2 * -10 + 3 = 23
425 /// assert_eq!(
426 /// (&Integer::from(23))
427 /// .checked_div(Integer::from(-10))
428 /// .to_debug_string(),
429 /// "Some(-2)"
430 /// );
431 /// assert_eq!((&Integer::ONE).checked_div(Integer::ZERO), None);
432 /// ```
433 fn checked_div(self, other: Integer) -> Option<Integer> {
434 match (self, other) {
435 (_, integer_zero!()) => None,
436 (x, y) => Some(x / y),
437 }
438 }
439}
440
441impl CheckedDiv<&Integer> for &Integer {
442 type Output = Integer;
443
444 /// Divides an [`Integer`] by another [`Integer`], taking both by reference. The quotient is
445 /// rounded towards negative infinity. The quotient and remainder (which is not computed)
446 /// satisfy $x = qy + r$ and $0 \leq r < y$. Returns `None` when the second [`Integer`] is zero,
447 /// `Some` otherwise.
448 ///
449 /// $$
450 /// f(x, y) = \begin{cases}
451 /// \operatorname{Some}\left ( \left \lfloor \frac{x}{y} \right \rfloor \right ) &
452 /// \text{if} \\quad y \neq 0 \\\\
453 /// \text{None} & \text{otherwise}
454 /// \end{cases}
455 /// $$
456 ///
457 /// # Worst-case complexity
458 /// $T(n) = O(n \log n \log\log n)$
459 ///
460 /// $M(n) = O(n \log n)$
461 ///
462 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
463 ///
464 /// # Panics
465 /// Panics if `other` is zero.
466 ///
467 /// # Examples
468 /// ```
469 /// use malachite_base::num::arithmetic::traits::CheckedDiv;
470 /// use malachite_base::num::basic::traits::{One, Zero};
471 /// use malachite_base::strings::ToDebugString;
472 /// use malachite_nz::integer::Integer;
473 ///
474 /// // -2 * -10 + 3 = 23
475 /// assert_eq!(
476 /// (&Integer::from(23))
477 /// .checked_div(&Integer::from(-10))
478 /// .to_debug_string(),
479 /// "Some(-2)"
480 /// );
481 /// assert_eq!((&Integer::ONE).checked_div(&Integer::ZERO), None);
482 /// ```
483 fn checked_div(self, other: &Integer) -> Option<Integer> {
484 match (self, other) {
485 (_, &integer_zero!()) => None,
486 (x, y) => Some(x / y),
487 }
488 }
489}