Docs, no_std, crate metadata
This commit is contained in:
parent
7823ebe622
commit
c0d559a2a2
@ -2,6 +2,11 @@
|
||||
authors = ["Vinzent Steinberg <Vinzent.Steinberg@gmail.com>"]
|
||||
name = "average"
|
||||
version = "0.1.0"
|
||||
license = "MIT/Apache-2.0"
|
||||
repository = "https://github.com/vks/average"
|
||||
description = "Calculate the average of a sequence and its error iteratively"
|
||||
keywords = ["statistics", "stats"]
|
||||
categories = ["science", "no-std"]
|
||||
|
||||
[dependencies]
|
||||
conv = "0.3"
|
||||
|
27
src/lib.rs
27
src/lib.rs
@ -1,11 +1,21 @@
|
||||
#![no_std]
|
||||
|
||||
extern crate conv;
|
||||
|
||||
use conv::ApproxFrom;
|
||||
|
||||
/// Represent and average value of a sequence of numbers.
|
||||
/// Represent an average value of a sequence of numbers.
|
||||
///
|
||||
/// The average is calculated iteratively, so the sequence of numbers can be an
|
||||
/// iterator.
|
||||
///
|
||||
/// ```
|
||||
/// use average::Average;
|
||||
///
|
||||
/// let a: Average = (1..6).map(|x| x as f64).collect();
|
||||
/// assert_eq!(a.avg(), 3.0);
|
||||
/// assert_eq!(a.var(), 2.5);
|
||||
/// ```
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Average {
|
||||
avg: f64,
|
||||
@ -54,7 +64,7 @@ impl Average {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::iter::FromIterator<f64> for Average {
|
||||
impl core::iter::FromIterator<f64> for Average {
|
||||
fn from_iter<T>(iter: T) -> Average
|
||||
where T: IntoIterator<Item=f64>
|
||||
{
|
||||
@ -73,7 +83,18 @@ mod tests {
|
||||
use ::conv::ConvAsUtil;
|
||||
|
||||
#[test]
|
||||
fn average() {
|
||||
fn average_trivial() {
|
||||
let mut a = Average::new();
|
||||
assert_eq!(a.len(), 0);
|
||||
a.add(1.0);
|
||||
assert_eq!(a.avg(), 1.0);
|
||||
assert_eq!(a.len(), 1);
|
||||
assert_eq!(a.var(), 0.0);
|
||||
assert_eq!(a.err(), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn average_simple() {
|
||||
let a: Average = (1..6).map(|x| x.approx().unwrap()).collect();
|
||||
assert_eq!(a.avg(), 3.0);
|
||||
assert_eq!(a.len(), 5);
|
||||
|
Loading…
x
Reference in New Issue
Block a user