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