pub fn to_fixed(x: f64, m: i32, n: i32, round: bool) -> Result<Q, String>
Expand description
Convert x
(f64) into fixed point format (Qm.n), if possible.
use fixed2float::{to_Q, Q};
use fixed2float::to_fixed;
assert_eq!(
to_Q(1.5, 1, 3, true),
Ok(
Q {
val: 0b1100,
m: 1,
n: 3,
is_exact: true,
}
)
);
assert_eq!(
to_Q(-2.5, 3, 3, true),
Ok(
Q {
val: 0b1101100,
m: 3,
n: 3,
is_exact: true,
}
)
);
assert_eq!(to_Q(1.5, 1, 3, true).unwrap().val, 0b1100);
assert_eq!(to_Q(0.0, 1, 5, true).unwrap().val, 0);
assert_eq!(to_Q(1.5, 1, 3, true).unwrap().is_exact, true);
assert_eq!(to_fixed(-2.5, 3, 3, false).unwrap().val, 108);