Skip to content

Commit a871068

Browse files
committed
stats: fix handling of NaN in min and max
The `cmp::min` and `cmp::max` functions are not correct with partially ordered values. #12712
1 parent 5973b0c commit a871068

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/libextra/stats.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#[allow(missing_doc)];
1212

13-
use std::cmp;
1413
use std::hash::Hash;
1514
use std::io;
1615
use std::mem;
@@ -203,12 +202,12 @@ impl<'a> Stats for &'a [f64] {
203202

204203
fn min(self) -> f64 {
205204
assert!(self.len() != 0);
206-
self.iter().fold(self[0], |p,q| cmp::min(p, *q))
205+
self.iter().fold(self[0], |p, q| p.min(*q))
207206
}
208207

209208
fn max(self) -> f64 {
210209
assert!(self.len() != 0);
211-
self.iter().fold(self[0], |p,q| cmp::max(p, *q))
210+
self.iter().fold(self[0], |p, q| p.max(*q))
212211
}
213212

214213
fn mean(self) -> f64 {
@@ -442,6 +441,7 @@ mod tests {
442441
use stats::write_boxplot;
443442
use std::io;
444443
use std::str;
444+
use std::f64;
445445

446446
macro_rules! assert_approx_eq(
447447
($a:expr, $b:expr) => ({
@@ -481,6 +481,14 @@ mod tests {
481481
assert_eq!(summ.iqr, summ2.iqr);
482482
}
483483

484+
#[test]
485+
fn test_min_max_nan() {
486+
let xs = &[1.0, 2.0, f64::NAN, 3.0, 4.0];
487+
let summary = Summary::new(xs);
488+
assert_eq!(summary.min, 1.0);
489+
assert_eq!(summary.max, 4.0);
490+
}
491+
484492
#[test]
485493
fn test_norm2() {
486494
let val = &[

0 commit comments

Comments
 (0)