1 | //! Calculates Euclidian division for a single-precision float. |
2 | |
3 | use super::F32; |
4 | |
5 | impl F32 { |
6 | /// Calculates Euclidean division, the matching method for `rem_euclid`. |
7 | pub fn div_euclid(self, rhs: Self) -> Self { |
8 | let q: F32 = (self / rhs).trunc(); |
9 | |
10 | if self % rhs >= Self::ZERO { |
11 | q |
12 | } else if rhs > Self::ZERO { |
13 | q - Self::ONE |
14 | } else { |
15 | q + Self::ONE |
16 | } |
17 | } |
18 | } |
19 | |
20 | #[cfg (test)] |
21 | mod tests { |
22 | use super::F32; |
23 | |
24 | #[test ] |
25 | fn sanity_check() { |
26 | let a = F32(7.0); |
27 | let b = F32(4.0); |
28 | |
29 | assert_eq!(a.div_euclid(b), F32(1.0)); |
30 | assert_eq!((-a).div_euclid(b), F32(-2.0)); |
31 | assert_eq!(a.div_euclid(-b), F32(-1.0)); |
32 | assert_eq!((-a).div_euclid(-b), F32(2.0)); |
33 | } |
34 | } |
35 | |