From 583c0f8ce60ad16d5bc8e46211d14aa6e5b345c2 Mon Sep 17 00:00:00 2001 From: Anders Olsson Date: Sat, 13 Oct 2018 11:40:03 +0200 Subject: [PATCH] Initial commit. --- .gitignore | 3 +++ Cargo.toml | 6 ++++++ src/lib.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6936990 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9213efd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "elo" +version = "0.1.0" +authors = ["Anders Olsson "] + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5538659 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,27 @@ +pub fn rating(a: f32, b: f32, score: f32, n: f32, k: f32) -> f32 { + a + delta(score, expected(a, b, n), k) +} + +fn delta(score: f32, rating: f32, k: f32) -> f32 { + k * (score - rating) +} + +fn expected(a: f32, b: f32, n: f32) -> f32 { + 1.0 / (1.0 + 10.0_f32.powf((b - a) / n)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_rating() { + let r = rating(1600.0, 1600.0, 1.0, 400.0, 32.0); + + assert_eq!(r, 1616.0); + + let r = rating(1600.0, 1600.0, 0.5, 400.0, 32.0); + + assert_eq!(r, 1600.0); + } +}