malachite_nz/integer/arithmetic/div_mod.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use malachite_base::num::arithmetic::traits::{
11 CeilingDivAssignMod, CeilingDivAssignNegMod, CeilingDivMod, CeilingDivNegMod, DivAssignMod,
12 DivAssignRem, DivMod, DivRem,
13};
14
15impl DivMod<Integer> for Integer {
16 type DivOutput = Integer;
17 type ModOutput = Integer;
18
19 /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning the
20 /// quotient and remainder. The quotient is rounded towards negative infinity, and the remainder
21 /// has the same sign as the second [`Integer`].
22 ///
23 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
24 ///
25 /// $$
26 /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
27 /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
28 /// $$
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::DivMod;
43 /// use malachite_base::strings::ToDebugString;
44 /// use malachite_nz::integer::Integer;
45 ///
46 /// // 2 * 10 + 3 = 23
47 /// assert_eq!(
48 /// Integer::from(23)
49 /// .div_mod(Integer::from(10))
50 /// .to_debug_string(),
51 /// "(2, 3)"
52 /// );
53 ///
54 /// // -3 * -10 + -7 = 23
55 /// assert_eq!(
56 /// Integer::from(23)
57 /// .div_mod(Integer::from(-10))
58 /// .to_debug_string(),
59 /// "(-3, -7)"
60 /// );
61 ///
62 /// // -3 * 10 + 7 = -23
63 /// assert_eq!(
64 /// Integer::from(-23)
65 /// .div_mod(Integer::from(10))
66 /// .to_debug_string(),
67 /// "(-3, 7)"
68 /// );
69 ///
70 /// // 2 * -10 + -3 = -23
71 /// assert_eq!(
72 /// Integer::from(-23)
73 /// .div_mod(Integer::from(-10))
74 /// .to_debug_string(),
75 /// "(2, -3)"
76 /// );
77 /// ```
78 #[inline]
79 fn div_mod(mut self, other: Integer) -> (Integer, Integer) {
80 let r = self.div_assign_mod(other);
81 (self, r)
82 }
83}
84
85impl DivMod<&Integer> for Integer {
86 type DivOutput = Integer;
87 type ModOutput = Integer;
88
89 /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
90 /// reference and returning the quotient and remainder. The quotient is rounded towards negative
91 /// infinity, and the remainder has the same sign as the second [`Integer`].
92 ///
93 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
94 ///
95 /// $$
96 /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
97 /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
98 /// $$
99 ///
100 /// # Worst-case complexity
101 /// $T(n) = O(n \log n \log \log n)$
102 ///
103 /// $M(n) = O(n \log n)$
104 ///
105 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
106 ///
107 /// # Panics
108 /// Panics if `other` is zero.
109 ///
110 /// # Examples
111 /// ```
112 /// use malachite_base::num::arithmetic::traits::DivMod;
113 /// use malachite_base::strings::ToDebugString;
114 /// use malachite_nz::integer::Integer;
115 ///
116 /// // 2 * 10 + 3 = 23
117 /// assert_eq!(
118 /// Integer::from(23)
119 /// .div_mod(&Integer::from(10))
120 /// .to_debug_string(),
121 /// "(2, 3)"
122 /// );
123 ///
124 /// // -3 * -10 + -7 = 23
125 /// assert_eq!(
126 /// Integer::from(23)
127 /// .div_mod(&Integer::from(-10))
128 /// .to_debug_string(),
129 /// "(-3, -7)"
130 /// );
131 ///
132 /// // -3 * 10 + 7 = -23
133 /// assert_eq!(
134 /// Integer::from(-23)
135 /// .div_mod(&Integer::from(10))
136 /// .to_debug_string(),
137 /// "(-3, 7)"
138 /// );
139 ///
140 /// // 2 * -10 + -3 = -23
141 /// assert_eq!(
142 /// Integer::from(-23)
143 /// .div_mod(&Integer::from(-10))
144 /// .to_debug_string(),
145 /// "(2, -3)"
146 /// );
147 /// ```
148 #[inline]
149 fn div_mod(mut self, other: &Integer) -> (Integer, Integer) {
150 let r = self.div_assign_mod(other);
151 (self, r)
152 }
153}
154
155impl DivMod<Integer> for &Integer {
156 type DivOutput = Integer;
157 type ModOutput = Integer;
158
159 /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
160 /// by value and returning the quotient and remainder. The quotient is rounded towards negative
161 /// infinity, and the remainder has the same sign as the second [`Integer`].
162 ///
163 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
164 ///
165 /// $$
166 /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
167 /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
168 /// $$
169 ///
170 /// # Worst-case complexity
171 /// $T(n) = O(n \log n \log \log n)$
172 ///
173 /// $M(n) = O(n \log n)$
174 ///
175 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
176 ///
177 /// # Panics
178 /// Panics if `other` is zero.
179 ///
180 /// # Examples
181 /// ```
182 /// use malachite_base::num::arithmetic::traits::DivMod;
183 /// use malachite_base::strings::ToDebugString;
184 /// use malachite_nz::integer::Integer;
185 ///
186 /// // 2 * 10 + 3 = 23
187 /// assert_eq!(
188 /// (&Integer::from(23))
189 /// .div_mod(Integer::from(10))
190 /// .to_debug_string(),
191 /// "(2, 3)"
192 /// );
193 ///
194 /// // -3 * -10 + -7 = 23
195 /// assert_eq!(
196 /// (&Integer::from(23))
197 /// .div_mod(Integer::from(-10))
198 /// .to_debug_string(),
199 /// "(-3, -7)"
200 /// );
201 ///
202 /// // -3 * 10 + 7 = -23
203 /// assert_eq!(
204 /// (&Integer::from(-23))
205 /// .div_mod(Integer::from(10))
206 /// .to_debug_string(),
207 /// "(-3, 7)"
208 /// );
209 ///
210 /// // 2 * -10 + -3 = -23
211 /// assert_eq!(
212 /// (&Integer::from(-23))
213 /// .div_mod(Integer::from(-10))
214 /// .to_debug_string(),
215 /// "(2, -3)"
216 /// );
217 /// ```
218 fn div_mod(self, other: Integer) -> (Integer, Integer) {
219 let q_sign = self.sign == other.sign;
220 let (q, r) = if q_sign {
221 (&self.abs).div_mod(other.abs)
222 } else {
223 (&self.abs).ceiling_div_neg_mod(other.abs)
224 };
225 (
226 Integer::from_sign_and_abs(q_sign, q),
227 Integer::from_sign_and_abs(other.sign, r),
228 )
229 }
230}
231
232impl DivMod<&Integer> for &Integer {
233 type DivOutput = Integer;
234 type ModOutput = Integer;
235
236 /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning the
237 /// quotient and remainder. The quotient is rounded towards negative infinity, and the remainder
238 /// has the same sign as the second [`Integer`].
239 ///
240 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
241 ///
242 /// $$
243 /// f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space
244 /// x - y\left \lfloor \frac{x}{y} \right \rfloor \right ).
245 /// $$
246 ///
247 /// # Worst-case complexity
248 /// $T(n) = O(n \log n \log \log n)$
249 ///
250 /// $M(n) = O(n \log n)$
251 ///
252 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
253 ///
254 /// # Panics
255 /// Panics if `other` is zero.
256 ///
257 /// # Examples
258 /// ```
259 /// use malachite_base::num::arithmetic::traits::DivMod;
260 /// use malachite_base::strings::ToDebugString;
261 /// use malachite_nz::integer::Integer;
262 ///
263 /// // 2 * 10 + 3 = 23
264 /// assert_eq!(
265 /// (&Integer::from(23))
266 /// .div_mod(&Integer::from(10))
267 /// .to_debug_string(),
268 /// "(2, 3)"
269 /// );
270 ///
271 /// // -3 * -10 + -7 = 23
272 /// assert_eq!(
273 /// (&Integer::from(23))
274 /// .div_mod(&Integer::from(-10))
275 /// .to_debug_string(),
276 /// "(-3, -7)"
277 /// );
278 ///
279 /// // -3 * 10 + 7 = -23
280 /// assert_eq!(
281 /// (&Integer::from(-23))
282 /// .div_mod(&Integer::from(10))
283 /// .to_debug_string(),
284 /// "(-3, 7)"
285 /// );
286 ///
287 /// // 2 * -10 + -3 = -23
288 /// assert_eq!(
289 /// (&Integer::from(-23))
290 /// .div_mod(&Integer::from(-10))
291 /// .to_debug_string(),
292 /// "(2, -3)"
293 /// );
294 /// ```
295 fn div_mod(self, other: &Integer) -> (Integer, Integer) {
296 let q_sign = self.sign == other.sign;
297 let (q, r) = if q_sign {
298 (&self.abs).div_mod(&other.abs)
299 } else {
300 (&self.abs).ceiling_div_neg_mod(&other.abs)
301 };
302 (
303 Integer::from_sign_and_abs(q_sign, q),
304 Integer::from_sign_and_abs(other.sign, r),
305 )
306 }
307}
308
309impl DivAssignMod<Integer> for Integer {
310 type ModOutput = Integer;
311
312 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
313 /// right-hand side by value and returning the remainder. The quotient is rounded towards
314 /// negative infinity, and the remainder has the same sign as the second [`Integer`].
315 ///
316 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
317 ///
318 /// $$
319 /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor,
320 /// $$
321 /// $$
322 /// x \gets \left \lfloor \frac{x}{y} \right \rfloor.
323 /// $$
324 ///
325 /// # Worst-case complexity
326 /// $T(n) = O(n \log n \log \log n)$
327 ///
328 /// $M(n) = O(n \log n)$
329 ///
330 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
331 ///
332 /// # Panics
333 /// Panics if `other` is zero.
334 ///
335 /// # Examples
336 /// ```
337 /// use malachite_base::num::arithmetic::traits::DivAssignMod;
338 /// use malachite_nz::integer::Integer;
339 ///
340 /// // 2 * 10 + 3 = 23
341 /// let mut x = Integer::from(23);
342 /// assert_eq!(x.div_assign_mod(Integer::from(10)), 3);
343 /// assert_eq!(x, 2);
344 ///
345 /// // -3 * -10 + -7 = 23
346 /// let mut x = Integer::from(23);
347 /// assert_eq!(x.div_assign_mod(Integer::from(-10)), -7);
348 /// assert_eq!(x, -3);
349 ///
350 /// // -3 * 10 + 7 = -23
351 /// let mut x = Integer::from(-23);
352 /// assert_eq!(x.div_assign_mod(Integer::from(10)), 7);
353 /// assert_eq!(x, -3);
354 ///
355 /// // 2 * -10 + -3 = -23
356 /// let mut x = Integer::from(-23);
357 /// assert_eq!(x.div_assign_mod(Integer::from(-10)), -3);
358 /// assert_eq!(x, 2);
359 /// ```
360 fn div_assign_mod(&mut self, other: Integer) -> Integer {
361 let r = if self.sign == other.sign {
362 self.sign = true;
363 self.abs.div_assign_mod(other.abs)
364 } else {
365 let r = self.abs.ceiling_div_assign_neg_mod(other.abs);
366 if self.abs != 0 {
367 self.sign = false;
368 }
369 r
370 };
371 Integer::from_sign_and_abs(other.sign, r)
372 }
373}
374
375impl DivAssignMod<&Integer> for Integer {
376 type ModOutput = Integer;
377
378 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
379 /// right-hand side by reference and returning the remainder. The quotient is rounded towards
380 /// negative infinity, and the remainder has the same sign as the second [`Integer`].
381 ///
382 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
383 ///
384 /// $$
385 /// f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor,
386 /// $$
387 /// $$
388 /// x \gets \left \lfloor \frac{x}{y} \right \rfloor.
389 /// $$
390 ///
391 /// # Worst-case complexity
392 /// $T(n) = O(n \log n \log \log n)$
393 ///
394 /// $M(n) = O(n \log n)$
395 ///
396 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
397 ///
398 /// # Panics
399 /// Panics if `other` is zero.
400 ///
401 /// # Examples
402 /// ```
403 /// use malachite_base::num::arithmetic::traits::DivAssignMod;
404 /// use malachite_nz::integer::Integer;
405 ///
406 /// // 2 * 10 + 3 = 23
407 /// let mut x = Integer::from(23);
408 /// assert_eq!(x.div_assign_mod(&Integer::from(10)), 3);
409 /// assert_eq!(x, 2);
410 ///
411 /// // -3 * -10 + -7 = 23
412 /// let mut x = Integer::from(23);
413 /// assert_eq!(x.div_assign_mod(&Integer::from(-10)), -7);
414 /// assert_eq!(x, -3);
415 ///
416 /// // -3 * 10 + 7 = -23
417 /// let mut x = Integer::from(-23);
418 /// assert_eq!(x.div_assign_mod(&Integer::from(10)), 7);
419 /// assert_eq!(x, -3);
420 ///
421 /// // 2 * -10 + -3 = -23
422 /// let mut x = Integer::from(-23);
423 /// assert_eq!(x.div_assign_mod(&Integer::from(-10)), -3);
424 /// assert_eq!(x, 2);
425 /// ```
426 fn div_assign_mod(&mut self, other: &Integer) -> Integer {
427 let r = if self.sign == other.sign {
428 self.sign = true;
429 self.abs.div_assign_mod(&other.abs)
430 } else {
431 let r = self.abs.ceiling_div_assign_neg_mod(&other.abs);
432 if self.abs != 0 {
433 self.sign = false;
434 }
435 r
436 };
437 Integer::from_sign_and_abs(other.sign, r)
438 }
439}
440
441impl DivRem<Integer> for Integer {
442 type DivOutput = Integer;
443 type RemOutput = Integer;
444
445 /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning the
446 /// quotient and remainder. The quotient is rounded towards zero and the remainder has the same
447 /// sign as the first [`Integer`].
448 ///
449 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
450 ///
451 /// $$
452 /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
453 /// \right \rfloor, \space
454 /// x - y \operatorname{sgn}(xy)
455 /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
456 /// $$
457 ///
458 /// # Worst-case complexity
459 /// $T(n) = O(n \log n \log \log n)$
460 ///
461 /// $M(n) = O(n \log n)$
462 ///
463 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
464 ///
465 /// # Panics
466 /// Panics if `other` is zero.
467 ///
468 /// # Examples
469 /// ```
470 /// use malachite_base::num::arithmetic::traits::DivRem;
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 /// .div_rem(Integer::from(10))
478 /// .to_debug_string(),
479 /// "(2, 3)"
480 /// );
481 ///
482 /// // -2 * -10 + 3 = 23
483 /// assert_eq!(
484 /// Integer::from(23)
485 /// .div_rem(Integer::from(-10))
486 /// .to_debug_string(),
487 /// "(-2, 3)"
488 /// );
489 ///
490 /// // -2 * 10 + -3 = -23
491 /// assert_eq!(
492 /// Integer::from(-23)
493 /// .div_rem(Integer::from(10))
494 /// .to_debug_string(),
495 /// "(-2, -3)"
496 /// );
497 ///
498 /// // 2 * -10 + -3 = -23
499 /// assert_eq!(
500 /// Integer::from(-23)
501 /// .div_rem(Integer::from(-10))
502 /// .to_debug_string(),
503 /// "(2, -3)"
504 /// );
505 /// ```
506 #[inline]
507 fn div_rem(mut self, other: Integer) -> (Integer, Integer) {
508 let r = self.div_assign_rem(other);
509 (self, r)
510 }
511}
512
513impl DivRem<&Integer> for Integer {
514 type DivOutput = Integer;
515 type RemOutput = Integer;
516
517 /// Divides an [`Integer`] by another [`Integer`], taking the first by value and the second by
518 /// reference and returning the quotient and remainder. The quotient is rounded towards zero and
519 /// the remainder has the same sign as the first [`Integer`].
520 ///
521 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
522 ///
523 /// $$
524 /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
525 /// \right \rfloor, \space
526 /// x - y \operatorname{sgn}(xy)
527 /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
528 /// $$
529 ///
530 /// # Worst-case complexity
531 /// $T(n) = O(n \log n \log \log n)$
532 ///
533 /// $M(n) = O(n \log n)$
534 ///
535 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
536 ///
537 /// # Panics
538 /// Panics if `other` is zero.
539 ///
540 /// # Examples
541 /// ```
542 /// use malachite_base::num::arithmetic::traits::DivRem;
543 /// use malachite_base::strings::ToDebugString;
544 /// use malachite_nz::integer::Integer;
545 ///
546 /// // 2 * 10 + 3 = 23
547 /// assert_eq!(
548 /// Integer::from(23)
549 /// .div_rem(&Integer::from(10))
550 /// .to_debug_string(),
551 /// "(2, 3)"
552 /// );
553 ///
554 /// // -2 * -10 + 3 = 23
555 /// assert_eq!(
556 /// Integer::from(23)
557 /// .div_rem(&Integer::from(-10))
558 /// .to_debug_string(),
559 /// "(-2, 3)"
560 /// );
561 ///
562 /// // -2 * 10 + -3 = -23
563 /// assert_eq!(
564 /// Integer::from(-23)
565 /// .div_rem(&Integer::from(10))
566 /// .to_debug_string(),
567 /// "(-2, -3)"
568 /// );
569 ///
570 /// // 2 * -10 + -3 = -23
571 /// assert_eq!(
572 /// Integer::from(-23)
573 /// .div_rem(&Integer::from(-10))
574 /// .to_debug_string(),
575 /// "(2, -3)"
576 /// );
577 /// ```
578 #[inline]
579 fn div_rem(mut self, other: &Integer) -> (Integer, Integer) {
580 let r = self.div_assign_rem(other);
581 (self, r)
582 }
583}
584
585impl DivRem<Integer> for &Integer {
586 type DivOutput = Integer;
587 type RemOutput = Integer;
588
589 /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
590 /// by value and returning the quotient and remainder. The quotient is rounded towards zero and
591 /// the remainder has the same sign as the first [`Integer`].
592 ///
593 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
594 ///
595 /// $$
596 /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
597 /// \right \rfloor, \space
598 /// x - y \operatorname{sgn}(xy)
599 /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
600 /// $$
601 ///
602 /// # Worst-case complexity
603 /// $T(n) = O(n \log n \log \log n)$
604 ///
605 /// $M(n) = O(n \log n)$
606 ///
607 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
608 ///
609 /// # Panics
610 /// Panics if `other` is zero.
611 ///
612 /// # Examples
613 /// ```
614 /// use malachite_base::num::arithmetic::traits::DivRem;
615 /// use malachite_base::strings::ToDebugString;
616 /// use malachite_nz::integer::Integer;
617 ///
618 /// // 2 * 10 + 3 = 23
619 /// assert_eq!(
620 /// (&Integer::from(23))
621 /// .div_rem(Integer::from(10))
622 /// .to_debug_string(),
623 /// "(2, 3)"
624 /// );
625 ///
626 /// // -2 * -10 + 3 = 23
627 /// assert_eq!(
628 /// (&Integer::from(23))
629 /// .div_rem(Integer::from(-10))
630 /// .to_debug_string(),
631 /// "(-2, 3)"
632 /// );
633 ///
634 /// // -2 * 10 + -3 = -23
635 /// assert_eq!(
636 /// (&Integer::from(-23))
637 /// .div_rem(Integer::from(10))
638 /// .to_debug_string(),
639 /// "(-2, -3)"
640 /// );
641 ///
642 /// // 2 * -10 + -3 = -23
643 /// assert_eq!(
644 /// (&Integer::from(-23))
645 /// .div_rem(Integer::from(-10))
646 /// .to_debug_string(),
647 /// "(2, -3)"
648 /// );
649 /// ```
650 #[inline]
651 fn div_rem(self, other: Integer) -> (Integer, Integer) {
652 let (q, r) = (&self.abs).div_mod(other.abs);
653 (
654 Integer::from_sign_and_abs(self.sign == other.sign, q),
655 Integer::from_sign_and_abs(self.sign, r),
656 )
657 }
658}
659
660impl DivRem<&Integer> for &Integer {
661 type DivOutput = Integer;
662 type RemOutput = Integer;
663
664 /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning the
665 /// quotient and remainder. The quotient is rounded towards zero and the remainder has the same
666 /// sign as the first [`Integer`].
667 ///
668 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
669 ///
670 /// $$
671 /// f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
672 /// \right \rfloor, \space
673 /// x - y \operatorname{sgn}(xy)
674 /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ).
675 /// $$
676 ///
677 /// # Worst-case complexity
678 /// $T(n) = O(n \log n \log \log n)$
679 ///
680 /// $M(n) = O(n \log n)$
681 ///
682 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
683 ///
684 /// # Panics
685 /// Panics if `other` is zero.
686 ///
687 /// # Examples
688 /// ```
689 /// use malachite_base::num::arithmetic::traits::DivRem;
690 /// use malachite_base::strings::ToDebugString;
691 /// use malachite_nz::integer::Integer;
692 ///
693 /// // 2 * 10 + 3 = 23
694 /// assert_eq!(
695 /// (&Integer::from(23))
696 /// .div_rem(&Integer::from(10))
697 /// .to_debug_string(),
698 /// "(2, 3)"
699 /// );
700 ///
701 /// // -2 * -10 + 3 = 23
702 /// assert_eq!(
703 /// (&Integer::from(23))
704 /// .div_rem(&Integer::from(-10))
705 /// .to_debug_string(),
706 /// "(-2, 3)"
707 /// );
708 ///
709 /// // -2 * 10 + -3 = -23
710 /// assert_eq!(
711 /// (&Integer::from(-23))
712 /// .div_rem(&Integer::from(10))
713 /// .to_debug_string(),
714 /// "(-2, -3)"
715 /// );
716 ///
717 /// // 2 * -10 + -3 = -23
718 /// assert_eq!(
719 /// (&Integer::from(-23))
720 /// .div_rem(&Integer::from(-10))
721 /// .to_debug_string(),
722 /// "(2, -3)"
723 /// );
724 /// ```
725 #[inline]
726 fn div_rem(self, other: &Integer) -> (Integer, Integer) {
727 let (q, r) = (&self.abs).div_mod(&other.abs);
728 (
729 Integer::from_sign_and_abs(self.sign == other.sign, q),
730 Integer::from_sign_and_abs(self.sign, r),
731 )
732 }
733}
734
735impl DivAssignRem<Integer> for Integer {
736 type RemOutput = Integer;
737
738 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
739 /// right-hand side by value and returning the remainder. The quotient is rounded towards zero
740 /// and the remainder has the same sign as the first [`Integer`].
741 ///
742 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
743 ///
744 /// $$
745 /// f(x, y) = x - y \operatorname{sgn}(xy)
746 /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor,
747 /// $$
748 /// $$
749 /// x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
750 /// \right \rfloor.
751 /// $$
752 ///
753 /// # Worst-case complexity
754 /// $T(n) = O(n \log n \log \log n)$
755 ///
756 /// $M(n) = O(n \log n)$
757 ///
758 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
759 ///
760 /// # Panics
761 /// Panics if `other` is zero.
762 ///
763 /// # Examples
764 /// ```
765 /// use malachite_base::num::arithmetic::traits::DivAssignRem;
766 /// use malachite_nz::integer::Integer;
767 ///
768 /// // 2 * 10 + 3 = 23
769 /// let mut x = Integer::from(23);
770 /// assert_eq!(x.div_assign_rem(Integer::from(10)), 3);
771 /// assert_eq!(x, 2);
772 ///
773 /// // -2 * -10 + 3 = 23
774 /// let mut x = Integer::from(23);
775 /// assert_eq!(x.div_assign_rem(Integer::from(-10)), 3);
776 /// assert_eq!(x, -2);
777 ///
778 /// // -2 * 10 + -3 = -23
779 /// let mut x = Integer::from(-23);
780 /// assert_eq!(x.div_assign_rem(Integer::from(10)), -3);
781 /// assert_eq!(x, -2);
782 ///
783 /// // 2 * -10 + -3 = -23
784 /// let mut x = Integer::from(-23);
785 /// assert_eq!(x.div_assign_rem(Integer::from(-10)), -3);
786 /// assert_eq!(x, 2);
787 /// ```
788 #[inline]
789 fn div_assign_rem(&mut self, other: Integer) -> Integer {
790 let r = Integer::from_sign_and_abs(self.sign, self.abs.div_assign_mod(other.abs));
791 self.sign = self.sign == other.sign || self.abs == 0;
792 r
793 }
794}
795
796impl DivAssignRem<&Integer> for Integer {
797 type RemOutput = Integer;
798
799 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
800 /// right-hand side by reference and returning the remainder. The quotient is rounded towards
801 /// zero and the remainder has the same sign as the first [`Integer`].
802 ///
803 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
804 ///
805 /// $$
806 /// f(x, y) = x - y \operatorname{sgn}(xy)
807 /// \left \lfloor \left | \frac{x}{y} \right | \right \rfloor,
808 /// $$
809 /// $$
810 /// x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right |
811 /// \right \rfloor.
812 /// $$
813 ///
814 /// # Worst-case complexity
815 /// $T(n) = O(n \log n \log \log n)$
816 ///
817 /// $M(n) = O(n \log n)$
818 ///
819 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
820 ///
821 /// # Panics
822 /// Panics if `other` is zero.
823 ///
824 /// # Examples
825 /// ```
826 /// use malachite_base::num::arithmetic::traits::DivAssignRem;
827 /// use malachite_nz::integer::Integer;
828 ///
829 /// // 2 * 10 + 3 = 23
830 /// let mut x = Integer::from(23);
831 /// assert_eq!(x.div_assign_rem(&Integer::from(10)), 3);
832 /// assert_eq!(x, 2);
833 ///
834 /// // -2 * -10 + 3 = 23
835 /// let mut x = Integer::from(23);
836 /// assert_eq!(x.div_assign_rem(&Integer::from(-10)), 3);
837 /// assert_eq!(x, -2);
838 ///
839 /// // -2 * 10 + -3 = -23
840 /// let mut x = Integer::from(-23);
841 /// assert_eq!(x.div_assign_rem(&Integer::from(10)), -3);
842 /// assert_eq!(x, -2);
843 ///
844 /// // 2 * -10 + -3 = -23
845 /// let mut x = Integer::from(-23);
846 /// assert_eq!(x.div_assign_rem(&Integer::from(-10)), -3);
847 /// assert_eq!(x, 2);
848 /// ```
849 #[inline]
850 fn div_assign_rem(&mut self, other: &Integer) -> Integer {
851 let r = Integer::from_sign_and_abs(self.sign, self.abs.div_assign_mod(&other.abs));
852 self.sign = self.sign == other.sign || self.abs == 0;
853 r
854 }
855}
856
857impl CeilingDivMod<Integer> for Integer {
858 type DivOutput = Integer;
859 type ModOutput = Integer;
860
861 /// Divides an [`Integer`] by another [`Integer`], taking both by value and returning the
862 /// quotient and remainder. The quotient is rounded towards positive infinity and the remainder
863 /// has the opposite sign as the second [`Integer`].
864 ///
865 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
866 ///
867 /// $$
868 /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
869 /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
870 /// $$
871 ///
872 /// # Worst-case complexity
873 /// $T(n) = O(n \log n \log \log n)$
874 ///
875 /// $M(n) = O(n \log n)$
876 ///
877 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
878 ///
879 /// # Panics
880 /// Panics if `other` is zero.
881 ///
882 /// # Examples
883 /// ```
884 /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
885 /// use malachite_base::strings::ToDebugString;
886 /// use malachite_nz::integer::Integer;
887 ///
888 /// // 3 * 10 + -7 = 23
889 /// assert_eq!(
890 /// Integer::from(23)
891 /// .ceiling_div_mod(Integer::from(10))
892 /// .to_debug_string(),
893 /// "(3, -7)"
894 /// );
895 ///
896 /// // -2 * -10 + 3 = 23
897 /// assert_eq!(
898 /// Integer::from(23)
899 /// .ceiling_div_mod(Integer::from(-10))
900 /// .to_debug_string(),
901 /// "(-2, 3)"
902 /// );
903 ///
904 /// // -2 * 10 + -3 = -23
905 /// assert_eq!(
906 /// Integer::from(-23)
907 /// .ceiling_div_mod(Integer::from(10))
908 /// .to_debug_string(),
909 /// "(-2, -3)"
910 /// );
911 ///
912 /// // 3 * -10 + 7 = -23
913 /// assert_eq!(
914 /// Integer::from(-23)
915 /// .ceiling_div_mod(Integer::from(-10))
916 /// .to_debug_string(),
917 /// "(3, 7)"
918 /// );
919 /// ```
920 #[inline]
921 fn ceiling_div_mod(mut self, other: Integer) -> (Integer, Integer) {
922 let r = self.ceiling_div_assign_mod(other);
923 (self, r)
924 }
925}
926
927impl CeilingDivMod<&Integer> for Integer {
928 type DivOutput = Integer;
929 type ModOutput = Integer;
930
931 /// Divides an [`Integer`] by another [`Integer`], taking both the first by value and the second
932 /// by reference and returning the quotient and remainder. The quotient is rounded towards
933 /// positive infinity and the remainder has the opposite sign as the second [`Integer`].
934 ///
935 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
936 ///
937 /// $$
938 /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
939 /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
940 /// $$
941 ///
942 /// # Worst-case complexity
943 /// $T(n) = O(n \log n \log \log n)$
944 ///
945 /// $M(n) = O(n \log n)$
946 ///
947 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
948 ///
949 /// # Panics
950 /// Panics if `other` is zero.
951 ///
952 /// # Examples
953 /// ```
954 /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
955 /// use malachite_base::strings::ToDebugString;
956 /// use malachite_nz::integer::Integer;
957 ///
958 /// // 3 * 10 + -7 = 23
959 /// assert_eq!(
960 /// Integer::from(23)
961 /// .ceiling_div_mod(&Integer::from(10))
962 /// .to_debug_string(),
963 /// "(3, -7)"
964 /// );
965 ///
966 /// // -2 * -10 + 3 = 23
967 /// assert_eq!(
968 /// Integer::from(23)
969 /// .ceiling_div_mod(&Integer::from(-10))
970 /// .to_debug_string(),
971 /// "(-2, 3)"
972 /// );
973 ///
974 /// // -2 * 10 + -3 = -23
975 /// assert_eq!(
976 /// Integer::from(-23)
977 /// .ceiling_div_mod(&Integer::from(10))
978 /// .to_debug_string(),
979 /// "(-2, -3)"
980 /// );
981 ///
982 /// // 3 * -10 + 7 = -23
983 /// assert_eq!(
984 /// Integer::from(-23)
985 /// .ceiling_div_mod(&Integer::from(-10))
986 /// .to_debug_string(),
987 /// "(3, 7)"
988 /// );
989 /// ```
990 #[inline]
991 fn ceiling_div_mod(mut self, other: &Integer) -> (Integer, Integer) {
992 let r = self.ceiling_div_assign_mod(other);
993 (self, r)
994 }
995}
996
997impl CeilingDivMod<Integer> for &Integer {
998 type DivOutput = Integer;
999 type ModOutput = Integer;
1000
1001 /// Divides an [`Integer`] by another [`Integer`], taking the first by reference and the second
1002 /// by value and returning the quotient and remainder. The quotient is rounded towards positive
1003 /// infinity and the remainder has the opposite sign as the second [`Integer`].
1004 ///
1005 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1006 ///
1007 /// $$
1008 /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
1009 /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
1010 /// $$
1011 ///
1012 /// # Worst-case complexity
1013 /// $T(n) = O(n \log n \log \log n)$
1014 ///
1015 /// $M(n) = O(n \log n)$
1016 ///
1017 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1018 ///
1019 /// # Panics
1020 /// Panics if `other` is zero.
1021 ///
1022 /// # Examples
1023 /// ```
1024 /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
1025 /// use malachite_base::strings::ToDebugString;
1026 /// use malachite_nz::integer::Integer;
1027 ///
1028 /// // 3 * 10 + -7 = 23
1029 /// assert_eq!(
1030 /// (&Integer::from(23))
1031 /// .ceiling_div_mod(Integer::from(10))
1032 /// .to_debug_string(),
1033 /// "(3, -7)"
1034 /// );
1035 ///
1036 /// // -2 * -10 + 3 = 23
1037 /// assert_eq!(
1038 /// (&Integer::from(23))
1039 /// .ceiling_div_mod(Integer::from(-10))
1040 /// .to_debug_string(),
1041 /// "(-2, 3)"
1042 /// );
1043 ///
1044 /// // -2 * 10 + -3 = -23
1045 /// assert_eq!(
1046 /// (&Integer::from(-23))
1047 /// .ceiling_div_mod(Integer::from(10))
1048 /// .to_debug_string(),
1049 /// "(-2, -3)"
1050 /// );
1051 ///
1052 /// // 3 * -10 + 7 = -23
1053 /// assert_eq!(
1054 /// (&Integer::from(-23))
1055 /// .ceiling_div_mod(Integer::from(-10))
1056 /// .to_debug_string(),
1057 /// "(3, 7)"
1058 /// );
1059 /// ```
1060 fn ceiling_div_mod(self, other: Integer) -> (Integer, Integer) {
1061 let q_sign = self.sign == other.sign;
1062 let (q, r) = if q_sign {
1063 (&self.abs).ceiling_div_neg_mod(other.abs)
1064 } else {
1065 (&self.abs).div_mod(other.abs)
1066 };
1067 (
1068 Integer::from_sign_and_abs(q_sign, q),
1069 Integer::from_sign_and_abs(!other.sign, r),
1070 )
1071 }
1072}
1073
1074impl CeilingDivMod<&Integer> for &Integer {
1075 type DivOutput = Integer;
1076 type ModOutput = Integer;
1077
1078 /// Divides an [`Integer`] by another [`Integer`], taking both by reference and returning the
1079 /// quotient and remainder. The quotient is rounded towards positive infinity and the remainder
1080 /// has the opposite sign as the second [`Integer`].
1081 ///
1082 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1083 ///
1084 /// $$
1085 /// f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space
1086 /// x - y\left \lceil \frac{x}{y} \right \rceil \right ).
1087 /// $$
1088 ///
1089 /// # Worst-case complexity
1090 /// $T(n) = O(n \log n \log \log n)$
1091 ///
1092 /// $M(n) = O(n \log n)$
1093 ///
1094 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1095 ///
1096 /// # Panics
1097 /// Panics if `other` is zero.
1098 ///
1099 /// # Examples
1100 /// ```
1101 /// use malachite_base::num::arithmetic::traits::CeilingDivMod;
1102 /// use malachite_base::strings::ToDebugString;
1103 /// use malachite_nz::integer::Integer;
1104 ///
1105 /// // 3 * 10 + -7 = 23
1106 /// assert_eq!(
1107 /// (&Integer::from(23))
1108 /// .ceiling_div_mod(&Integer::from(10))
1109 /// .to_debug_string(),
1110 /// "(3, -7)"
1111 /// );
1112 ///
1113 /// // -2 * -10 + 3 = 23
1114 /// assert_eq!(
1115 /// (&Integer::from(23))
1116 /// .ceiling_div_mod(&Integer::from(-10))
1117 /// .to_debug_string(),
1118 /// "(-2, 3)"
1119 /// );
1120 ///
1121 /// // -2 * 10 + -3 = -23
1122 /// assert_eq!(
1123 /// (&Integer::from(-23))
1124 /// .ceiling_div_mod(&Integer::from(10))
1125 /// .to_debug_string(),
1126 /// "(-2, -3)"
1127 /// );
1128 ///
1129 /// // 3 * -10 + 7 = -23
1130 /// assert_eq!(
1131 /// (&Integer::from(-23))
1132 /// .ceiling_div_mod(&Integer::from(-10))
1133 /// .to_debug_string(),
1134 /// "(3, 7)"
1135 /// );
1136 /// ```
1137 fn ceiling_div_mod(self, other: &Integer) -> (Integer, Integer) {
1138 let q_sign = self.sign == other.sign;
1139 let (q, r) = if q_sign {
1140 (&self.abs).ceiling_div_neg_mod(&other.abs)
1141 } else {
1142 (&self.abs).div_mod(&other.abs)
1143 };
1144 (
1145 Integer::from_sign_and_abs(q_sign, q),
1146 Integer::from_sign_and_abs(!other.sign, r),
1147 )
1148 }
1149}
1150
1151impl CeilingDivAssignMod<Integer> for Integer {
1152 type ModOutput = Integer;
1153
1154 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
1155 /// right-hand side by value and returning the remainder. The quotient is rounded towards
1156 /// positive infinity and the remainder has the opposite sign as the second [`Integer`].
1157 ///
1158 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1159 ///
1160 /// $$
1161 /// f(x, y) = x - y\left \lceil\frac{x}{y} \right \rceil,
1162 /// $$
1163 /// $$
1164 /// x \gets \left \lceil \frac{x}{y} \right \rceil.
1165 /// $$
1166 ///
1167 /// # Worst-case complexity
1168 /// $T(n) = O(n \log n \log \log n)$
1169 ///
1170 /// $M(n) = O(n \log n)$
1171 ///
1172 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1173 ///
1174 /// # Panics
1175 /// Panics if `other` is zero.
1176 ///
1177 /// # Examples
1178 /// ```
1179 /// use malachite_base::num::arithmetic::traits::CeilingDivAssignMod;
1180 /// use malachite_nz::integer::Integer;
1181 ///
1182 /// // 3 * 10 + -7 = 23
1183 /// let mut x = Integer::from(23);
1184 /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(10)), -7);
1185 /// assert_eq!(x, 3);
1186 ///
1187 /// // -2 * -10 + 3 = 23
1188 /// let mut x = Integer::from(23);
1189 /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(-10)), 3);
1190 /// assert_eq!(x, -2);
1191 ///
1192 /// // -2 * 10 + -3 = -23
1193 /// let mut x = Integer::from(-23);
1194 /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(10)), -3);
1195 /// assert_eq!(x, -2);
1196 ///
1197 /// // 3 * -10 + 7 = -23
1198 /// let mut x = Integer::from(-23);
1199 /// assert_eq!(x.ceiling_div_assign_mod(Integer::from(-10)), 7);
1200 /// assert_eq!(x, 3);
1201 /// ```
1202 fn ceiling_div_assign_mod(&mut self, other: Integer) -> Integer {
1203 let r = if self.sign == other.sign {
1204 self.sign = true;
1205 self.abs.ceiling_div_assign_neg_mod(other.abs)
1206 } else {
1207 let r = self.abs.div_assign_mod(other.abs);
1208 self.sign = self.abs == 0;
1209 r
1210 };
1211 Integer::from_sign_and_abs(!other.sign, r)
1212 }
1213}
1214
1215impl CeilingDivAssignMod<&Integer> for Integer {
1216 type ModOutput = Integer;
1217
1218 /// Divides an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
1219 /// right-hand side by reference and returning the remainder. The quotient is rounded towards
1220 /// positive infinity and the remainder has the opposite sign as the second [`Integer`].
1221 ///
1222 /// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
1223 ///
1224 /// $$
1225 /// f(x, y) = x - y\left \lceil\frac{x}{y} \right \rceil,
1226 /// $$
1227 /// $$
1228 /// x \gets \left \lceil \frac{x}{y} \right \rceil.
1229 /// $$
1230 ///
1231 /// # Worst-case complexity
1232 /// $T(n) = O(n \log n \log \log n)$
1233 ///
1234 /// $M(n) = O(n \log n)$
1235 ///
1236 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1237 ///
1238 /// # Panics
1239 /// Panics if `other` is zero.
1240 ///
1241 /// # Examples
1242 /// ```
1243 /// use malachite_base::num::arithmetic::traits::CeilingDivAssignMod;
1244 /// use malachite_nz::integer::Integer;
1245 ///
1246 /// // 3 * 10 + -7 = 23
1247 /// let mut x = Integer::from(23);
1248 /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(10)), -7);
1249 /// assert_eq!(x, 3);
1250 ///
1251 /// // -2 * -10 + 3 = 23
1252 /// let mut x = Integer::from(23);
1253 /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(-10)), 3);
1254 /// assert_eq!(x, -2);
1255 ///
1256 /// // -2 * 10 + -3 = -23
1257 /// let mut x = Integer::from(-23);
1258 /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(10)), -3);
1259 /// assert_eq!(x, -2);
1260 ///
1261 /// // 3 * -10 + 7 = -23
1262 /// let mut x = Integer::from(-23);
1263 /// assert_eq!(x.ceiling_div_assign_mod(&Integer::from(-10)), 7);
1264 /// assert_eq!(x, 3);
1265 /// ```
1266 fn ceiling_div_assign_mod(&mut self, other: &Integer) -> Integer {
1267 let r = if self.sign == other.sign {
1268 self.sign = true;
1269 self.abs.ceiling_div_assign_neg_mod(&other.abs)
1270 } else {
1271 let r = self.abs.div_assign_mod(&other.abs);
1272 self.sign = self.abs == 0;
1273 r
1274 };
1275 Integer::from_sign_and_abs(!other.sign, r)
1276 }
1277}