use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct UnitScore(f32);
impl UnitScore {
pub fn new(value: f32) -> Self {
if value.is_nan() {
Self(0.0)
} else {
Self(value.clamp(0.0, 1.0))
}
}
pub fn value(&self) -> f32 {
self.0
}
}
impl Default for UnitScore {
fn default() -> Self {
Self(0.5)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_midpoint() {
assert!((UnitScore::default().value() - 0.5).abs() < f32::EPSILON);
}
}