Last active
August 29, 2015 14:04
-
-
Save tybl/e2121311cf5d940c2cc2 to your computer and use it in GitHub Desktop.
Rust struct for calculating the running average(mean)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::num::{ zero, one }; | |
struct Mean { | |
mean: f64, | |
count: u64, | |
} | |
impl Mean { | |
fn consider(&mut self, other: f64) { | |
self.count = self.count + one(); | |
let delta = other - self.mean; | |
self.mean = self.mean + (delta / self.count as f64); | |
} | |
fn mean(&self) -> f64 { | |
self.mean | |
} | |
fn new() -> Mean { | |
Mean { mean: zero(), count: zero() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment