1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Copyright 2025 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::Arbi;
impl Arbi {
/// Divide `self` by `rhs`, returning both a quotient and remainder:
/// `(q, r)`.
///
/// The quotient is rounded towards negative infinity and the remainder will
/// have the same sign as `rhs`.
///
/// # Panic
/// Panics if `rhs` is zero.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// let (a, b) = (Arbi::from(8), Arbi::from(3));
/// let (ma, mb) = (a.negate_ref(), b.negate_ref());
///
/// let (q, r) = a.divrem_floor_ref(&b);
/// assert_eq!(q, 2);
/// assert_eq!(r, 2);
///
/// let (q, r) = a.divrem_floor_ref(&mb);
/// assert_eq!(q, -3);
/// assert_eq!(r, -1);
///
/// let (q, r) = ma.divrem_floor_ref(&b);
/// assert_eq!(q, -3);
/// assert_eq!(r, 1);
///
/// let (q, r) = ma.divrem_floor_ref(&mb);
/// assert_eq!(q, 2);
/// assert_eq!(r, -2);
///
/// let (q, r) = Arbi::zero().divrem_floor_ref(&a);
/// assert_eq!(q, 0);
/// assert_eq!(r, 0);
///
/// let (q, r) = Arbi::zero().divrem_floor_ref(&ma);
/// assert_eq!(q, 0);
/// assert_eq!(r, 0);
/// ```
///
/// Panics if `rhs` is zero:
/// ```should_panic
/// use arbi::Arbi;
///
/// let x = Arbi::from(123456789);
/// let (q, r) = x.divrem_floor_ref(&Arbi::zero());
/// ```
pub fn divrem_floor_ref(&self, rhs: &Self) -> (Self, Self) {
let (mut q, mut r) = (Arbi::zero(), Arbi::zero());
Self::fdivide(&mut q, &mut r, self, rhs);
(q, r)
}
}