diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2014-03-05 16:40:26 -0500 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2014-03-05 19:20:06 -0500 |
| commit | a871068b472d234e6515d5e979fac4826ec1dac1 (patch) | |
| tree | cba5b946a1c2b0fe59207724452c71ed2d7657ab /src/libextra | |
| parent | 5973b0c4ad26729aa1379b98317479f61cd0d87c (diff) | |
| download | rust-a871068b472d234e6515d5e979fac4826ec1dac1.tar.gz rust-a871068b472d234e6515d5e979fac4826ec1dac1.zip | |
stats: fix handling of NaN in `min` and `max`
The `cmp::min` and `cmp::max` functions are not correct with partially ordered values. #12712
Diffstat (limited to 'src/libextra')
| -rw-r--r-- | src/libextra/stats.rs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/libextra/stats.rs b/src/libextra/stats.rs index 2450e22fc10..23e3e104f16 100644 --- a/src/libextra/stats.rs +++ b/src/libextra/stats.rs @@ -10,7 +10,6 @@ #[allow(missing_doc)]; -use std::cmp; use std::hash::Hash; use std::io; use std::mem; @@ -203,12 +202,12 @@ impl<'a> Stats for &'a [f64] { fn min(self) -> f64 { assert!(self.len() != 0); - self.iter().fold(self[0], |p,q| cmp::min(p, *q)) + self.iter().fold(self[0], |p, q| p.min(*q)) } fn max(self) -> f64 { assert!(self.len() != 0); - self.iter().fold(self[0], |p,q| cmp::max(p, *q)) + self.iter().fold(self[0], |p, q| p.max(*q)) } fn mean(self) -> f64 { @@ -442,6 +441,7 @@ mod tests { use stats::write_boxplot; use std::io; use std::str; + use std::f64; macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ @@ -482,6 +482,14 @@ mod tests { } #[test] + fn test_min_max_nan() { + let xs = &[1.0, 2.0, f64::NAN, 3.0, 4.0]; + let summary = Summary::new(xs); + assert_eq!(summary.min, 1.0); + assert_eq!(summary.max, 4.0); + } + + #[test] fn test_norm2() { let val = &[ 958.0000000000, |
