use crate::constants::Q32_ONE;
pub fn to_q32(value: f64) -> u64 {
(value * Q32_ONE as f64) as u64
}
pub fn from_q32(q32: u64) -> f64 {
let integer_part = q32 >> 32;
let fractional_part = q32 & 0xFFFF_FFFF;
integer_part as f64 + (fractional_part as f64 / Q32_ONE as f64)
}
pub fn pct_to_q32(pct: f64) -> u64 {
to_q32(pct)
}
pub fn spread_bps_to_q32(bps: u16) -> u64 {
to_q32(1.0 - (bps as f64) / 10_000.0)
}
pub fn spread_q32_to_bps(q32: u64) -> f64 {
(1.0 - from_q32(q32)) * 10_000.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_q32_roundtrip() {
let original = 1.5;
let q32 = to_q32(original);
let back = from_q32(q32);
assert!((back - original).abs() < 1e-9);
}
#[test]
fn test_q32_one() {
assert_eq!(to_q32(1.0), Q32_ONE);
assert!((from_q32(Q32_ONE) - 1.0).abs() < 1e-9);
}
#[test]
fn test_spread_bps_roundtrip() {
let bps: u16 = 5;
let q32 = spread_bps_to_q32(bps);
let back = spread_q32_to_bps(q32);
assert!((back - 5.0).abs() < 0.01);
}
}