Skip to content

Commit

Permalink
Fixed #8451 - extra::stats::write_boxplot() applied to negative or ze…
Browse files Browse the repository at this point in the history
…ro sample values
  • Loading branch information
dmanescu committed Aug 11, 2013
1 parent cac9aff commit 767688f
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/libextra/stats.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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");

}

}

5 comments on commit 767688f

@bors
Copy link
Contributor

@bors bors commented on 767688f Aug 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at dmanescu@767688f

@bors
Copy link
Contributor

@bors bors commented on 767688f Aug 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging dmanescu/rust/8451 = 767688f into auto

@bors
Copy link
Contributor

@bors bors commented on 767688f Aug 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dmanescu/rust/8451 = 767688f merged ok, testing candidate = 836a3d9

@bors
Copy link
Contributor

@bors bors commented on 767688f Aug 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 767688f Aug 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 836a3d9

Please sign in to comment.