malachite_base/num/arithmetic/div_exact.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::num::arithmetic::traits::{DivExact, DivExactAssign};
10
11macro_rules! impl_div_exact {
12 ($t:ident) => {
13 impl DivExact<$t> for $t {
14 type Output = $t;
15
16 /// Divides a value by another value. The first value must be exactly divisible by the
17 /// second.
18 ///
19 /// If `self` is not exactly divisible by `other`, this function may panic or return a
20 /// meaningless result.
21 ///
22 /// $$
23 /// f(x, y) = \frac{x}{y}.
24 /// $$
25 ///
26 /// If you are unsure whether the division will be exact, use `self / other` instead. If
27 /// you're unsure and you want to know, use `self.div_mod(other)` and check whether the
28 /// remainder is zero. If you want a function that panics if the division is not exact,
29 /// use `self.div_round(other, Exact)`.
30 ///
31 /// # Worst-case complexity
32 /// Constant time and additional memory.
33 ///
34 /// # Panics
35 /// Panics if `other` is zero or if `self` is `Self::MIN` and other is -1.
36 ///
37 /// # Examples
38 /// See [here](super::div_exact#div_exact).
39 #[inline]
40 fn div_exact(self, other: $t) -> $t {
41 self / other
42 }
43 }
44
45 impl DivExactAssign<$t> for $t {
46 /// Divides a value by another value in place. The value being assigned to must be
47 /// exactly divisible by the value on the right-hand side.
48 ///
49 /// If `self` is not exactly divisible by `other`, this function may panic or return a
50 /// meaningless result.
51 ///
52 /// $$
53 /// x \gets \frac{x}{y}.
54 /// $$
55 ///
56 /// If you are unsure whether the division will be exact, use `self /= other` instead.
57 /// If you're unsure and you want to know, use `self.div_assign_mod(other)` and check
58 /// whether the remainder is zero. If you want a function that panics if the division is
59 /// not exact, use `self.div_round_assign(other, Exact)`.
60 ///
61 /// # Worst-case complexity
62 /// Constant time and additional memory.
63 ///
64 /// # Panics
65 /// Panics if `other` is zero or if `self` is `Self::MIN` and other is -1.
66 ///
67 /// # Examples
68 /// See [here](super::div_exact#div_exact_assign).
69 #[inline]
70 fn div_exact_assign(&mut self, other: $t) {
71 *self /= other;
72 }
73 }
74 };
75}
76apply_to_primitive_ints!(impl_div_exact);