diff --git a/rustapps/advanced_calculator/src/stats/kurtosis.rs b/rustapps/advanced_calculator/src/stats/kurtosis.rs index e238d75..ec2dd28 100644 --- a/rustapps/advanced_calculator/src/stats/kurtosis.rs +++ b/rustapps/advanced_calculator/src/stats/kurtosis.rs @@ -18,7 +18,7 @@ use std::collections::HashMap; /// use advanced_calculator::stats::kurtosis::kurtosis; /// let data = [1.0, 2.0, 3.0, 4.0, 5.0]; /// let result = kurtosis(&data); -/// assert!((result - 1.7).abs() < 0.1); +/// assert!((result - 1.7).abs() < 0.1); // approximate value /// ``` pub fn kurtosis(data: &[f64]) -> f64 { let mean = mean::mean(data); diff --git a/rustapps/advanced_calculator/src/stats/std.rs b/rustapps/advanced_calculator/src/stats/std.rs index 81398a5..75a1c3b 100644 --- a/rustapps/advanced_calculator/src/stats/std.rs +++ b/rustapps/advanced_calculator/src/stats/std.rs @@ -1,3 +1,5 @@ +#![allow(unused_imports)] +use crate::stats::{mean, variance}; /// Calculates the standard deviation of a slice of numbers. /// /// # Arguments @@ -17,15 +19,17 @@ /// assert_eq!(result, 1.5811388300841898); /// ``` pub fn std(data: &[f64]) -> f64 { - let mean = super::mean::mean(data); - let variance = data - .iter() - .map(|value| { - let diff = mean - *value; - diff * diff - }) - .sum::() - / data.len() as f64; - - variance.sqrt() + let var = variance::variance(data); + var.sqrt() } + +// #[cfg(test)] +// mod tests { +// use super::*; + +// #[test] +// fn test_std() { +// let data = [1.0, 2.0, 3.0, 4.0, 5.0]; +// assert!((std(&data) - 1.5811388300841898).abs() < 1e-10); // approximate value +// } +// } diff --git a/rustapps/advanced_calculator/src/stats/variance.rs b/rustapps/advanced_calculator/src/stats/variance.rs index bb9e018..e55c7c6 100644 --- a/rustapps/advanced_calculator/src/stats/variance.rs +++ b/rustapps/advanced_calculator/src/stats/variance.rs @@ -26,3 +26,14 @@ pub fn variance(data: &[f64]) -> f64 { .sum::() / data.len() as f64 } + +// #[cfg(test)] +// mod tests { +// use super::*; + +// #[test] +// fn test_variance() { +// let data = [1.0, 2.0, 3.0, 4.0, 5.0]; +// assert!((variance(&data) - 2.5).abs() < 1e-10); // approximate value +// } +// }