Merge branch 'master' of https://github.com/vks/average
Conflicts: Cargo.toml
This commit is contained in:
commit
597311ec7f
@ -4,9 +4,12 @@ name = "average"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
repository = "https://github.com/vks/average"
|
repository = "https://github.com/vks/average"
|
||||||
description = "Calculate the average of a sequence iteratively"
|
description = "Calculate the average of a sequence and its error iteratively"
|
||||||
keywords = ["statistics", "stats"]
|
|
||||||
categories = ["science", "no-std"]
|
categories = ["science", "no-std"]
|
||||||
|
keywords = ["statistics", "stats"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
conv = "0.3"
|
conv = "0.3"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
rand = "0.3"
|
||||||
|
24
src/lib.rs
24
src/lib.rs
@ -1,6 +1,8 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
extern crate conv;
|
extern crate conv;
|
||||||
|
#[cfg(test)] extern crate rand;
|
||||||
|
#[cfg(test)] #[macro_use] extern crate std;
|
||||||
|
|
||||||
use conv::ApproxFrom;
|
use conv::ApproxFrom;
|
||||||
|
|
||||||
@ -76,6 +78,14 @@ impl core::iter::FromIterator<f64> for Average {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! assert_almost_eq {
|
||||||
|
($a:expr, $b:expr, $prec:expr) => (
|
||||||
|
if ($a - $b).abs() > $prec {
|
||||||
|
panic!(format!("assertion failed: `abs(left - right) < {:e}`, (left: `{}`, right: `{}`)", $prec, $a, $b));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -99,6 +109,18 @@ mod tests {
|
|||||||
assert_eq!(a.avg(), 3.0);
|
assert_eq!(a.avg(), 3.0);
|
||||||
assert_eq!(a.len(), 5);
|
assert_eq!(a.len(), 5);
|
||||||
assert_eq!(a.var(), 2.5);
|
assert_eq!(a.var(), 2.5);
|
||||||
assert!((a.err() - f64::sqrt(0.5)).abs() < 1e-16);
|
assert_almost_eq!(a.err(), f64::sqrt(0.5), 1e-16);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn average_normal_distribution() {
|
||||||
|
use rand::distributions::{Normal, IndependentSample};
|
||||||
|
let normal = Normal::new(2.0, 3.0);
|
||||||
|
let mut a = Average::new();
|
||||||
|
for _ in 0..1000000 {
|
||||||
|
a.add(normal.ind_sample(&mut ::rand::thread_rng()));
|
||||||
|
}
|
||||||
|
assert_almost_eq!(a.avg(), 2.0, 1e-2);
|
||||||
|
assert_almost_eq!(a.var().sqrt(), 3.0, 1e-2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user