Skip to main content

malachite_nz/integer/arithmetic/
div_mod.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 crate::natural::Natural;
11use crate::natural::arithmetic::div_mod::DivModData;
12use malachite_base::num::arithmetic::traits::{
13    CeilingDivAssignMod, CeilingDivAssignNegMod, CeilingDivMod, CeilingDivNegMod, DivAssignMod,
14    DivAssignModPrecomputed, DivAssignRem, DivMod, DivModPrecomputed, DivRem,
15};
16use malachite_base::num::basic::traits::One;
17
18impl DivMod<Self> for Integer {
19    type DivOutput = Self;
20    type ModOutput = Self;
21
22    /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning the
23    /// quotient and remainder. The quotient is rounded towards negative infinity, and the remainder
24    /// has the same sign as the second [`Integer`].
25    ///
26    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
27    ///
28    /// $$
29    /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
30    /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
31    /// $$
32    ///
33    /// # Worst-case complexity
34    /// $T(n) = O(n \log n \log \log n)$
35    ///
36    /// $M(n) = O(n \log n)$
37    ///
38    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
39    ///
40    /// # Panics
41    /// Panics if `other` is zero.
42    ///
43    /// # Examples
44    /// ```
45    /// use malachite_base::num::arithmetic::traits::DivMod;
46    /// use malachite_base::strings::ToDebugString;
47    /// use malachite_nz::integer::Integer;
48    ///
49    /// // 2 * 10 + 3 = 23
50    /// assert_eq!(
51    ///     Integer::from(23)
52    ///         .div_mod(Integer::from(10))
53    ///         .to_debug_string(),
54    ///     "(2, 3)"
55    /// );
56    ///
57    /// // -3 * -10 + -7 = 23
58    /// assert_eq!(
59    ///     Integer::from(23)
60    ///         .div_mod(Integer::from(-10))
61    ///         .to_debug_string(),
62    ///     "(-3, -7)"
63    /// );
64    ///
65    /// // -3 * 10 + 7 = -23
66    /// assert_eq!(
67    ///     Integer::from(-23)
68    ///         .div_mod(Integer::from(10))
69    ///         .to_debug_string(),
70    ///     "(-3, 7)"
71    /// );
72    ///
73    /// // 2 * -10 + -3 = -23
74    /// assert_eq!(
75    ///     Integer::from(-23)
76    ///         .div_mod(Integer::from(-10))
77    ///         .to_debug_string(),
78    ///     "(2, -3)"
79    /// );
80    /// ```
81    #[inline]
82    fn div_mod(mut self, other: Self) -> (Self, Self) {
83        let r = self.div_assign_mod(other);
84        (self, r)
85    }
86}
87
88impl DivMod<&Self> for Integer {
89    type DivOutput = Self;
90    type ModOutput = Self;
91
92    /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
93    /// reference and returning the quotient and remainder. The quotient is rounded towards negative
94    /// infinity, and the remainder has the same sign as the second [`Integer`].
95    ///
96    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
97    ///
98    /// $$
99    /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
100    /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
101    /// $$
102    ///
103    /// # Worst-case complexity
104    /// $T(n) = O(n \log n \log \log n)$
105    ///
106    /// $M(n) = O(n \log n)$
107    ///
108    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
109    ///
110    /// # Panics
111    /// Panics if `other` is zero.
112    ///
113    /// # Examples
114    /// ```
115    /// use malachite_base::num::arithmetic::traits::DivMod;
116    /// use malachite_base::strings::ToDebugString;
117    /// use malachite_nz::integer::Integer;
118    ///
119    /// // 2 * 10 + 3 = 23
120    /// assert_eq!(
121    ///     Integer::from(23)
122    ///         .div_mod(&Integer::from(10))
123    ///         .to_debug_string(),
124    ///     "(2, 3)"
125    /// );
126    ///
127    /// // -3 * -10 + -7 = 23
128    /// assert_eq!(
129    ///     Integer::from(23)
130    ///         .div_mod(&Integer::from(-10))
131    ///         .to_debug_string(),
132    ///     "(-3, -7)"
133    /// );
134    ///
135    /// // -3 * 10 + 7 = -23
136    /// assert_eq!(
137    ///     Integer::from(-23)
138    ///         .div_mod(&Integer::from(10))
139    ///         .to_debug_string(),
140    ///     "(-3, 7)"
141    /// );
142    ///
143    /// // 2 * -10 + -3 = -23
144    /// assert_eq!(
145    ///     Integer::from(-23)
146    ///         .div_mod(&Integer::from(-10))
147    ///         .to_debug_string(),
148    ///     "(2, -3)"
149    /// );
150    /// ```
151    #[inline]
152    fn div_mod(mut self, other: &Self) -> (Self, Self) {
153        let r = self.div_assign_mod(other);
154        (self, r)
155    }
156}
157
158impl DivMod<Integer> for &Integer {
159    type DivOutput = Integer;
160    type ModOutput = Integer;
161
162    /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
163    /// by value and returning the quotient and remainder. The quotient is rounded towards negative
164    /// infinity, and the remainder has the same sign as the second [`Integer`].
165    ///
166    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
167    ///
168    /// $$
169    /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
170    /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
171    /// $$
172    ///
173    /// # Worst-case complexity
174    /// $T(n) = O(n \log n \log \log n)$
175    ///
176    /// $M(n) = O(n \log n)$
177    ///
178    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
179    ///
180    /// # Panics
181    /// Panics if `other` is zero.
182    ///
183    /// # Examples
184    /// ```
185    /// use malachite_base::num::arithmetic::traits::DivMod;
186    /// use malachite_base::strings::ToDebugString;
187    /// use malachite_nz::integer::Integer;
188    ///
189    /// // 2 * 10 + 3 = 23
190    /// assert_eq!(
191    ///     (&Integer::from(23))
192    ///         .div_mod(Integer::from(10))
193    ///         .to_debug_string(),
194    ///     "(2, 3)"
195    /// );
196    ///
197    /// // -3 * -10 + -7 = 23
198    /// assert_eq!(
199    ///     (&Integer::from(23))
200    ///         .div_mod(Integer::from(-10))
201    ///         .to_debug_string(),
202    ///     "(-3, -7)"
203    /// );
204    ///
205    /// // -3 * 10 + 7 = -23
206    /// assert_eq!(
207    ///     (&Integer::from(-23))
208    ///         .div_mod(Integer::from(10))
209    ///         .to_debug_string(),
210    ///     "(-3, 7)"
211    /// );
212    ///
213    /// // 2 * -10 + -3 = -23
214    /// assert_eq!(
215    ///     (&Integer::from(-23))
216    ///         .div_mod(Integer::from(-10))
217    ///         .to_debug_string(),
218    ///     "(2, -3)"
219    /// );
220    /// ```
221    fn div_mod(self, other: Integer) -> (Integer, Integer) {
222        let q_sign = self.sign == other.sign;
223        let (q, r) = if q_sign {
224            (&self.abs).div_mod(other.abs)
225        } else {
226            (&self.abs).ceiling_div_neg_mod(other.abs)
227        };
228        (
229            Integer::from_sign_and_abs(q_sign, q),
230            Integer::from_sign_and_abs(other.sign, r),
231        )
232    }
233}
234
235impl DivMod<&Integer> for &Integer {
236    type DivOutput = Integer;
237    type ModOutput = Integer;
238
239    /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning the
240    /// quotient and remainder. The quotient is rounded towards negative infinity, and the remainder
241    /// has the same sign as the second [`Integer`].
242    ///
243    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
244    ///
245    /// $$
246    /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
247    /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
248    /// $$
249    ///
250    /// # Worst-case complexity
251    /// $T(n) = O(n \log n \log \log n)$
252    ///
253    /// $M(n) = O(n \log n)$
254    ///
255    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
256    ///
257    /// # Panics
258    /// Panics if `other` is zero.
259    ///
260    /// # Examples
261    /// ```
262    /// use malachite_base::num::arithmetic::traits::DivMod;
263    /// use malachite_base::strings::ToDebugString;
264    /// use malachite_nz::integer::Integer;
265    ///
266    /// // 2 * 10 + 3 = 23
267    /// assert_eq!(
268    ///     (&Integer::from(23))
269    ///         .div_mod(&Integer::from(10))
270    ///         .to_debug_string(),
271    ///     "(2, 3)"
272    /// );
273    ///
274    /// // -3 * -10 + -7 = 23
275    /// assert_eq!(
276    ///     (&Integer::from(23))
277    ///         .div_mod(&Integer::from(-10))
278    ///         .to_debug_string(),
279    ///     "(-3, -7)"
280    /// );
281    ///
282    /// // -3 * 10 + 7 = -23
283    /// assert_eq!(
284    ///     (&Integer::from(-23))
285    ///         .div_mod(&Integer::from(10))
286    ///         .to_debug_string(),
287    ///     "(-3, 7)"
288    /// );
289    ///
290    /// // 2 * -10 + -3 = -23
291    /// assert_eq!(
292    ///     (&Integer::from(-23))
293    ///         .div_mod(&Integer::from(-10))
294    ///         .to_debug_string(),
295    ///     "(2, -3)"
296    /// );
297    /// ```
298    fn div_mod(self, other: &Integer) -> (Integer, Integer) {
299        let q_sign = self.sign == other.sign;
300        let (q, r) = if q_sign {
301            (&self.abs).div_mod(&other.abs)
302        } else {
303            (&self.abs).ceiling_div_neg_mod(&other.abs)
304        };
305        (
306            Integer::from_sign_and_abs(q_sign, q),
307            Integer::from_sign_and_abs(other.sign, r),
308        )
309    }
310}
311
312impl DivAssignMod<Self> for Integer {
313    type ModOutput = Self;
314
315    /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
316    /// right-hand side by value and returning the remainder. The quotient is rounded towards
317    /// negative infinity, and the remainder has the same sign as the second [`Integer`].
318    ///
319    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
320    ///
321    /// $$
322    /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor,
323    /// $$
324    /// $$
325    /// x \gets \left \lfloor \frac{x}{y} \right \rfloor.
326    /// $$
327    ///
328    /// # Worst-case complexity
329    /// $T(n) = O(n \log n \log \log n)$
330    ///
331    /// $M(n) = O(n \log n)$
332    ///
333    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
334    ///
335    /// # Panics
336    /// Panics if `other` is zero.
337    ///
338    /// # Examples
339    /// ```
340    /// use malachite_base::num::arithmetic::traits::DivAssignMod;
341    /// use malachite_nz::integer::Integer;
342    ///
343    /// // 2 * 10 + 3 = 23
344    /// let mut x = Integer::from(23);
345    /// assert_eq!(x.div_assign_mod(Integer::from(10)), 3);
346    /// assert_eq!(x, 2);
347    ///
348    /// // -3 * -10 + -7 = 23
349    /// let mut x = Integer::from(23);
350    /// assert_eq!(x.div_assign_mod(Integer::from(-10)), -7);
351    /// assert_eq!(x, -3);
352    ///
353    /// // -3 * 10 + 7 = -23
354    /// let mut x = Integer::from(-23);
355    /// assert_eq!(x.div_assign_mod(Integer::from(10)), 7);
356    /// assert_eq!(x, -3);
357    ///
358    /// // 2 * -10 + -3 = -23
359    /// let mut x = Integer::from(-23);
360    /// assert_eq!(x.div_assign_mod(Integer::from(-10)), -3);
361    /// assert_eq!(x, 2);
362    /// ```
363    fn div_assign_mod(&mut self, other: Self) -> Self {
364        let r = if self.sign == other.sign {
365            self.sign = true;
366            self.abs.div_assign_mod(other.abs)
367        } else {
368            let r = self.abs.ceiling_div_assign_neg_mod(other.abs);
369            if self.abs != 0u32 {
370                self.sign = false;
371            }
372            r
373        };
374        Self::from_sign_and_abs(other.sign, r)
375    }
376}
377
378impl DivAssignMod<&Self> for Integer {
379    type ModOutput = Self;
380
381    /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
382    /// right-hand side by reference and returning the remainder. The quotient is rounded towards
383    /// negative infinity, and the remainder has the same sign as the second [`Integer`].
384    ///
385    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
386    ///
387    /// $$
388    /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor,
389    /// $$
390    /// $$
391    /// x \gets \left \lfloor \frac{x}{y} \right \rfloor.
392    /// $$
393    ///
394    /// # Worst-case complexity
395    /// $T(n) = O(n \log n \log \log n)$
396    ///
397    /// $M(n) = O(n \log n)$
398    ///
399    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
400    ///
401    /// # Panics
402    /// Panics if `other` is zero.
403    ///
404    /// # Examples
405    /// ```
406    /// use malachite_base::num::arithmetic::traits::DivAssignMod;
407    /// use malachite_nz::integer::Integer;
408    ///
409    /// // 2 * 10 + 3 = 23
410    /// let mut x = Integer::from(23);
411    /// assert_eq!(x.div_assign_mod(&Integer::from(10)), 3);
412    /// assert_eq!(x, 2);
413    ///
414    /// // -3 * -10 + -7 = 23
415    /// let mut x = Integer::from(23);
416    /// assert_eq!(x.div_assign_mod(&Integer::from(-10)), -7);
417    /// assert_eq!(x, -3);
418    ///
419    /// // -3 * 10 + 7 = -23
420    /// let mut x = Integer::from(-23);
421    /// assert_eq!(x.div_assign_mod(&Integer::from(10)), 7);
422    /// assert_eq!(x, -3);
423    ///
424    /// // 2 * -10 + -3 = -23
425    /// let mut x = Integer::from(-23);
426    /// assert_eq!(x.div_assign_mod(&Integer::from(-10)), -3);
427    /// assert_eq!(x, 2);
428    /// ```
429    fn div_assign_mod(&mut self, other: &Self) -> Self {
430        let r = if self.sign == other.sign {
431            self.sign = true;
432            self.abs.div_assign_mod(&other.abs)
433        } else {
434            let r = self.abs.ceiling_div_assign_neg_mod(&other.abs);
435            if self.abs != 0u32 {
436                self.sign = false;
437            }
438            r
439        };
440        Self::from_sign_and_abs(other.sign, r)
441    }
442}
443
444// # Worst-case complexity
445// $T(n) = O(n \log n \log\log n)$
446//
447// $M(n) = O(n \log n)$
448//
449// where $T$ is time, $M$ is additional memory, and $n$ is `x.significant_bits()`.
450fn div_mod_precomputed_integers(
451    x: &Integer,
452    other: &Integer,
453    data: &DivModData,
454) -> (Integer, Integer) {
455    let q_sign = x.sign == other.sign;
456    let (mut q, mut r) = (&x.abs).div_mod_precomputed(&other.abs, data);
457    if !q_sign && r != 0u32 {
458        // The floor of the negative quotient is one less than the negated floor of the positive
459        // quotient, and the remainder is adjusted to have the divisor's sign.
460        q += Natural::ONE;
461        r = &other.abs - r;
462    }
463    (
464        Integer::from_sign_and_abs(q_sign, q),
465        Integer::from_sign_and_abs(other.sign, r),
466    )
467}
468
469macro_rules! integer_precompute_div_mod_data_doc {
470    ($f:item) => {
471        /// Precomputes data for division by an [`Integer`]. See `div_mod_precomputed` and
472        /// [`div_assign_mod_precomputed`](
473        /// malachite_base::num::arithmetic::traits::DivAssignModPrecomputed).
474        ///
475        /// The data depends only on the absolute value of the divisor.
476        ///
477        /// # Worst-case complexity
478        /// $T(n) = O(n \log n \log\log n)$
479        ///
480        /// $M(n) = O(n \log n)$
481        ///
482        /// where $T$ is time, $M$ is additional memory, and $n$ is `other.significant_bits()`.
483        ///
484        /// # Panics
485        /// Panics if `other` is zero.
486        $f
487    };
488}
489
490macro_rules! integer_div_mod_precomputed_doc {
491    ($f:item) => {
492        /// Divides an [`Integer`] by another [`Integer`], returning the quotient and remainder. The
493        /// quotient is rounded towards negative infinity, and the remainder has the same sign as
494        /// the second [`Integer`].
495        ///
496        /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
497        ///
498        /// Some precomputed data is provided; this speeds up computations involving several
499        /// divisions by the same divisor. The precomputed data should be obtained using
500        /// [`precompute_div_mod_data`](DivModPrecomputed::precompute_div_mod_data), applied to the
501        /// same divisor or to its negative.
502        ///
503        /// $$
504        /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
505        /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
506        /// $$
507        ///
508        /// # Worst-case complexity
509        /// $T(n) = O(n \log n \log\log n)$
510        ///
511        /// $M(n) = O(n \log n)$
512        ///
513        /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
514        ///
515        /// # Panics
516        /// May panic if `data` was not computed from `other` or its negative.
517        ///
518        /// # Examples
519        /// ```
520        /// use malachite_base::num::arithmetic::traits::DivModPrecomputed;
521        /// use malachite_base::strings::ToDebugString;
522        /// use malachite_nz::integer::Integer;
523        ///
524        /// let d = Integer::from(10);
525        /// let data = Integer::precompute_div_mod_data(&d);
526        /// // 2 * 10 + 3 = 23
527        /// assert_eq!(
528        ///     Integer::from(23)
529        ///         .div_mod_precomputed(&d, &data)
530        ///         .to_debug_string(),
531        ///     "(2, 3)"
532        /// );
533        /// // -3 * 10 + 7 = -23
534        /// assert_eq!(
535        ///     Integer::from(-23)
536        ///         .div_mod_precomputed(&d, &data)
537        ///         .to_debug_string(),
538        ///     "(-3, 7)"
539        /// );
540        ///
541        /// let d = Integer::from(-10);
542        /// let data = Integer::precompute_div_mod_data(&d);
543        /// // -3 * -10 + -7 = 23
544        /// assert_eq!(
545        ///     Integer::from(23)
546        ///         .div_mod_precomputed(&d, &data)
547        ///         .to_debug_string(),
548        ///     "(-3, -7)"
549        /// );
550        /// // 2 * -10 + -3 = -23
551        /// assert_eq!(
552        ///     Integer::from(-23)
553        ///         .div_mod_precomputed(&d, &data)
554        ///         .to_debug_string(),
555        ///     "(2, -3)"
556        /// );
557        /// ```
558        $f
559    };
560}
561
562impl DivModPrecomputed<Self> for Integer {
563    type DivOutput = Self;
564    type ModOutput = Self;
565    type Data = DivModData;
566
567    integer_precompute_div_mod_data_doc! {
568        #[inline]
569        fn precompute_div_mod_data(other: &Self) -> DivModData {
570            Natural::precompute_div_mod_data(&other.abs)
571        }
572    }
573
574    integer_div_mod_precomputed_doc! {
575        #[inline]
576        fn div_mod_precomputed(self, other: Self, data: &DivModData) -> (Self, Self) {
577            div_mod_precomputed_integers(&self, &other, data)
578        }
579    }
580}
581
582impl DivModPrecomputed<&Self> for Integer {
583    type DivOutput = Self;
584    type ModOutput = Self;
585    type Data = DivModData;
586
587    integer_precompute_div_mod_data_doc! {
588        #[inline]
589        fn precompute_div_mod_data(other: &&Self) -> DivModData {
590            Natural::precompute_div_mod_data(&other.abs)
591        }
592    }
593
594    integer_div_mod_precomputed_doc! {
595        #[inline]
596        fn div_mod_precomputed(self, other: &Self, data: &DivModData) -> (Self, Self) {
597            div_mod_precomputed_integers(&self, other, data)
598        }
599    }
600}
601
602impl DivModPrecomputed<Integer> for &Integer {
603    type DivOutput = Integer;
604    type ModOutput = Integer;
605    type Data = DivModData;
606
607    integer_precompute_div_mod_data_doc! {
608        #[inline]
609        fn precompute_div_mod_data(other: &Integer) -> DivModData {
610            Natural::precompute_div_mod_data(&other.abs)
611        }
612    }
613
614    integer_div_mod_precomputed_doc! {
615        #[inline]
616        fn div_mod_precomputed(self, other: Integer, data: &DivModData) -> (Integer, Integer) {
617            div_mod_precomputed_integers(self, &other, data)
618        }
619    }
620}
621
622impl DivModPrecomputed<&Integer> for &Integer {
623    type DivOutput = Integer;
624    type ModOutput = Integer;
625    type Data = DivModData;
626
627    integer_precompute_div_mod_data_doc! {
628        #[inline]
629        fn precompute_div_mod_data(other: &&Integer) -> DivModData {
630            Natural::precompute_div_mod_data(&other.abs)
631        }
632    }
633
634    integer_div_mod_precomputed_doc! {
635        #[inline]
636        fn div_mod_precomputed(self, other: &Integer, data: &DivModData) -> (Integer, Integer) {
637            div_mod_precomputed_integers(self, other, data)
638        }
639    }
640}
641
642impl DivAssignModPrecomputed<Self> for Integer {
643    /// Divides an [`Integer`] by another [`Integer`] in place, returning the remainder. The
644    /// quotient is rounded towards negative infinity, and the remainder has the same sign as the
645    /// second [`Integer`].
646    ///
647    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
648    ///
649    /// Some precomputed data is provided; this speeds up computations involving several divisions
650    /// by the same divisor. The precomputed data should be obtained using
651    /// [`precompute_div_mod_data`](DivModPrecomputed::precompute_div_mod_data), applied to the same
652    /// divisor or to its negative.
653    ///
654    /// # Worst-case complexity
655    /// $T(n) = O(n \log n \log\log n)$
656    ///
657    /// $M(n) = O(n \log n)$
658    ///
659    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
660    ///
661    /// # Panics
662    /// May panic if `data` was not computed from `other` or its negative.
663    ///
664    /// # Examples
665    /// ```
666    /// use malachite_base::num::arithmetic::traits::{DivAssignModPrecomputed, DivModPrecomputed};
667    /// use malachite_nz::integer::Integer;
668    ///
669    /// let d = Integer::from(10);
670    /// let data = Integer::precompute_div_mod_data(&d);
671    /// // -3 * 10 + 7 = -23
672    /// let mut x = Integer::from(-23);
673    /// assert_eq!(x.div_assign_mod_precomputed(d, &data), 7);
674    /// assert_eq!(x, -3);
675    /// ```
676    #[inline]
677    fn div_assign_mod_precomputed(&mut self, other: Self, data: &DivModData) -> Self {
678        let (q, r) = div_mod_precomputed_integers(self, &other, data);
679        *self = q;
680        r
681    }
682}
683
684impl DivAssignModPrecomputed<&Self> for Integer {
685    /// Divides an [`Integer`] by another [`Integer`] in place, returning the remainder. The
686    /// quotient is rounded towards negative infinity, and the remainder has the same sign as the
687    /// second [`Integer`].
688    ///
689    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
690    ///
691    /// Some precomputed data is provided; this speeds up computations involving several divisions
692    /// by the same divisor. The precomputed data should be obtained using
693    /// [`precompute_div_mod_data`](DivModPrecomputed::precompute_div_mod_data), applied to the same
694    /// divisor or to its negative.
695    ///
696    /// # Worst-case complexity
697    /// $T(n) = O(n \log n \log\log n)$
698    ///
699    /// $M(n) = O(n \log n)$
700    ///
701    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
702    ///
703    /// # Panics
704    /// May panic if `data` was not computed from `other` or its negative.
705    ///
706    /// # Examples
707    /// ```
708    /// use malachite_base::num::arithmetic::traits::{DivAssignModPrecomputed, DivModPrecomputed};
709    /// use malachite_nz::integer::Integer;
710    ///
711    /// let d = Integer::from(10);
712    /// let data = Integer::precompute_div_mod_data(&d);
713    /// // -3 * 10 + 7 = -23
714    /// let mut x = Integer::from(-23);
715    /// assert_eq!(x.div_assign_mod_precomputed(&d, &data), 7);
716    /// assert_eq!(x, -3);
717    /// ```
718    #[inline]
719    fn div_assign_mod_precomputed(&mut self, other: &Self, data: &DivModData) -> Self {
720        let (q, r) = div_mod_precomputed_integers(self, other, data);
721        *self = q;
722        r
723    }
724}
725
726impl DivRem<Self> for Integer {
727    type DivOutput = Self;
728    type RemOutput = Self;
729
730    /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning the
731    /// quotient and remainder. The quotient is rounded towards zero and the remainder has the same
732    /// sign as the first [`Integer`].
733    ///
734    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
735    ///
736    /// $$
737    /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
738    /// \right \rfloor, \space
739    /// x - y \operatorname{sgn}(xy)
740    /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
741    /// $$
742    ///
743    /// # Worst-case complexity
744    /// $T(n) = O(n \log n \log \log n)$
745    ///
746    /// $M(n) = O(n \log n)$
747    ///
748    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
749    ///
750    /// # Panics
751    /// Panics if `other` is zero.
752    ///
753    /// # Examples
754    /// ```
755    /// use malachite_base::num::arithmetic::traits::DivRem;
756    /// use malachite_base::strings::ToDebugString;
757    /// use malachite_nz::integer::Integer;
758    ///
759    /// // 2 * 10 + 3 = 23
760    /// assert_eq!(
761    ///     Integer::from(23)
762    ///         .div_rem(Integer::from(10))
763    ///         .to_debug_string(),
764    ///     "(2, 3)"
765    /// );
766    ///
767    /// // -2 * -10 + 3 = 23
768    /// assert_eq!(
769    ///     Integer::from(23)
770    ///         .div_rem(Integer::from(-10))
771    ///         .to_debug_string(),
772    ///     "(-2, 3)"
773    /// );
774    ///
775    /// // -2 * 10 + -3 = -23
776    /// assert_eq!(
777    ///     Integer::from(-23)
778    ///         .div_rem(Integer::from(10))
779    ///         .to_debug_string(),
780    ///     "(-2, -3)"
781    /// );
782    ///
783    /// // 2 * -10 + -3 = -23
784    /// assert_eq!(
785    ///     Integer::from(-23)
786    ///         .div_rem(Integer::from(-10))
787    ///         .to_debug_string(),
788    ///     "(2, -3)"
789    /// );
790    /// ```
791    #[inline]
792    fn div_rem(mut self, other: Self) -> (Self, Self) {
793        let r = self.div_assign_rem(other);
794        (self, r)
795    }
796}
797
798impl DivRem<&Self> for Integer {
799    type DivOutput = Self;
800    type RemOutput = Self;
801
802    /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
803    /// reference and returning the quotient and remainder. The quotient is rounded towards zero and
804    /// the remainder has the same sign as the first [`Integer`].
805    ///
806    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
807    ///
808    /// $$
809    /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
810    /// \right \rfloor, \space
811    /// x - y \operatorname{sgn}(xy)
812    /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
813    /// $$
814    ///
815    /// # Worst-case complexity
816    /// $T(n) = O(n \log n \log \log n)$
817    ///
818    /// $M(n) = O(n \log n)$
819    ///
820    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
821    ///
822    /// # Panics
823    /// Panics if `other` is zero.
824    ///
825    /// # Examples
826    /// ```
827    /// use malachite_base::num::arithmetic::traits::DivRem;
828    /// use malachite_base::strings::ToDebugString;
829    /// use malachite_nz::integer::Integer;
830    ///
831    /// // 2 * 10 + 3 = 23
832    /// assert_eq!(
833    ///     Integer::from(23)
834    ///         .div_rem(&Integer::from(10))
835    ///         .to_debug_string(),
836    ///     "(2, 3)"
837    /// );
838    ///
839    /// // -2 * -10 + 3 = 23
840    /// assert_eq!(
841    ///     Integer::from(23)
842    ///         .div_rem(&Integer::from(-10))
843    ///         .to_debug_string(),
844    ///     "(-2, 3)"
845    /// );
846    ///
847    /// // -2 * 10 + -3 = -23
848    /// assert_eq!(
849    ///     Integer::from(-23)
850    ///         .div_rem(&Integer::from(10))
851    ///         .to_debug_string(),
852    ///     "(-2, -3)"
853    /// );
854    ///
855    /// // 2 * -10 + -3 = -23
856    /// assert_eq!(
857    ///     Integer::from(-23)
858    ///         .div_rem(&Integer::from(-10))
859    ///         .to_debug_string(),
860    ///     "(2, -3)"
861    /// );
862    /// ```
863    #[inline]
864    fn div_rem(mut self, other: &Self) -> (Self, Self) {
865        let r = self.div_assign_rem(other);
866        (self, r)
867    }
868}
869
870impl DivRem<Integer> for &Integer {
871    type DivOutput = Integer;
872    type RemOutput = Integer;
873
874    /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
875    /// by value and returning the quotient and remainder. The quotient is rounded towards zero and
876    /// the remainder has the same sign as the first [`Integer`].
877    ///
878    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
879    ///
880    /// $$
881    /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
882    /// \right \rfloor, \space
883    /// x - y \operatorname{sgn}(xy)
884    /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
885    /// $$
886    ///
887    /// # Worst-case complexity
888    /// $T(n) = O(n \log n \log \log n)$
889    ///
890    /// $M(n) = O(n \log n)$
891    ///
892    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
893    ///
894    /// # Panics
895    /// Panics if `other` is zero.
896    ///
897    /// # Examples
898    /// ```
899    /// use malachite_base::num::arithmetic::traits::DivRem;
900    /// use malachite_base::strings::ToDebugString;
901    /// use malachite_nz::integer::Integer;
902    ///
903    /// // 2 * 10 + 3 = 23
904    /// assert_eq!(
905    ///     (&Integer::from(23))
906    ///         .div_rem(Integer::from(10))
907    ///         .to_debug_string(),
908    ///     "(2, 3)"
909    /// );
910    ///
911    /// // -2 * -10 + 3 = 23
912    /// assert_eq!(
913    ///     (&Integer::from(23))
914    ///         .div_rem(Integer::from(-10))
915    ///         .to_debug_string(),
916    ///     "(-2, 3)"
917    /// );
918    ///
919    /// // -2 * 10 + -3 = -23
920    /// assert_eq!(
921    ///     (&Integer::from(-23))
922    ///         .div_rem(Integer::from(10))
923    ///         .to_debug_string(),
924    ///     "(-2, -3)"
925    /// );
926    ///
927    /// // 2 * -10 + -3 = -23
928    /// assert_eq!(
929    ///     (&Integer::from(-23))
930    ///         .div_rem(Integer::from(-10))
931    ///         .to_debug_string(),
932    ///     "(2, -3)"
933    /// );
934    /// ```
935    #[inline]
936    fn div_rem(self, other: Integer) -> (Integer, Integer) {
937        let (q, r) = (&self.abs).div_mod(other.abs);
938        (
939            Integer::from_sign_and_abs(self.sign == other.sign, q),
940            Integer::from_sign_and_abs(self.sign, r),
941        )
942    }
943}
944
945impl DivRem<&Integer> for &Integer {
946    type DivOutput = Integer;
947    type RemOutput = Integer;
948
949    /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning the
950    /// quotient and remainder. The quotient is rounded towards zero and the remainder has the same
951    /// sign as the first [`Integer`].
952    ///
953    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
954    ///
955    /// $$
956    /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
957    /// \right \rfloor, \space
958    /// x - y \operatorname{sgn}(xy)
959    /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
960    /// $$
961    ///
962    /// # Worst-case complexity
963    /// $T(n) = O(n \log n \log \log n)$
964    ///
965    /// $M(n) = O(n \log n)$
966    ///
967    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
968    ///
969    /// # Panics
970    /// Panics if `other` is zero.
971    ///
972    /// # Examples
973    /// ```
974    /// use malachite_base::num::arithmetic::traits::DivRem;
975    /// use malachite_base::strings::ToDebugString;
976    /// use malachite_nz::integer::Integer;
977    ///
978    /// // 2 * 10 + 3 = 23
979    /// assert_eq!(
980    ///     (&Integer::from(23))
981    ///         .div_rem(&Integer::from(10))
982    ///         .to_debug_string(),
983    ///     "(2, 3)"
984    /// );
985    ///
986    /// // -2 * -10 + 3 = 23
987    /// assert_eq!(
988    ///     (&Integer::from(23))
989    ///         .div_rem(&Integer::from(-10))
990    ///         .to_debug_string(),
991    ///     "(-2, 3)"
992    /// );
993    ///
994    /// // -2 * 10 + -3 = -23
995    /// assert_eq!(
996    ///     (&Integer::from(-23))
997    ///         .div_rem(&Integer::from(10))
998    ///         .to_debug_string(),
999    ///     "(-2, -3)"
1000    /// );
1001    ///
1002    /// // 2 * -10 + -3 = -23
1003    /// assert_eq!(
1004    ///     (&Integer::from(-23))
1005    ///         .div_rem(&Integer::from(-10))
1006    ///         .to_debug_string(),
1007    ///     "(2, -3)"
1008    /// );
1009    /// ```
1010    #[inline]
1011    fn div_rem(self, other: &Integer) -> (Integer, Integer) {
1012        let (q, r) = (&self.abs).div_mod(&other.abs);
1013        (
1014            Integer::from_sign_and_abs(self.sign == other.sign, q),
1015            Integer::from_sign_and_abs(self.sign, r),
1016        )
1017    }
1018}
1019
1020impl DivAssignRem<Self> for Integer {
1021    type RemOutput = Self;
1022
1023    /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
1024    /// right-hand side by value and returning the remainder. The quotient is rounded towards zero
1025    /// and the remainder has the same sign as the first [`Integer`].
1026    ///
1027    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1028    ///
1029    /// $$
1030    /// f(x, y) = x - y \operatorname{sgn}(xy)
1031    /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor,
1032    /// $$
1033    /// $$
1034    /// x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
1035    /// \right \rfloor.
1036    /// $$
1037    ///
1038    /// # Worst-case complexity
1039    /// $T(n) = O(n \log n \log \log n)$
1040    ///
1041    /// $M(n) = O(n \log n)$
1042    ///
1043    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1044    ///
1045    /// # Panics
1046    /// Panics if `other` is zero.
1047    ///
1048    /// # Examples
1049    /// ```
1050    /// use malachite_base::num::arithmetic::traits::DivAssignRem;
1051    /// use malachite_nz::integer::Integer;
1052    ///
1053    /// // 2 * 10 + 3 = 23
1054    /// let mut x = Integer::from(23);
1055    /// assert_eq!(x.div_assign_rem(Integer::from(10)), 3);
1056    /// assert_eq!(x, 2);
1057    ///
1058    /// // -2 * -10 + 3 = 23
1059    /// let mut x = Integer::from(23);
1060    /// assert_eq!(x.div_assign_rem(Integer::from(-10)), 3);
1061    /// assert_eq!(x, -2);
1062    ///
1063    /// // -2 * 10 + -3 = -23
1064    /// let mut x = Integer::from(-23);
1065    /// assert_eq!(x.div_assign_rem(Integer::from(10)), -3);
1066    /// assert_eq!(x, -2);
1067    ///
1068    /// // 2 * -10 + -3 = -23
1069    /// let mut x = Integer::from(-23);
1070    /// assert_eq!(x.div_assign_rem(Integer::from(-10)), -3);
1071    /// assert_eq!(x, 2);
1072    /// ```
1073    #[inline]
1074    fn div_assign_rem(&mut self, other: Self) -> Self {
1075        let r = Self::from_sign_and_abs(self.sign, self.abs.div_assign_mod(other.abs));
1076        self.sign = self.sign == other.sign || self.abs == 0u32;
1077        r
1078    }
1079}
1080
1081impl DivAssignRem<&Self> for Integer {
1082    type RemOutput = Self;
1083
1084    /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
1085    /// right-hand side by reference and returning the remainder. The quotient is rounded towards
1086    /// zero and the remainder has the same sign as the first [`Integer`].
1087    ///
1088    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1089    ///
1090    /// $$
1091    /// f(x, y) = x - y \operatorname{sgn}(xy)
1092    /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor,
1093    /// $$
1094    /// $$
1095    /// x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
1096    /// \right \rfloor.
1097    /// $$
1098    ///
1099    /// # Worst-case complexity
1100    /// $T(n) = O(n \log n \log \log n)$
1101    ///
1102    /// $M(n) = O(n \log n)$
1103    ///
1104    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1105    ///
1106    /// # Panics
1107    /// Panics if `other` is zero.
1108    ///
1109    /// # Examples
1110    /// ```
1111    /// use malachite_base::num::arithmetic::traits::DivAssignRem;
1112    /// use malachite_nz::integer::Integer;
1113    ///
1114    /// // 2 * 10 + 3 = 23
1115    /// let mut x = Integer::from(23);
1116    /// assert_eq!(x.div_assign_rem(&Integer::from(10)), 3);
1117    /// assert_eq!(x, 2);
1118    ///
1119    /// // -2 * -10 + 3 = 23
1120    /// let mut x = Integer::from(23);
1121    /// assert_eq!(x.div_assign_rem(&Integer::from(-10)), 3);
1122    /// assert_eq!(x, -2);
1123    ///
1124    /// // -2 * 10 + -3 = -23
1125    /// let mut x = Integer::from(-23);
1126    /// assert_eq!(x.div_assign_rem(&Integer::from(10)), -3);
1127    /// assert_eq!(x, -2);
1128    ///
1129    /// // 2 * -10 + -3 = -23
1130    /// let mut x = Integer::from(-23);
1131    /// assert_eq!(x.div_assign_rem(&Integer::from(-10)), -3);
1132    /// assert_eq!(x, 2);
1133    /// ```
1134    #[inline]
1135    fn div_assign_rem(&mut self, other: &Self) -> Self {
1136        let r = Self::from_sign_and_abs(self.sign, self.abs.div_assign_mod(&other.abs));
1137        self.sign = self.sign == other.sign || self.abs == 0u32;
1138        r
1139    }
1140}
1141
1142impl CeilingDivMod<Self> for Integer {
1143    type DivOutput = Self;
1144    type ModOutput = Self;
1145
1146    /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning the
1147    /// quotient and remainder. The quotient is rounded towards positive infinity and the remainder
1148    /// has the opposite sign as the second [`Integer`].
1149    ///
1150    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1151    ///
1152    /// $$
1153    /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
1154    /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
1155    /// $$
1156    ///
1157    /// # Worst-case complexity
1158    /// $T(n) = O(n \log n \log \log n)$
1159    ///
1160    /// $M(n) = O(n \log n)$
1161    ///
1162    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1163    ///
1164    /// # Panics
1165    /// Panics if `other` is zero.
1166    ///
1167    /// # Examples
1168    /// ```
1169    /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
1170    /// use malachite_base::strings::ToDebugString;
1171    /// use malachite_nz::integer::Integer;
1172    ///
1173    /// // 3 * 10 + -7 = 23
1174    /// assert_eq!(
1175    ///     Integer::from(23)
1176    ///         .ceiling_div_mod(Integer::from(10))
1177    ///         .to_debug_string(),
1178    ///     "(3, -7)"
1179    /// );
1180    ///
1181    /// // -2 * -10 + 3 = 23
1182    /// assert_eq!(
1183    ///     Integer::from(23)
1184    ///         .ceiling_div_mod(Integer::from(-10))
1185    ///         .to_debug_string(),
1186    ///     "(-2, 3)"
1187    /// );
1188    ///
1189    /// // -2 * 10 + -3 = -23
1190    /// assert_eq!(
1191    ///     Integer::from(-23)
1192    ///         .ceiling_div_mod(Integer::from(10))
1193    ///         .to_debug_string(),
1194    ///     "(-2, -3)"
1195    /// );
1196    ///
1197    /// // 3 * -10 + 7 = -23
1198    /// assert_eq!(
1199    ///     Integer::from(-23)
1200    ///         .ceiling_div_mod(Integer::from(-10))
1201    ///         .to_debug_string(),
1202    ///     "(3, 7)"
1203    /// );
1204    /// ```
1205    #[inline]
1206    fn ceiling_div_mod(mut self, other: Self) -> (Self, Self) {
1207        let r = self.ceiling_div_assign_mod(other);
1208        (self, r)
1209    }
1210}
1211
1212impl CeilingDivMod<&Self> for Integer {
1213    type DivOutput = Self;
1214    type ModOutput = Self;
1215
1216    /// Divides an [`Integer`] by another [`Integer`], taking both the first by value and the second
1217    /// by reference and returning the quotient and remainder. The quotient is rounded towards
1218    /// positive infinity and the remainder has the opposite sign as the second [`Integer`].
1219    ///
1220    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1221    ///
1222    /// $$
1223    /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
1224    /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
1225    /// $$
1226    ///
1227    /// # Worst-case complexity
1228    /// $T(n) = O(n \log n \log \log n)$
1229    ///
1230    /// $M(n) = O(n \log n)$
1231    ///
1232    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1233    ///
1234    /// # Panics
1235    /// Panics if `other` is zero.
1236    ///
1237    /// # Examples
1238    /// ```
1239    /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
1240    /// use malachite_base::strings::ToDebugString;
1241    /// use malachite_nz::integer::Integer;
1242    ///
1243    /// // 3 * 10 + -7 = 23
1244    /// assert_eq!(
1245    ///     Integer::from(23)
1246    ///         .ceiling_div_mod(&Integer::from(10))
1247    ///         .to_debug_string(),
1248    ///     "(3, -7)"
1249    /// );
1250    ///
1251    /// // -2 * -10 + 3 = 23
1252    /// assert_eq!(
1253    ///     Integer::from(23)
1254    ///         .ceiling_div_mod(&Integer::from(-10))
1255    ///         .to_debug_string(),
1256    ///     "(-2, 3)"
1257    /// );
1258    ///
1259    /// // -2 * 10 + -3 = -23
1260    /// assert_eq!(
1261    ///     Integer::from(-23)
1262    ///         .ceiling_div_mod(&Integer::from(10))
1263    ///         .to_debug_string(),
1264    ///     "(-2, -3)"
1265    /// );
1266    ///
1267    /// // 3 * -10 + 7 = -23
1268    /// assert_eq!(
1269    ///     Integer::from(-23)
1270    ///         .ceiling_div_mod(&Integer::from(-10))
1271    ///         .to_debug_string(),
1272    ///     "(3, 7)"
1273    /// );
1274    /// ```
1275    #[inline]
1276    fn ceiling_div_mod(mut self, other: &Self) -> (Self, Self) {
1277        let r = self.ceiling_div_assign_mod(other);
1278        (self, r)
1279    }
1280}
1281
1282impl CeilingDivMod<Integer> for &Integer {
1283    type DivOutput = Integer;
1284    type ModOutput = Integer;
1285
1286    /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
1287    /// by value and returning the quotient and remainder. The quotient is rounded towards positive
1288    /// infinity and the remainder has the opposite sign as the second [`Integer`].
1289    ///
1290    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1291    ///
1292    /// $$
1293    /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
1294    /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
1295    /// $$
1296    ///
1297    /// # Worst-case complexity
1298    /// $T(n) = O(n \log n \log \log n)$
1299    ///
1300    /// $M(n) = O(n \log n)$
1301    ///
1302    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1303    ///
1304    /// # Panics
1305    /// Panics if `other` is zero.
1306    ///
1307    /// # Examples
1308    /// ```
1309    /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
1310    /// use malachite_base::strings::ToDebugString;
1311    /// use malachite_nz::integer::Integer;
1312    ///
1313    /// // 3 * 10 + -7 = 23
1314    /// assert_eq!(
1315    ///     (&Integer::from(23))
1316    ///         .ceiling_div_mod(Integer::from(10))
1317    ///         .to_debug_string(),
1318    ///     "(3, -7)"
1319    /// );
1320    ///
1321    /// // -2 * -10 + 3 = 23
1322    /// assert_eq!(
1323    ///     (&Integer::from(23))
1324    ///         .ceiling_div_mod(Integer::from(-10))
1325    ///         .to_debug_string(),
1326    ///     "(-2, 3)"
1327    /// );
1328    ///
1329    /// // -2 * 10 + -3 = -23
1330    /// assert_eq!(
1331    ///     (&Integer::from(-23))
1332    ///         .ceiling_div_mod(Integer::from(10))
1333    ///         .to_debug_string(),
1334    ///     "(-2, -3)"
1335    /// );
1336    ///
1337    /// // 3 * -10 + 7 = -23
1338    /// assert_eq!(
1339    ///     (&Integer::from(-23))
1340    ///         .ceiling_div_mod(Integer::from(-10))
1341    ///         .to_debug_string(),
1342    ///     "(3, 7)"
1343    /// );
1344    /// ```
1345    fn ceiling_div_mod(self, other: Integer) -> (Integer, Integer) {
1346        let q_sign = self.sign == other.sign;
1347        let (q, r) = if q_sign {
1348            (&self.abs).ceiling_div_neg_mod(other.abs)
1349        } else {
1350            (&self.abs).div_mod(other.abs)
1351        };
1352        (
1353            Integer::from_sign_and_abs(q_sign, q),
1354            Integer::from_sign_and_abs(!other.sign, r),
1355        )
1356    }
1357}
1358
1359impl CeilingDivMod<&Integer> for &Integer {
1360    type DivOutput = Integer;
1361    type ModOutput = Integer;
1362
1363    /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning the
1364    /// quotient and remainder. The quotient is rounded towards positive infinity and the remainder
1365    /// has the opposite sign as the second [`Integer`].
1366    ///
1367    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1368    ///
1369    /// $$
1370    /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
1371    /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
1372    /// $$
1373    ///
1374    /// # Worst-case complexity
1375    /// $T(n) = O(n \log n \log \log n)$
1376    ///
1377    /// $M(n) = O(n \log n)$
1378    ///
1379    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1380    ///
1381    /// # Panics
1382    /// Panics if `other` is zero.
1383    ///
1384    /// # Examples
1385    /// ```
1386    /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
1387    /// use malachite_base::strings::ToDebugString;
1388    /// use malachite_nz::integer::Integer;
1389    ///
1390    /// // 3 * 10 + -7 = 23
1391    /// assert_eq!(
1392    ///     (&Integer::from(23))
1393    ///         .ceiling_div_mod(&Integer::from(10))
1394    ///         .to_debug_string(),
1395    ///     "(3, -7)"
1396    /// );
1397    ///
1398    /// // -2 * -10 + 3 = 23
1399    /// assert_eq!(
1400    ///     (&Integer::from(23))
1401    ///         .ceiling_div_mod(&Integer::from(-10))
1402    ///         .to_debug_string(),
1403    ///     "(-2, 3)"
1404    /// );
1405    ///
1406    /// // -2 * 10 + -3 = -23
1407    /// assert_eq!(
1408    ///     (&Integer::from(-23))
1409    ///         .ceiling_div_mod(&Integer::from(10))
1410    ///         .to_debug_string(),
1411    ///     "(-2, -3)"
1412    /// );
1413    ///
1414    /// // 3 * -10 + 7 = -23
1415    /// assert_eq!(
1416    ///     (&Integer::from(-23))
1417    ///         .ceiling_div_mod(&Integer::from(-10))
1418    ///         .to_debug_string(),
1419    ///     "(3, 7)"
1420    /// );
1421    /// ```
1422    fn ceiling_div_mod(self, other: &Integer) -> (Integer, Integer) {
1423        let q_sign = self.sign == other.sign;
1424        let (q, r) = if q_sign {
1425            (&self.abs).ceiling_div_neg_mod(&other.abs)
1426        } else {
1427            (&self.abs).div_mod(&other.abs)
1428        };
1429        (
1430            Integer::from_sign_and_abs(q_sign, q),
1431            Integer::from_sign_and_abs(!other.sign, r),
1432        )
1433    }
1434}
1435
1436impl CeilingDivAssignMod<Self> for Integer {
1437    type ModOutput = Self;
1438
1439    /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
1440    /// right-hand side by value and returning the remainder. The quotient is rounded towards
1441    /// positive infinity and the remainder has the opposite sign as the second [`Integer`].
1442    ///
1443    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1444    ///
1445    /// $$
1446    /// f(x, y) = x - y\left \lceil\frac{x}{y} \right \rceil,
1447    /// $$
1448    /// $$
1449    /// x \gets \left \lceil \frac{x}{y} \right \rceil.
1450    /// $$
1451    ///
1452    /// # Worst-case complexity
1453    /// $T(n) = O(n \log n \log \log n)$
1454    ///
1455    /// $M(n) = O(n \log n)$
1456    ///
1457    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1458    ///
1459    /// # Panics
1460    /// Panics if `other` is zero.
1461    ///
1462    /// # Examples
1463    /// ```
1464    /// use malachite_base::num::arithmetic::traits::CeilingDivAssignMod;
1465    /// use malachite_nz::integer::Integer;
1466    ///
1467    /// // 3 * 10 + -7 = 23
1468    /// let mut x = Integer::from(23);
1469    /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(10)), -7);
1470    /// assert_eq!(x, 3);
1471    ///
1472    /// // -2 * -10 + 3 = 23
1473    /// let mut x = Integer::from(23);
1474    /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(-10)), 3);
1475    /// assert_eq!(x, -2);
1476    ///
1477    /// // -2 * 10 + -3 = -23
1478    /// let mut x = Integer::from(-23);
1479    /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(10)), -3);
1480    /// assert_eq!(x, -2);
1481    ///
1482    /// // 3 * -10 + 7 = -23
1483    /// let mut x = Integer::from(-23);
1484    /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(-10)), 7);
1485    /// assert_eq!(x, 3);
1486    /// ```
1487    fn ceiling_div_assign_mod(&mut self, other: Self) -> Self {
1488        let r = if self.sign == other.sign {
1489            self.sign = true;
1490            self.abs.ceiling_div_assign_neg_mod(other.abs)
1491        } else {
1492            let r = self.abs.div_assign_mod(other.abs);
1493            self.sign = self.abs == 0u32;
1494            r
1495        };
1496        Self::from_sign_and_abs(!other.sign, r)
1497    }
1498}
1499
1500impl CeilingDivAssignMod<&Self> for Integer {
1501    type ModOutput = Self;
1502
1503    /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
1504    /// right-hand side by reference and returning the remainder. The quotient is rounded towards
1505    /// positive infinity and the remainder has the opposite sign as the second [`Integer`].
1506    ///
1507    /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1508    ///
1509    /// $$
1510    /// f(x, y) = x - y\left \lceil\frac{x}{y} \right \rceil,
1511    /// $$
1512    /// $$
1513    /// x \gets \left \lceil \frac{x}{y} \right \rceil.
1514    /// $$
1515    ///
1516    /// # Worst-case complexity
1517    /// $T(n) = O(n \log n \log \log n)$
1518    ///
1519    /// $M(n) = O(n \log n)$
1520    ///
1521    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1522    ///
1523    /// # Panics
1524    /// Panics if `other` is zero.
1525    ///
1526    /// # Examples
1527    /// ```
1528    /// use malachite_base::num::arithmetic::traits::CeilingDivAssignMod;
1529    /// use malachite_nz::integer::Integer;
1530    ///
1531    /// // 3 * 10 + -7 = 23
1532    /// let mut x = Integer::from(23);
1533    /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(10)), -7);
1534    /// assert_eq!(x, 3);
1535    ///
1536    /// // -2 * -10 + 3 = 23
1537    /// let mut x = Integer::from(23);
1538    /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(-10)), 3);
1539    /// assert_eq!(x, -2);
1540    ///
1541    /// // -2 * 10 + -3 = -23
1542    /// let mut x = Integer::from(-23);
1543    /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(10)), -3);
1544    /// assert_eq!(x, -2);
1545    ///
1546    /// // 3 * -10 + 7 = -23
1547    /// let mut x = Integer::from(-23);
1548    /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(-10)), 7);
1549    /// assert_eq!(x, 3);
1550    /// ```
1551    fn ceiling_div_assign_mod(&mut self, other: &Self) -> Self {
1552        let r = if self.sign == other.sign {
1553            self.sign = true;
1554            self.abs.ceiling_div_assign_neg_mod(&other.abs)
1555        } else {
1556            let r = self.abs.div_assign_mod(&other.abs);
1557            self.sign = self.abs == 0u32;
1558            r
1559        };
1560        Self::from_sign_and_abs(!other.sign, r)
1561    }
1562}