| 1 | //! Floating point floor approximation for a single-precision float. |
| 2 | |
| 3 | use super::F32; |
| 4 | |
| 5 | impl F32 { |
| 6 | /// Returns the largest integer less than or equal to a number. |
| 7 | pub fn floor(self) -> Self { |
| 8 | let mut res: f32 = (self.0 as i32) as f32; |
| 9 | |
| 10 | if self.0 < res { |
| 11 | res -= 1.0; |
| 12 | } |
| 13 | |
| 14 | Self(res) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | #[cfg (test)] |
| 19 | mod tests { |
| 20 | use super::F32; |
| 21 | |
| 22 | #[test ] |
| 23 | fn sanity_check() { |
| 24 | assert_eq!(F32(-1.1).floor().0, -2.0); |
| 25 | assert_eq!(F32(-0.1).floor().0, -1.0); |
| 26 | assert_eq!(F32(0.0).floor().0, 0.0); |
| 27 | assert_eq!(F32(1.0).floor().0, 1.0); |
| 28 | assert_eq!(F32(1.1).floor().0, 1.0); |
| 29 | assert_eq!(F32(2.9).floor().0, 2.0); |
| 30 | } |
| 31 | } |
| 32 | |