malachite_base/num/arithmetic/
eq_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::num::arithmetic::traits::{EqMod, Mod};
10use crate::num::basic::traits::Zero;
11
12fn eq_mod<U: Eq, S: Copy + Eq + Mod<S, Output = U> + Zero>(x: S, other: S, m: S) -> bool {
13    x == other || m != S::ZERO && x.mod_op(m) == other.mod_op(m)
14}
15
16macro_rules! impl_eq_mod {
17    ($t:ident) => {
18        impl EqMod<$t> for $t {
19            /// Returns whether a number is equivalent to another number modulo a third; that is,
20            /// whether the difference between the first two is a multiple of the third.
21            ///
22            /// Two numbers are equal to each other modulo 0 iff they are equal.
23            ///
24            /// $f(x, y, m) = (x \equiv y \mod m)$.
25            ///
26            /// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
27            ///
28            /// # Worst-case complexity
29            /// Constant time and additional memory.
30            ///
31            /// # Examples
32            /// See [here](super::eq_mod#eq_mod).
33            #[inline]
34            fn eq_mod(self, other: $t, m: $t) -> bool {
35                eq_mod(self, other, m)
36            }
37        }
38    };
39}
40apply_to_primitive_ints!(impl_eq_mod);