Skip to main content

embedded_maths/embedded-maths-functions/
fract.rs

1// SPDX-License-Identifier: GPL-2.0-or-later
2//! `f32` fractional-part operations.
3
4/// Returns the fractional part of `x`.
5#[inline(always)]
6pub fn fract(x: f32) -> f32 {
7    if x.is_nan() || x.is_infinite() {
8        return f32::NAN;
9    }
10    x - super::trunc::trunc(x)
11}
12
13#[cfg(test)]
14mod tests {
15    use super::fract;
16
17    #[test]
18    fn fractional_parts() {
19        assert_eq!(fract(3.25), 0.25);
20        assert_eq!(fract(-3.25), -0.25);
21        assert_eq!(fract(4.0), 0.0);
22    }
23}