@@ -291,10 +291,22 @@ pub fn write_boxplot(w: @io::Writer, s: &Summary, width_hint: uint) {
291291
292292 let ( q1, q2, q3) = s. quartiles ;
293293
294- let lomag = ( 10.0_f64 ) . pow ( & s. min . log10 ( ) . floor ( ) ) ;
295- let himag = ( 10.0_f64 ) . pow ( & ( s. max . log10 ( ) . floor ( ) ) ) ;
296- let lo = ( s. min / lomag) . floor ( ) * lomag;
297- let hi = ( s. max / himag) . ceil ( ) * himag;
294+ // the .abs() handles the case where numbers are negative
295+ let lomag = ( 10.0_f64 ) . pow ( & ( s. min . abs ( ) . log10 ( ) . floor ( ) ) ) ;
296+ let himag = ( 10.0_f64 ) . pow ( & ( s. max . abs ( ) . log10 ( ) . floor ( ) ) ) ;
297+
298+ // need to consider when the limit is zero
299+ let lo = if lomag == 0.0 {
300+ 0.0
301+ } else {
302+ ( s. min / lomag) . floor ( ) * lomag
303+ } ;
304+
305+ let hi = if himag == 0.0 {
306+ 0.0
307+ } else {
308+ ( s. max / himag) . ceil ( ) * himag
309+ } ;
298310
299311 let range = hi - lo;
300312
@@ -920,4 +932,21 @@ mod tests {
920932 } ;
921933 check ( val, summ) ;
922934 }
935+
936+ #[ test]
937+ fn test_boxplot_nonpositive( ) {
938+ fn t ( s : & Summary , expected : ~str ) {
939+ let out = do io:: with_str_writer |w| {
940+ write_boxplot ( w, s, 30 )
941+ } ;
942+
943+ assert_eq ! ( out, expected) ;
944+ }
945+
946+ t( & Summary :: new ( [ -2.0 , -1.0 ] ) , ~"-2 |[ ------* * * * * * #* * * * * ---] | -1 ") ;
947+ t( & Summary :: new( [ 0.0 , 2.0 ] ) , ~"0 |[ -------* * * * * #* * * * * * * ---] | 2 ") ;
948+ t( & Summary :: new( [ -2.0 , 0.0 ] ) , ~"-2 |[ ------* * * * * * #* * * * * * ---] | 0 ") ;
949+
950+ }
951+
923952}
0 commit comments