malachite_nz/integer/arithmetic/
mod_op.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 core::ops::{Rem, RemAssign};
11use malachite_base::num::arithmetic::traits::{
12    CeilingMod, CeilingModAssign, Mod, ModAssign, NegMod, NegModAssign,
13};
14
15impl Mod<Integer> for Integer {
16    type Output = Integer;
17
18    /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning just the
19    /// remainder. The remainder has the same sign as the second [`Integer`].
20    ///
21    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
22    /// \leq |r| < |y|$.
23    ///
24    /// $$
25    /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor.
26    /// $$
27    ///
28    /// This function is called `mod_op` rather than `mod` because `mod` is a Rust keyword.
29    ///
30    /// # Worst-case complexity
31    /// $T(n) = O(n \log n \log\log n)$
32    ///
33    /// $M(n) = O(n \log n)$
34    ///
35    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
36    ///
37    /// # Panics
38    /// Panics if `other` is zero.
39    ///
40    /// # Examples
41    /// ```
42    /// use malachite_base::num::arithmetic::traits::Mod;
43    /// use malachite_nz::integer::Integer;
44    ///
45    /// // 2 * 10 + 3 = 23
46    /// assert_eq!(Integer::from(23).mod_op(Integer::from(10)), 3);
47    ///
48    /// // -3 * -10 + -7 = 23
49    /// assert_eq!(Integer::from(23).mod_op(Integer::from(-10)), -7);
50    ///
51    /// // -3 * 10 + 7 = -23
52    /// assert_eq!(Integer::from(-23).mod_op(Integer::from(10)), 7);
53    ///
54    /// // 2 * -10 + -3 = -23
55    /// assert_eq!(Integer::from(-23).mod_op(Integer::from(-10)), -3);
56    /// ```
57    #[inline]
58    fn mod_op(mut self, other: Integer) -> Integer {
59        self.mod_assign(other);
60        self
61    }
62}
63
64impl Mod<&Integer> for Integer {
65    type Output = Integer;
66
67    /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
68    /// reference and returning just the remainder. The remainder has the same sign as the second
69    /// [`Integer`].
70    ///
71    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
72    /// \leq |r| < |y|$.
73    ///
74    /// $$
75    /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor.
76    /// $$
77    ///
78    /// This function is called `mod_op` rather than `mod` because `mod` is a Rust keyword.
79    ///
80    /// # Worst-case complexity
81    /// $T(n) = O(n \log n \log\log n)$
82    ///
83    /// $M(n) = O(n \log n)$
84    ///
85    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
86    ///
87    /// # Panics
88    /// Panics if `other` is zero.
89    ///
90    /// # Examples
91    /// ```
92    /// use malachite_base::num::arithmetic::traits::Mod;
93    /// use malachite_nz::integer::Integer;
94    ///
95    /// // 2 * 10 + 3 = 23
96    /// assert_eq!(Integer::from(23).mod_op(&Integer::from(10)), 3);
97    ///
98    /// // -3 * -10 + -7 = 23
99    /// assert_eq!(Integer::from(23).mod_op(&Integer::from(-10)), -7);
100    ///
101    /// // -3 * 10 + 7 = -23
102    /// assert_eq!(Integer::from(-23).mod_op(&Integer::from(10)), 7);
103    ///
104    /// // 2 * -10 + -3 = -23
105    /// assert_eq!(Integer::from(-23).mod_op(&Integer::from(-10)), -3);
106    /// ```
107    #[inline]
108    fn mod_op(mut self, other: &Integer) -> Integer {
109        self.mod_assign(other);
110        self
111    }
112}
113
114impl Mod<Integer> for &Integer {
115    type Output = Integer;
116
117    /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
118    /// by value and returning just the remainder. The remainder has the same sign as the second
119    /// [`Integer`].
120    ///
121    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
122    /// \leq |r| < |y|$.
123    ///
124    /// $$
125    /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor.
126    /// $$
127    ///
128    /// This function is called `mod_op` rather than `mod` because `mod` is a Rust keyword.
129    ///
130    /// # Worst-case complexity
131    /// $T(n) = O(n \log n \log\log n)$
132    ///
133    /// $M(n) = O(n \log n)$
134    ///
135    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
136    ///
137    /// # Panics
138    /// Panics if `other` is zero.
139    ///
140    /// # Examples
141    /// ```
142    /// use malachite_base::num::arithmetic::traits::Mod;
143    /// use malachite_nz::integer::Integer;
144    ///
145    /// // 2 * 10 + 3 = 23
146    /// assert_eq!((&Integer::from(23)).mod_op(Integer::from(10)), 3);
147    ///
148    /// // -3 * -10 + -7 = 23
149    /// assert_eq!((&Integer::from(23)).mod_op(Integer::from(-10)), -7);
150    ///
151    /// // -3 * 10 + 7 = -23
152    /// assert_eq!((&Integer::from(-23)).mod_op(Integer::from(10)), 7);
153    ///
154    /// // 2 * -10 + -3 = -23
155    /// assert_eq!((&Integer::from(-23)).mod_op(Integer::from(-10)), -3);
156    /// ```
157    fn mod_op(self, other: Integer) -> Integer {
158        Integer::from_sign_and_abs(
159            other.sign,
160            if self.sign == other.sign {
161                &self.abs % other.abs
162            } else {
163                (&self.abs).neg_mod(other.abs)
164            },
165        )
166    }
167}
168
169impl Mod<&Integer> for &Integer {
170    type Output = Integer;
171
172    /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning just
173    /// the remainder. The remainder has the same sign as the second [`Integer`].
174    ///
175    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
176    /// \leq |r| < |y|$.
177    ///
178    /// $$
179    /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor.
180    /// $$
181    ///
182    /// This function is called `mod_op` rather than `mod` because `mod` is a Rust keyword.
183    ///
184    /// # Worst-case complexity
185    /// $T(n) = O(n \log n \log\log n)$
186    ///
187    /// $M(n) = O(n \log n)$
188    ///
189    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
190    ///
191    /// # Panics
192    /// Panics if `other` is zero.
193    ///
194    /// # Examples
195    /// ```
196    /// use malachite_base::num::arithmetic::traits::Mod;
197    /// use malachite_nz::integer::Integer;
198    ///
199    /// // 2 * 10 + 3 = 23
200    /// assert_eq!((&Integer::from(23)).mod_op(&Integer::from(10)), 3);
201    ///
202    /// // -3 * -10 + -7 = 23
203    /// assert_eq!((&Integer::from(23)).mod_op(&Integer::from(-10)), -7);
204    ///
205    /// // -3 * 10 + 7 = -23
206    /// assert_eq!((&Integer::from(-23)).mod_op(&Integer::from(10)), 7);
207    ///
208    /// // 2 * -10 + -3 = -23
209    /// assert_eq!((&Integer::from(-23)).mod_op(&Integer::from(-10)), -3);
210    /// ```
211    fn mod_op(self, other: &Integer) -> Integer {
212        Integer::from_sign_and_abs(
213            other.sign,
214            if self.sign == other.sign {
215                &self.abs % &other.abs
216            } else {
217                (&self.abs).neg_mod(&other.abs)
218            },
219        )
220    }
221}
222
223impl ModAssign<Integer> for Integer {
224    /// Divides an [`Integer`] by another [`Integer`], taking the second [`Integer`] by value and
225    /// replacing the first by the remainder. The remainder has the same sign as the second
226    /// [`Integer`].
227    ///
228    /// If the quotient were computed, he quotient and remainder would satisfy $x = qy + r$ and $0
229    /// \leq |r| < |y|$.
230    ///
231    /// $$
232    /// x \gets x - y\left \lfloor \frac{x}{y} \right \rfloor.
233    /// $$
234    ///
235    /// # Worst-case complexity
236    /// $T(n) = O(n \log n \log\log n)$
237    ///
238    /// $M(n) = O(n \log n)$
239    ///
240    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
241    ///
242    /// # Panics
243    /// Panics if `other` is zero.
244    ///
245    /// # Examples
246    /// ```
247    /// use malachite_base::num::arithmetic::traits::ModAssign;
248    /// use malachite_nz::integer::Integer;
249    ///
250    /// // 2 * 10 + 3 = 23
251    /// let mut x = Integer::from(23);
252    /// x.mod_assign(Integer::from(10));
253    /// assert_eq!(x, 3);
254    ///
255    /// // -3 * -10 + -7 = 23
256    /// let mut x = Integer::from(23);
257    /// x.mod_assign(Integer::from(-10));
258    /// assert_eq!(x, -7);
259    ///
260    /// // -3 * 10 + 7 = -23
261    /// let mut x = Integer::from(-23);
262    /// x.mod_assign(Integer::from(10));
263    /// assert_eq!(x, 7);
264    ///
265    /// // 2 * -10 + -3 = -23
266    /// let mut x = Integer::from(-23);
267    /// x.mod_assign(Integer::from(-10));
268    /// assert_eq!(x, -3);
269    /// ```
270    fn mod_assign(&mut self, other: Integer) {
271        if self.sign == other.sign {
272            self.abs %= other.abs;
273        } else {
274            self.abs.neg_mod_assign(other.abs);
275        };
276        self.sign = other.sign || self.abs == 0;
277    }
278}
279
280impl ModAssign<&Integer> for Integer {
281    /// Divides an [`Integer`] by another [`Integer`], taking the second [`Integer`] by reference
282    /// and replacing the first by the remainder. The remainder has the same sign as the second
283    /// [`Integer`].
284    ///
285    /// If the quotient were computed, he quotient and remainder would satisfy $x = qy + r$ and $0
286    /// \leq |r| < |y|$.
287    ///
288    /// $$
289    /// x \gets x - y\left \lfloor \frac{x}{y} \right \rfloor.
290    /// $$
291    ///
292    /// # Worst-case complexity
293    /// $T(n) = O(n \log n \log\log n)$
294    ///
295    /// $M(n) = O(n \log n)$
296    ///
297    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
298    ///
299    /// # Panics
300    /// Panics if `other` is zero.
301    ///
302    /// # Examples
303    /// ```
304    /// use malachite_base::num::arithmetic::traits::ModAssign;
305    /// use malachite_nz::integer::Integer;
306    ///
307    /// // 2 * 10 + 3 = 23
308    /// let mut x = Integer::from(23);
309    /// x.mod_assign(&Integer::from(10));
310    /// assert_eq!(x, 3);
311    ///
312    /// // -3 * -10 + -7 = 23
313    /// let mut x = Integer::from(23);
314    /// x.mod_assign(&Integer::from(-10));
315    /// assert_eq!(x, -7);
316    ///
317    /// // -3 * 10 + 7 = -23
318    /// let mut x = Integer::from(-23);
319    /// x.mod_assign(&Integer::from(10));
320    /// assert_eq!(x, 7);
321    ///
322    /// // 2 * -10 + -3 = -23
323    /// let mut x = Integer::from(-23);
324    /// x.mod_assign(&Integer::from(-10));
325    /// assert_eq!(x, -3);
326    /// ```
327    fn mod_assign(&mut self, other: &Integer) {
328        if self.sign == other.sign {
329            self.abs %= &other.abs;
330        } else {
331            self.abs.neg_mod_assign(&other.abs);
332        };
333        self.sign = other.sign || self.abs == 0;
334    }
335}
336
337impl Rem<Integer> for Integer {
338    type Output = Integer;
339
340    /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning just the
341    /// remainder. The remainder has the same sign as the first [`Integer`].
342    ///
343    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
344    /// \leq |r| < |y|$.
345    ///
346    /// $$
347    /// f(x, y) = x - y \operatorname{sgn}(xy)
348    ///     \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
349    /// $$
350    ///
351    /// # Worst-case complexity
352    /// $T(n) = O(n \log n \log\log n)$
353    ///
354    /// $M(n) = O(n \log n)$
355    ///
356    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
357    ///
358    /// # Panics
359    /// Panics if `other` is zero.
360    ///
361    /// # Examples
362    /// ```
363    /// use malachite_nz::integer::Integer;
364    ///
365    /// // 2 * 10 + 3 = 23
366    /// assert_eq!(Integer::from(23) % Integer::from(10), 3);
367    ///
368    /// // -3 * -10 + -7 = 23
369    /// assert_eq!(Integer::from(23) % Integer::from(-10), 3);
370    ///
371    /// // -3 * 10 + 7 = -23
372    /// assert_eq!(Integer::from(-23) % Integer::from(10), -3);
373    ///
374    /// // 2 * -10 + -3 = -23
375    /// assert_eq!(Integer::from(-23) % Integer::from(-10), -3);
376    /// ```
377    #[inline]
378    fn rem(mut self, other: Integer) -> Integer {
379        self %= other;
380        self
381    }
382}
383
384impl Rem<&Integer> for Integer {
385    type Output = Integer;
386
387    /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
388    /// reference and returning just the remainder. The remainder has the same sign as the first
389    /// [`Integer`].
390    ///
391    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
392    /// \leq |r| < |y|$.
393    ///
394    /// $$
395    /// f(x, y) = x - y \operatorname{sgn}(xy)
396    ///     \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
397    /// $$
398    ///
399    /// # Worst-case complexity
400    /// $T(n) = O(n \log n \log\log n)$
401    ///
402    /// $M(n) = O(n \log n)$
403    ///
404    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
405    ///
406    /// # Panics
407    /// Panics if `other` is zero.
408    ///
409    /// # Examples
410    /// ```
411    /// use malachite_nz::integer::Integer;
412    ///
413    /// // 2 * 10 + 3 = 23
414    /// assert_eq!(Integer::from(23) % &Integer::from(10), 3);
415    ///
416    /// // -3 * -10 + -7 = 23
417    /// assert_eq!(Integer::from(23) % &Integer::from(-10), 3);
418    ///
419    /// // -3 * 10 + 7 = -23
420    /// assert_eq!(Integer::from(-23) % &Integer::from(10), -3);
421    ///
422    /// // 2 * -10 + -3 = -23
423    /// assert_eq!(Integer::from(-23) % &Integer::from(-10), -3);
424    /// ```
425    #[inline]
426    fn rem(mut self, other: &Integer) -> Integer {
427        self %= other;
428        self
429    }
430}
431
432impl Rem<Integer> for &Integer {
433    type Output = Integer;
434
435    /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
436    /// by value and returning just the remainder. The remainder has the same sign as the first
437    /// [`Integer`].
438    ///
439    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
440    /// \leq |r| < |y|$.
441    ///
442    /// $$
443    /// f(x, y) = x - y \operatorname{sgn}(xy)
444    ///     \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
445    /// $$
446    ///
447    /// # Worst-case complexity
448    /// $T(n) = O(n \log n \log\log n)$
449    ///
450    /// $M(n) = O(n \log n)$
451    ///
452    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
453    ///
454    /// # Panics
455    /// Panics if `other` is zero.
456    ///
457    /// # Examples
458    /// ```
459    /// use malachite_nz::integer::Integer;
460    ///
461    /// // 2 * 10 + 3 = 23
462    /// assert_eq!(&Integer::from(23) % Integer::from(10), 3);
463    ///
464    /// // -3 * -10 + -7 = 23
465    /// assert_eq!(&Integer::from(23) % Integer::from(-10), 3);
466    ///
467    /// // -3 * 10 + 7 = -23
468    /// assert_eq!(&Integer::from(-23) % Integer::from(10), -3);
469    ///
470    /// // 2 * -10 + -3 = -23
471    /// assert_eq!(&Integer::from(-23) % Integer::from(-10), -3);
472    /// ```
473    #[inline]
474    fn rem(self, other: Integer) -> Integer {
475        Integer::from_sign_and_abs(self.sign, &self.abs % other.abs)
476    }
477}
478
479impl Rem<&Integer> for &Integer {
480    type Output = Integer;
481
482    /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning just
483    /// the remainder. The remainder has the same sign as the first [`Integer`].
484    ///
485    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
486    /// \leq |r| < |y|$.
487    ///
488    /// $$
489    /// f(x, y) = x - y \operatorname{sgn}(xy)
490    ///     \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
491    /// $$
492    ///
493    /// # Worst-case complexity
494    /// $T(n) = O(n \log n \log\log n)$
495    ///
496    /// $M(n) = O(n \log n)$
497    ///
498    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
499    ///
500    /// # Panics
501    /// Panics if `other` is zero.
502    ///
503    /// # Examples
504    /// ```
505    /// use malachite_nz::integer::Integer;
506    ///
507    /// // 2 * 10 + 3 = 23
508    /// assert_eq!(&Integer::from(23) % &Integer::from(10), 3);
509    ///
510    /// // -3 * -10 + -7 = 23
511    /// assert_eq!(&Integer::from(23) % &Integer::from(-10), 3);
512    ///
513    /// // -3 * 10 + 7 = -23
514    /// assert_eq!(&Integer::from(-23) % &Integer::from(10), -3);
515    ///
516    /// // 2 * -10 + -3 = -23
517    /// assert_eq!(&Integer::from(-23) % &Integer::from(-10), -3);
518    /// ```
519    #[inline]
520    fn rem(self, other: &Integer) -> Integer {
521        Integer::from_sign_and_abs(self.sign, &self.abs % &other.abs)
522    }
523}
524
525impl RemAssign<Integer> for Integer {
526    /// Divides an [`Integer`] by another [`Integer`], taking the second [`Integer`] by value and
527    /// replacing the first by the remainder. The remainder has the same sign as the first
528    /// [`Integer`].
529    ///
530    /// If the quotient were computed, he quotient and remainder would satisfy $x = qy + r$ and $0
531    /// \leq |r| < |y|$.
532    ///
533    /// $$
534    /// x \gets x - y \operatorname{sgn}(xy)
535    ///     \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
536    /// $$
537    ///
538    /// # Worst-case complexity
539    /// $T(n) = O(n \log n \log\log n)$
540    ///
541    /// $M(n) = O(n \log n)$
542    ///
543    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
544    ///
545    /// # Panics
546    /// Panics if `other` is zero.
547    ///
548    /// # Examples
549    /// ```
550    /// use malachite_nz::integer::Integer;
551    ///
552    /// // 2 * 10 + 3 = 23
553    /// let mut x = Integer::from(23);
554    /// x %= Integer::from(10);
555    /// assert_eq!(x, 3);
556    ///
557    /// // -3 * -10 + -7 = 23
558    /// let mut x = Integer::from(23);
559    /// x %= Integer::from(-10);
560    /// assert_eq!(x, 3);
561    ///
562    /// // -3 * 10 + 7 = -23
563    /// let mut x = Integer::from(-23);
564    /// x %= Integer::from(10);
565    /// assert_eq!(x, -3);
566    ///
567    /// // 2 * -10 + -3 = -23
568    /// let mut x = Integer::from(-23);
569    /// x %= Integer::from(-10);
570    /// assert_eq!(x, -3);
571    /// ```
572    #[inline]
573    fn rem_assign(&mut self, other: Integer) {
574        self.abs %= other.abs;
575        self.sign = self.sign || self.abs == 0;
576    }
577}
578
579impl RemAssign<&Integer> for Integer {
580    /// Divides an [`Integer`] by another [`Integer`], taking the second [`Integer`] by reference
581    /// and replacing the first by the remainder. The remainder has the same sign as the first
582    /// [`Integer`].
583    ///
584    /// If the quotient were computed, he quotient and remainder would satisfy $x = qy + r$ and $0
585    /// \leq |r| < |y|$.
586    ///
587    /// $$
588    /// x \gets x - y \operatorname{sgn}(xy)
589    ///     \left \lfloor \left | \frac{x}{y} \right | \right \rfloor.
590    /// $$
591    ///
592    /// # Worst-case complexity
593    /// $T(n) = O(n \log n \log\log n)$
594    ///
595    /// $M(n) = O(n \log n)$
596    ///
597    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
598    ///
599    /// # Panics
600    /// Panics if `other` is zero.
601    ///
602    /// # Examples
603    /// ```
604    /// use malachite_nz::integer::Integer;
605    ///
606    /// // 2 * 10 + 3 = 23
607    /// let mut x = Integer::from(23);
608    /// x %= &Integer::from(10);
609    /// assert_eq!(x, 3);
610    ///
611    /// // -3 * -10 + -7 = 23
612    /// let mut x = Integer::from(23);
613    /// x %= &Integer::from(-10);
614    /// assert_eq!(x, 3);
615    ///
616    /// // -3 * 10 + 7 = -23
617    /// let mut x = Integer::from(-23);
618    /// x %= &Integer::from(10);
619    /// assert_eq!(x, -3);
620    ///
621    /// // 2 * -10 + -3 = -23
622    /// let mut x = Integer::from(-23);
623    /// x %= &Integer::from(-10);
624    /// assert_eq!(x, -3);
625    /// ```
626    #[inline]
627    fn rem_assign(&mut self, other: &Integer) {
628        self.abs %= &other.abs;
629        self.sign = self.sign || self.abs == 0;
630    }
631}
632
633impl CeilingMod<Integer> for Integer {
634    type Output = Integer;
635
636    /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning just the
637    /// remainder. The remainder has the opposite sign as the second [`Integer`].
638    ///
639    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
640    /// \leq |r| < |y|$.
641    ///
642    /// $$
643    /// f(x, y) =  x - y\left \lceil \frac{x}{y} \right \rceil.
644    /// $$
645    ///
646    /// # Worst-case complexity
647    /// $T(n) = O(n \log n \log\log n)$
648    ///
649    /// $M(n) = O(n \log n)$
650    ///
651    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
652    ///
653    /// # Panics
654    /// Panics if `other` is zero.
655    ///
656    /// # Examples
657    /// ```
658    /// use malachite_base::num::arithmetic::traits::CeilingMod;
659    /// use malachite_nz::integer::Integer;
660    ///
661    /// // 2 * 10 + 3 = 23
662    /// assert_eq!(Integer::from(23).ceiling_mod(Integer::from(10)), -7);
663    ///
664    /// // -3 * -10 + -7 = 23
665    /// assert_eq!(Integer::from(23).ceiling_mod(Integer::from(-10)), 3);
666    ///
667    /// // -3 * 10 + 7 = -23
668    /// assert_eq!(Integer::from(-23).ceiling_mod(Integer::from(10)), -3);
669    ///
670    /// // 2 * -10 + -3 = -23
671    /// assert_eq!(Integer::from(-23).ceiling_mod(Integer::from(-10)), 7);
672    /// ```
673    #[inline]
674    fn ceiling_mod(mut self, other: Integer) -> Integer {
675        self.ceiling_mod_assign(other);
676        self
677    }
678}
679
680impl CeilingMod<&Integer> for Integer {
681    type Output = Integer;
682
683    /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
684    /// reference and returning just the remainder. The remainder has the opposite sign as the
685    /// second [`Integer`].
686    ///
687    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
688    /// \leq |r| < |y|$.
689    ///
690    /// $$
691    /// f(x, y) =  x - y\left \lceil \frac{x}{y} \right \rceil.
692    /// $$
693    ///
694    /// # Worst-case complexity
695    /// $T(n) = O(n \log n \log\log n)$
696    ///
697    /// $M(n) = O(n \log n)$
698    ///
699    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
700    ///
701    /// # Panics
702    /// Panics if `other` is zero.
703    ///
704    /// # Examples
705    /// ```
706    /// use malachite_base::num::arithmetic::traits::CeilingMod;
707    /// use malachite_nz::integer::Integer;
708    ///
709    /// // 2 * 10 + 3 = 23
710    /// assert_eq!(Integer::from(23).ceiling_mod(&Integer::from(10)), -7);
711    ///
712    /// // -3 * -10 + -7 = 23
713    /// assert_eq!(Integer::from(23).ceiling_mod(&Integer::from(-10)), 3);
714    ///
715    /// // -3 * 10 + 7 = -23
716    /// assert_eq!(Integer::from(-23).ceiling_mod(&Integer::from(10)), -3);
717    ///
718    /// // 2 * -10 + -3 = -23
719    /// assert_eq!(Integer::from(-23).ceiling_mod(&Integer::from(-10)), 7);
720    /// ```
721    #[inline]
722    fn ceiling_mod(mut self, other: &Integer) -> Integer {
723        self.ceiling_mod_assign(other);
724        self
725    }
726}
727
728impl CeilingMod<Integer> for &Integer {
729    type Output = Integer;
730
731    /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
732    /// by value and returning just the remainder. The remainder has the opposite sign as the second
733    /// [`Integer`].
734    ///
735    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
736    /// \leq |r| < |y|$.
737    ///
738    /// $$
739    /// f(x, y) =  x - y\left \lceil \frac{x}{y} \right \rceil.
740    /// $$
741    ///
742    /// # Worst-case complexity
743    /// $T(n) = O(n \log n \log\log n)$
744    ///
745    /// $M(n) = O(n \log n)$
746    ///
747    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
748    ///
749    /// # Panics
750    /// Panics if `other` is zero.
751    ///
752    /// # Examples
753    /// ```
754    /// use malachite_base::num::arithmetic::traits::CeilingMod;
755    /// use malachite_nz::integer::Integer;
756    ///
757    /// // 2 * 10 + 3 = 23
758    /// assert_eq!((&Integer::from(23)).ceiling_mod(Integer::from(10)), -7);
759    ///
760    /// // -3 * -10 + -7 = 23
761    /// assert_eq!((&Integer::from(23)).ceiling_mod(Integer::from(-10)), 3);
762    ///
763    /// // -3 * 10 + 7 = -23
764    /// assert_eq!((&Integer::from(-23)).ceiling_mod(Integer::from(10)), -3);
765    ///
766    /// // 2 * -10 + -3 = -23
767    /// assert_eq!((&Integer::from(-23)).ceiling_mod(Integer::from(-10)), 7);
768    /// ```
769    fn ceiling_mod(self, other: Integer) -> Integer {
770        Integer::from_sign_and_abs(
771            !other.sign,
772            if self.sign == other.sign {
773                (&self.abs).neg_mod(other.abs)
774            } else {
775                &self.abs % other.abs
776            },
777        )
778    }
779}
780
781impl CeilingMod<&Integer> for &Integer {
782    type Output = Integer;
783
784    /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning just
785    /// the remainder. The remainder has the opposite sign as the second [`Integer`].
786    ///
787    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
788    /// \leq |r| < |y|$.
789    ///
790    /// $$
791    /// f(x, y) =  x - y\left \lceil \frac{x}{y} \right \rceil.
792    /// $$
793    ///
794    /// # Worst-case complexity
795    /// $T(n) = O(n \log n \log\log n)$
796    ///
797    /// $M(n) = O(n \log n)$
798    ///
799    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
800    ///
801    /// # Panics
802    /// Panics if `other` is zero.
803    ///
804    /// # Examples
805    /// ```
806    /// use malachite_base::num::arithmetic::traits::CeilingMod;
807    /// use malachite_nz::integer::Integer;
808    ///
809    /// // 2 * 10 + 3 = 23
810    /// assert_eq!((&Integer::from(23)).ceiling_mod(&Integer::from(10)), -7);
811    ///
812    /// // -3 * -10 + -7 = 23
813    /// assert_eq!((&Integer::from(23)).ceiling_mod(&Integer::from(-10)), 3);
814    ///
815    /// // -3 * 10 + 7 = -23
816    /// assert_eq!((&Integer::from(-23)).ceiling_mod(&Integer::from(10)), -3);
817    ///
818    /// // 2 * -10 + -3 = -23
819    /// assert_eq!((&Integer::from(-23)).ceiling_mod(&Integer::from(-10)), 7);
820    /// ```
821    fn ceiling_mod(self, other: &Integer) -> Integer {
822        Integer::from_sign_and_abs(
823            !other.sign,
824            if self.sign == other.sign {
825                (&self.abs).neg_mod(&other.abs)
826            } else {
827                &self.abs % &other.abs
828            },
829        )
830    }
831}
832
833impl CeilingModAssign<Integer> for Integer {
834    /// Divides an [`Integer`] by another [`Integer`], taking the [`Integer`] on the right-hand side
835    /// by value and replacing the first number by the remainder. The remainder has the opposite
836    /// sign as the second number.
837    ///
838    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
839    /// \leq |r| < |y|$.
840    ///
841    /// $$
842    /// x \gets x - y\left \lceil\frac{x}{y} \right \rceil.
843    /// $$
844    ///
845    /// # Worst-case complexity
846    /// $T(n) = O(n \log n \log\log n)$
847    ///
848    /// $M(n) = O(n \log n)$
849    ///
850    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
851    ///
852    /// # Panics
853    /// Panics if `other` is zero.
854    ///
855    /// # Examples
856    /// ```
857    /// use malachite_base::num::arithmetic::traits::CeilingModAssign;
858    /// use malachite_nz::integer::Integer;
859    ///
860    /// // 2 * 10 + 3 = 23
861    /// let mut x = Integer::from(23);
862    /// x.ceiling_mod_assign(Integer::from(10));
863    /// assert_eq!(x, -7);
864    ///
865    /// // -3 * -10 + -7 = 23
866    /// let mut x = Integer::from(23);
867    /// x.ceiling_mod_assign(Integer::from(-10));
868    /// assert_eq!(x, 3);
869    ///
870    /// // -3 * 10 + 7 = -23
871    /// let mut x = Integer::from(-23);
872    /// x.ceiling_mod_assign(Integer::from(10));
873    /// assert_eq!(x, -3);
874    ///
875    /// // 2 * -10 + -3 = -23
876    /// let mut x = Integer::from(-23);
877    /// x.ceiling_mod_assign(Integer::from(-10));
878    /// assert_eq!(x, 7);
879    /// ```
880    fn ceiling_mod_assign(&mut self, other: Integer) {
881        if self.sign == other.sign {
882            self.abs.neg_mod_assign(other.abs);
883        } else {
884            self.abs %= other.abs;
885        };
886        self.sign = !other.sign || self.abs == 0;
887    }
888}
889
890impl CeilingModAssign<&Integer> for Integer {
891    /// Divides an [`Integer`] by another [`Integer`], taking the [`Integer`] on the right-hand side
892    /// by reference and replacing the first number by the remainder. The remainder has the opposite
893    /// sign as the second number.
894    ///
895    /// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
896    /// \leq |r| < |y|$.
897    ///
898    /// $$
899    /// x \gets x - y\left \lceil\frac{x}{y} \right \rceil.
900    /// $$
901    ///
902    /// # Worst-case complexity
903    /// $T(n) = O(n \log n \log\log n)$
904    ///
905    /// $M(n) = O(n \log n)$
906    ///
907    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
908    ///
909    /// # Panics
910    /// Panics if `other` is zero.
911    ///
912    /// # Examples
913    /// ```
914    /// use malachite_base::num::arithmetic::traits::CeilingModAssign;
915    /// use malachite_nz::integer::Integer;
916    ///
917    /// // 2 * 10 + 3 = 23
918    /// let mut x = Integer::from(23);
919    /// x.ceiling_mod_assign(&Integer::from(10));
920    /// assert_eq!(x, -7);
921    ///
922    /// // -3 * -10 + -7 = 23
923    /// let mut x = Integer::from(23);
924    /// x.ceiling_mod_assign(&Integer::from(-10));
925    /// assert_eq!(x, 3);
926    ///
927    /// // -3 * 10 + 7 = -23
928    /// let mut x = Integer::from(-23);
929    /// x.ceiling_mod_assign(&Integer::from(10));
930    /// assert_eq!(x, -3);
931    ///
932    /// // 2 * -10 + -3 = -23
933    /// let mut x = Integer::from(-23);
934    /// x.ceiling_mod_assign(&Integer::from(-10));
935    /// assert_eq!(x, 7);
936    /// ```
937    fn ceiling_mod_assign(&mut self, other: &Integer) {
938        if self.sign == other.sign {
939            self.abs.neg_mod_assign(&other.abs);
940        } else {
941            self.abs %= &other.abs;
942        };
943        self.sign = !other.sign || self.abs == 0;
944    }
945}