From 5973b0c4ad26729aa1379b98317479f61cd0d87c Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 5 Mar 2014 17:18:56 -0500 Subject: [PATCH 1/2] add tests for `min` and `max` from `Float` --- src/libstd/num/f32.rs | 12 ++++++++++++ src/libstd/num/f64.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 9ec028b76b956..3ba3a9a134f68 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -866,6 +866,18 @@ mod tests { use num::*; use num; + #[test] + fn test_min_nan() { + assert_eq!(NAN.min(2.0), 2.0); + assert_eq!(2.0f32.min(NAN), 2.0); + } + + #[test] + fn test_max_nan() { + assert_eq!(NAN.max(2.0), 2.0); + assert_eq!(2.0f32.max(NAN), 2.0); + } + #[test] fn test_num() { num::test_num(10f32, 2f32); diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index d173abbcde1b3..b95188b07650d 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -865,6 +865,18 @@ mod tests { use num::*; use num; + #[test] + fn test_min_nan() { + assert_eq!(NAN.min(2.0), 2.0); + assert_eq!(2.0f64.min(NAN), 2.0); + } + + #[test] + fn test_max_nan() { + assert_eq!(NAN.max(2.0), 2.0); + assert_eq!(2.0f64.max(NAN), 2.0); + } + #[test] fn test_num() { num::test_num(10f64, 2f64); From a871068b472d234e6515d5e979fac4826ec1dac1 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 5 Mar 2014 16:40:26 -0500 Subject: [PATCH 2/2] stats: fix handling of NaN in `min` and `max` The `cmp::min` and `cmp::max` functions are not correct with partially ordered values. #12712 --- src/libextra/stats.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/libextra/stats.rs b/src/libextra/stats.rs index 2450e22fc105d..23e3e104f164f 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) => ({ @@ -481,6 +481,14 @@ mod tests { assert_eq!(summ.iqr, summ2.iqr); } + #[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 = &[