clbustos / statsample

A suite for basic and advanced statistics on Ruby.
http://github.com/clbustos/statsample
BSD 3-Clause "New" or "Revised" License
402 stars 96 forks source link

Prevent FloatDomainError on Histogram with span=0 #36

Closed rdlugosz closed 10 years ago

rdlugosz commented 10 years ago

If a Histogram is created with a set of equal values (e.g., [3,3,3]) a FloatDomainError: -Infinity error will be raised when calling to_svg. This happens because the span calculated in Statsample::Util#nice is 0 (see stacktrace below).

It appears that the original code may have been guarding against this by testing !span, but this is incorrect and should be tested as span == 0. (This may have been a check against nil and not zero, but there are better ways to handle that (e.g., span.nil?) and it also seems that span could not be nil due to the lines of code immediately above, which would raise an error if either s or e were nil.)

FloatDomainError: -Infinity
    /Users/ryan/workspace/statsample/lib/statsample.rb:246:in `round'
    /Users/ryan/workspace/statsample/lib/statsample.rb:246:in `nice'
    /Users/ryan/workspace/statsample/lib/statsample/vector.rb:1004:in
    `histogram'
    /Users/ryan/workspace/statsample/lib/statsample/graph/histogram.rb:77:in
    `pre_vis'
    /Users/ryan/workspace/statsample/lib/statsample/graph/histogram.rb:107:in
    `rubyvis_panel'
    /Users/ryan/workspace/statsample/lib/statsample/graph/histogram.rb:177:in
    `to_svg'
clbustos commented 10 years ago

Thank you very much.

rdlugosz commented 10 years ago

you're welcome!