use crate::round_tools::{nearest_multiple_shortest_repr, ot_round};
pub const MAX_F2DOT14: f64 = 0x7FFF as f64 / (1 << 14) as f64;
fn scale(precision_bits: u32) -> f64 {
(1u64 << precision_bits) as f64
}
pub fn fixed_to_float(value: i64, precision_bits: u32) -> f64 {
value as f64 / scale(precision_bits)
}
pub fn float_to_fixed(value: f64, precision_bits: u32) -> i64 {
ot_round(value * scale(precision_bits))
}
pub fn float_to_fixed_to_float(value: f64, precision_bits: u32) -> f64 {
let scale = scale(precision_bits);
ot_round(value * scale) as f64 / scale
}
pub fn fixed_to_str(value: i64, precision_bits: u32) -> String {
let scale = scale(precision_bits);
nearest_multiple_shortest_repr(value as f64 / scale, 1.0 / scale)
}
pub fn str_to_fixed(string: &str, precision_bits: u32) -> Option<i64> {
let value = crate::py::parse_float(string)?;
Some(ot_round(value * scale(precision_bits)))
}
pub fn str_to_fixed_to_float(string: &str, precision_bits: u32) -> Option<f64> {
let value = crate::py::parse_float(string)?;
let scale = scale(precision_bits);
Some(ot_round(value * scale) as f64 / scale)
}
pub fn float_to_fixed_to_str(value: f64, precision_bits: u32) -> String {
nearest_multiple_shortest_repr(value, 1.0 / scale(precision_bits))
}
pub fn ensure_version_is_long(value: f64) -> i64 {
if value < 65536.0 {
let new_value = float_to_fixed(value, 16);
log::warn!(
"Table version value is a float: {value:.4}; fix to use hex instead: 0x{new_value:08x}"
);
new_value
} else {
value as i64
}
}
pub fn version_to_fixed(value: &str) -> Option<i64> {
let v = if value.starts_with('0') {
crate::text_tools::parse_int_literal(value)? as f64
} else {
crate::py::parse_float(value)?
};
Some(ensure_version_is_long(v))
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_almost_eq(a: f64, b: f64) {
assert!((a - b).abs() < 5e-8, "{a} != {b}");
}
#[test]
fn test_roundtrip() {
for bits in 0..15 {
for value in -(1i64 << (bits + 1))..(1i64 << (bits + 1)) {
assert_eq!(value, float_to_fixed(fixed_to_float(value, bits), bits));
}
}
}
#[test]
fn test_fixed_to_float_precision14() {
assert_almost_eq(0.7999878, fixed_to_float(13107, 14));
assert_eq!(0.0, fixed_to_float(0, 14));
assert_eq!(1.0, fixed_to_float(16384, 14));
assert_eq!(-1.0, fixed_to_float(-16384, 14));
assert_almost_eq(0.999939, fixed_to_float(16383, 14));
assert_almost_eq(-0.999939, fixed_to_float(-16383, 14));
}
#[test]
fn test_fixed_to_float_precision6() {
assert_almost_eq(-9.984375, fixed_to_float(-639, 6));
assert_almost_eq(-10.0, fixed_to_float(-640, 6));
assert_almost_eq(9.984375, fixed_to_float(639, 6));
assert_almost_eq(10.0, fixed_to_float(640, 6));
}
#[test]
fn test_fixed_to_str_precision14() {
assert_eq!("0.8", fixed_to_str(13107, 14));
assert_eq!("0.0", fixed_to_str(0, 14));
assert_eq!("1.0", fixed_to_str(16384, 14));
assert_eq!("-1.0", fixed_to_str(-16384, 14));
assert_eq!("0.99994", fixed_to_str(16383, 14));
assert_eq!("-0.99994", fixed_to_str(-16383, 14));
}
#[test]
fn test_fixed_to_str_precision6() {
assert_eq!("-9.98", fixed_to_str(-639, 6));
assert_eq!("-10.0", fixed_to_str(-640, 6));
assert_eq!("9.98", fixed_to_str(639, 6));
assert_eq!("10.0", fixed_to_str(640, 6));
}
#[test]
fn test_float_to_fixed_precision14() {
assert_eq!(13107, float_to_fixed(0.8, 14));
assert_eq!(16384, float_to_fixed(1.0, 14));
assert_eq!(-16384, float_to_fixed(-1.0, 14));
assert_eq!(0, float_to_fixed(0.0, 14));
}
#[test]
fn test_str_to_fixed_precision14() {
assert_eq!(Some(13107), str_to_fixed("0.8", 14));
assert_eq!(Some(16384), str_to_fixed("1.0", 14));
assert_eq!(Some(16384), str_to_fixed("1", 14));
assert_eq!(Some(-16384), str_to_fixed("-1.0", 14));
assert_eq!(Some(-16384), str_to_fixed("-1", 14));
assert_eq!(Some(0), str_to_fixed("0", 14));
}
#[test]
fn test_str_to_fixed_to_float_precision14() {
assert_almost_eq(
0.7999878,
str_to_fixed_to_float("0.8", 14).unwrap_or_default(),
);
assert_eq!(Some(0.0), str_to_fixed_to_float("0", 14));
assert_eq!(Some(1.0), str_to_fixed_to_float("1.0", 14));
assert_eq!(Some(-1.0), str_to_fixed_to_float("-1.0", 14));
assert_almost_eq(
0.999939,
str_to_fixed_to_float("0.99994", 14).unwrap_or_default(),
);
assert_almost_eq(
-0.999939,
str_to_fixed_to_float("-0.99994", 14).unwrap_or_default(),
);
}
#[test]
fn test_float_to_fixed_to_str_precision14() {
assert_eq!("0.8", float_to_fixed_to_str(0.7999878, 14));
assert_eq!("1.0", float_to_fixed_to_str(1.0, 14));
assert_eq!("-1.0", float_to_fixed_to_str(-1.0, 14));
assert_eq!("0.0", float_to_fixed_to_str(0.0, 14));
}
#[test]
fn doctests() {
assert_eq!(float_to_fixed(-0.61883544921875, 14), -10139);
assert_eq!(float_to_fixed(-0.61884, 14), -10139);
assert_eq!(fixed_to_str(-10139, 14), "-0.61884");
assert_eq!(str_to_fixed("-0.61884", 14), Some(-10139));
assert_eq!(float_to_fixed_to_str(-0.61883544921875, 14), "-0.61884");
let f2 = float_to_fixed_to_float(-0.61884, 14);
assert_ne!(f2, -0.61884);
assert_almost_eq(f2, -0.61883544921875);
}
}