diff options
| author | David Manescu <dman2626@uni.sydney.edu.au> | 2013-08-12 00:36:24 +1000 |
|---|---|---|
| committer | David Manescu <david.manescu@gmail.com> | 2013-08-12 01:32:05 +1000 |
| commit | 767688fc3d4af0fd7efb4b0b6127f7fa2f4707be (patch) | |
| tree | ba4a589cf4812c0aa84e485c54e5cdf6d39217a8 | |
| parent | cac9affc20984a8a2d01c2ff3201a56f91b5bc6e (diff) | |
| download | rust-767688fc3d4af0fd7efb4b0b6127f7fa2f4707be.tar.gz rust-767688fc3d4af0fd7efb4b0b6127f7fa2f4707be.zip | |
Fixed #8451 - extra::stats::write_boxplot() applied to negative or zero sample values
| -rw-r--r-- | src/libextra/stats.rs | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/src/libextra/stats.rs b/src/libextra/stats.rs index 881d931fe0a..0868c767e16 100644 --- a/src/libextra/stats.rs +++ b/src/libextra/stats.rs @@ -291,10 +291,22 @@ pub fn write_boxplot(w: @io::Writer, s: &Summary, width_hint: uint) { let (q1,q2,q3) = s.quartiles; - let lomag = (10.0_f64).pow(&s.min.log10().floor()); - let himag = (10.0_f64).pow(&(s.max.log10().floor())); - let lo = (s.min / lomag).floor() * lomag; - let hi = (s.max / himag).ceil() * himag; + // the .abs() handles the case where numbers are negative + let lomag = (10.0_f64).pow(&(s.min.abs().log10().floor())); + let himag = (10.0_f64).pow(&(s.max.abs().log10().floor())); + + // need to consider when the limit is zero + let lo = if lomag == 0.0 { + 0.0 + } else { + (s.min / lomag).floor() * lomag + }; + + let hi = if himag == 0.0 { + 0.0 + } else { + (s.max / himag).ceil() * himag + }; let range = hi - lo; @@ -920,4 +932,21 @@ mod tests { }; check(val, summ); } + + #[test] + fn test_boxplot_nonpositive() { + fn t(s: &Summary, expected: ~str) { + let out = do io::with_str_writer |w| { + write_boxplot(w, s, 30) + }; + + assert_eq!(out, expected); + } + + t(&Summary::new([-2.0, -1.0]), ~"-2 |[------******#*****---]| -1"); + t(&Summary::new([0.0, 2.0]), ~"0 |[-------*****#*******---]| 2"); + t(&Summary::new([-2.0, 0.0]), ~"-2 |[------******#******---]| 0"); + + } + } |
