pub fn raw_score(symmetric: f64, asymmetric: f64) -> f32 {
(4.5 - 0.1 * symmetric - 0.0309 * asymmetric) as f32
}
pub fn mos_lqo(raw: f64) -> f32 {
(0.999 + 4.0 / (1.0 + (-1.4945 * raw + 4.6607).exp())) as f32
}
pub fn mos_lqo_wb(raw: f64) -> f32 {
(0.999 + 4.0 / (1.0 + (-1.3669 * raw + 3.8224).exp())) as f32
}
pub fn round_3dp(value: f32) -> f32 {
(value * 1000.0).round() / 1000.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn raw_score_matches_spec_5_1() {
let score = raw_score(10.0, 20.0);
assert!((score - 2.882).abs() < 1e-5, "got {score}");
assert_eq!(raw_score(0.0, 0.0), 4.5);
}
#[test]
fn mos_lqo_stays_in_range_and_grows() {
for raw in [-0.5, 0.0, 1.0, 2.5, 4.5] {
let mos = mos_lqo(raw);
assert!(
(0.9..=4.7).contains(&mos),
"mos_lqo({raw}) = {mos} is out of range"
);
}
assert!(mos_lqo(4.5) > mos_lqo(1.0));
assert!(mos_lqo(1.0) > mos_lqo(-0.5));
}
#[test]
fn rounding_keeps_three_decimals() {
assert_eq!(round_3dp(2.2374), 2.237);
assert_eq!(round_3dp(2.2375), 2.238);
assert_eq!(round_3dp(-0.0004), -0.0);
}
#[test]
fn mos_lqo_matches_hand_computed_values() {
assert!((mos_lqo(0.0) - 1.03648).abs() < 1e-4);
assert!((mos_lqo(4.5) - 4.54864).abs() < 1e-4);
}
#[test]
fn mos_lqo_wb_matches_hand_computed_values() {
assert!((mos_lqo_wb(3.575) - 3.9730).abs() < 5e-4);
assert!((mos_lqo_wb(2.798) - 3.00118).abs() < 5e-4);
assert!(mos_lqo_wb(4.5) > mos_lqo_wb(1.0));
assert!(mos_lqo_wb(1.0) > mos_lqo_wb(-0.5));
assert!(mos_lqo_wb(-0.5) > 0.999);
assert!(mos_lqo_wb(4.5) < 4.999);
}
}