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