Refactor algorithm slightly

This commit is contained in:
Vinzent Steinberg 2017-05-05 14:16:34 +02:00
parent 52f9e1e5f0
commit 2b6aa2e37e
1 changed files with 3 additions and 3 deletions

View File

@ -40,9 +40,9 @@ impl Average {
/// Add a number to the sequence of which the average is calculated.
pub fn add(&mut self, x: f64) {
self.n += 1;
let prev_avg = self.avg;
self.avg += (x - prev_avg) / f64::approx_from(self.n).unwrap();
self.v += (x - prev_avg) * (x - self.avg);
let delta = x - self.avg;
self.avg += delta / f64::approx_from(self.n).unwrap();
self.v += delta * (x - self.avg);
}
/// Return the mean of the sequence.