1use core::ops::{Div, Rem};
2
3pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
4 fn div_euclid(&self, v: &Self) -> Self;
24
25 fn rem_euclid(&self, v: &Self) -> Self;
49
50 fn div_rem_euclid(&self, v: &Self) -> (Self, Self) {
68 (self.div_euclid(v), self.rem_euclid(v))
69 }
70}
71
72macro_rules! euclid_forward_impl {
73 ($($t:ty)*) => {$(
74 impl Euclid for $t {
75 #[inline]
76 fn div_euclid(&self, v: &$t) -> Self {
77 <$t>::div_euclid(*self, *v)
78 }
79
80 #[inline]
81 fn rem_euclid(&self, v: &$t) -> Self {
82 <$t>::rem_euclid(*self, *v)
83 }
84 }
85 )*}
86}
87
88impl Euclid for i128 {
#[inline]
fn div_euclid(&self, v: &i128) -> Self { <i128>::div_euclid(*self, *v) }
#[inline]
fn rem_euclid(&self, v: &i128) -> Self { <i128>::rem_euclid(*self, *v) }
}euclid_forward_impl!(isize i8 i16 i32 i64 i128);
89impl Euclid for u128 {
#[inline]
fn div_euclid(&self, v: &u128) -> Self { <u128>::div_euclid(*self, *v) }
#[inline]
fn rem_euclid(&self, v: &u128) -> Self { <u128>::rem_euclid(*self, *v) }
}euclid_forward_impl!(usize u8 u16 u32 u64 u128);
90
91#[cfg(feature = "std")]
92impl Euclid for f64 {
#[inline]
fn div_euclid(&self, v: &f64) -> Self { <f64>::div_euclid(*self, *v) }
#[inline]
fn rem_euclid(&self, v: &f64) -> Self { <f64>::rem_euclid(*self, *v) }
}euclid_forward_impl!(f32 f64);
93
94#[cfg(not(feature = "std"))]
95impl Euclid for f32 {
96 #[inline]
97 fn div_euclid(&self, v: &f32) -> f32 {
98 let q = <f32 as crate::float::FloatCore>::trunc(self / v);
99 if self % v < 0.0 {
100 return if *v > 0.0 { q - 1.0 } else { q + 1.0 };
101 }
102 q
103 }
104
105 #[inline]
106 fn rem_euclid(&self, v: &f32) -> f32 {
107 let r = self % v;
108 if r < 0.0 {
109 r + <f32 as crate::float::FloatCore>::abs(*v)
110 } else {
111 r
112 }
113 }
114}
115
116#[cfg(not(feature = "std"))]
117impl Euclid for f64 {
118 #[inline]
119 fn div_euclid(&self, v: &f64) -> f64 {
120 let q = <f64 as crate::float::FloatCore>::trunc(self / v);
121 if self % v < 0.0 {
122 return if *v > 0.0 { q - 1.0 } else { q + 1.0 };
123 }
124 q
125 }
126
127 #[inline]
128 fn rem_euclid(&self, v: &f64) -> f64 {
129 let r = self % v;
130 if r < 0.0 {
131 r + <f64 as crate::float::FloatCore>::abs(*v)
132 } else {
133 r
134 }
135 }
136}
137
138pub trait CheckedEuclid: Euclid {
139 fn checked_div_euclid(&self, v: &Self) -> Option<Self>;
142
143 fn checked_rem_euclid(&self, v: &Self) -> Option<Self>;
146
147 fn checked_div_rem_euclid(&self, v: &Self) -> Option<(Self, Self)> {
164 Some((self.checked_div_euclid(v)?, self.checked_rem_euclid(v)?))
165 }
166}
167
168macro_rules! checked_euclid_forward_impl {
169 ($($t:ty)*) => {$(
170 impl CheckedEuclid for $t {
171 #[inline]
172 fn checked_div_euclid(&self, v: &$t) -> Option<Self> {
173 <$t>::checked_div_euclid(*self, *v)
174 }
175
176 #[inline]
177 fn checked_rem_euclid(&self, v: &$t) -> Option<Self> {
178 <$t>::checked_rem_euclid(*self, *v)
179 }
180 }
181 )*}
182}
183
184impl CheckedEuclid for i128 {
#[inline]
fn checked_div_euclid(&self, v: &i128) -> Option<Self> {
<i128>::checked_div_euclid(*self, *v)
}
#[inline]
fn checked_rem_euclid(&self, v: &i128) -> Option<Self> {
<i128>::checked_rem_euclid(*self, *v)
}
}checked_euclid_forward_impl!(isize i8 i16 i32 i64 i128);
185impl CheckedEuclid for u128 {
#[inline]
fn checked_div_euclid(&self, v: &u128) -> Option<Self> {
<u128>::checked_div_euclid(*self, *v)
}
#[inline]
fn checked_rem_euclid(&self, v: &u128) -> Option<Self> {
<u128>::checked_rem_euclid(*self, *v)
}
}checked_euclid_forward_impl!(usize u8 u16 u32 u64 u128);
186
187#[cfg(test)]
188mod tests {
189 use super::*;
190
191 #[test]
192 fn euclid_unsigned() {
193 macro_rules! test_euclid {
194 ($($t:ident)+) => {
195 $(
196 {
197 let x: $t = 10;
198 let y: $t = 3;
199 let div = Euclid::div_euclid(&x, &y);
200 let rem = Euclid::rem_euclid(&x, &y);
201 assert_eq!(div, 3);
202 assert_eq!(rem, 1);
203 assert_eq!((div, rem), Euclid::div_rem_euclid(&x, &y));
204 }
205 )+
206 };
207 }
208
209 test_euclid!(usize u8 u16 u32 u64);
210 }
211
212 #[test]
213 fn euclid_signed() {
214 macro_rules! test_euclid {
215 ($($t:ident)+) => {
216 $(
217 {
218 let x: $t = 10;
219 let y: $t = -3;
220 assert_eq!(Euclid::div_euclid(&x, &y), -3);
221 assert_eq!(Euclid::div_euclid(&-x, &y), 4);
222 assert_eq!(Euclid::rem_euclid(&x, &y), 1);
223 assert_eq!(Euclid::rem_euclid(&-x, &y), 2);
224 assert_eq!((Euclid::div_euclid(&x, &y), Euclid::rem_euclid(&x, &y)), Euclid::div_rem_euclid(&x, &y));
225 let x: $t = $t::min_value() + 1;
226 let y: $t = -1;
227 assert_eq!(Euclid::div_euclid(&x, &y), $t::max_value());
228 }
229 )+
230 };
231 }
232
233 test_euclid!(isize i8 i16 i32 i64 i128);
234 }
235
236 #[test]
237 fn euclid_float() {
238 macro_rules! test_euclid {
239 ($($t:ident)+) => {
240 $(
241 {
242 let x: $t = 12.1;
243 let y: $t = 3.2;
244 assert!(Euclid::div_euclid(&x, &y) * y + Euclid::rem_euclid(&x, &y) - x
245 <= 46.4 * <$t as crate::float::FloatCore>::epsilon());
246 assert!(Euclid::div_euclid(&x, &-y) * -y + Euclid::rem_euclid(&x, &-y) - x
247 <= 46.4 * <$t as crate::float::FloatCore>::epsilon());
248 assert!(Euclid::div_euclid(&-x, &y) * y + Euclid::rem_euclid(&-x, &y) + x
249 <= 46.4 * <$t as crate::float::FloatCore>::epsilon());
250 assert!(Euclid::div_euclid(&-x, &-y) * -y + Euclid::rem_euclid(&-x, &-y) + x
251 <= 46.4 * <$t as crate::float::FloatCore>::epsilon());
252 assert_eq!((Euclid::div_euclid(&x, &y), Euclid::rem_euclid(&x, &y)), Euclid::div_rem_euclid(&x, &y));
253 }
254 )+
255 };
256 }
257
258 test_euclid!(f32 f64);
259 }
260
261 #[test]
262 fn euclid_checked() {
263 macro_rules! test_euclid_checked {
264 ($($t:ident)+) => {
265 $(
266 {
267 assert_eq!(CheckedEuclid::checked_div_euclid(&$t::min_value(), &-1), None);
268 assert_eq!(CheckedEuclid::checked_rem_euclid(&$t::min_value(), &-1), None);
269 assert_eq!(CheckedEuclid::checked_div_euclid(&1, &0), None);
270 assert_eq!(CheckedEuclid::checked_rem_euclid(&1, &0), None);
271 }
272 )+
273 };
274 }
275
276 test_euclid_checked!(isize i8 i16 i32 i64 i128);
277 }
278}