Skip to main content

deep_time/math/
trunc.rs

1use crate::Real;
2
3/// Const-compatible version of `f64::trunc` (rounds toward zero)
4pub const fn trunc(x: Real) -> Real {
5    let bits = x.to_bits();
6    let sign = bits & (1u64 << 63);
7    let exp = ((bits >> 52) & 0x7ff) as i32 - 1023;
8
9    if exp < 0 {
10        // |x| < 1.0 → result is signed zero
11        return Real::from_bits(sign);
12    }
13
14    if exp >= 52 {
15        return x;
16    }
17
18    // Clear all fractional bits
19    let mask = !0u64 << (52 - exp);
20    let truncated = (bits & mask) | sign; // preserve sign
21    Real::from_bits(truncated)
22}
23// tests in super::round