Added trait Rateable.

This commit is contained in:
2018-10-14 20:15:31 +02:00
parent 583c0f8ce6
commit fd228caaf7
2 changed files with 42 additions and 0 deletions

View File

@@ -10,6 +10,24 @@ fn expected(a: f32, b: f32, n: f32) -> f32 {
1.0 / (1.0 + 10.0_f32.powf((b - a) / n))
}
trait Rateable {
fn rating(&mut self) -> &mut f32;
fn record_match(&mut self, other: &mut Self, score: f32) {
let r_a = *self.rating();
let r_b = *other.rating();
*self.rating() = rating(r_a, r_b, score, 400.0, 32.0);
*other.rating() = rating(r_b, r_a, 1.0 - score, 400.0, 32.0);
}
}
impl Rateable for f32 {
fn rating(&mut self) -> &mut f32 {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -24,4 +42,14 @@ mod tests {
assert_eq!(r, 1600.0);
}
#[test]
fn test_rateable() {
let mut a: f32 = 1600.0;
let mut b: f32 = 1600.0;
a.record_match(&mut b, 1.0);
assert_eq!(a, 1616.0);
}
}