Bug in v0.7: Incorrect way to translate input number to histogram bin
mtanti opened this issue · 1 comments
I have found that the way you're converting the numbers into bins is susceptible to floating point precision errors.
If you try the following code:
fast_histogram.histogram1d([33], 126, (0,126))
you will have a 1 in the histogram in the bin for 32 rather than 33.
This is due to:
(33-0)*(1/(126-0))*126
being evaluated to 32.99999999999999 rather than 33.0.
I have found that changing the reciprocal to a division with the number of bins solves this:
(33-0)*126/(126-0)
= 33.0
In effect you need to do the following:
Replace https://github.com/astrofrog/fast-histogram/blob/master/fast_histogram/_histogram_core.c#L147 with
normx = fnx / (xmax - xmin);
and https://github.com/astrofrog/fast-histogram/blob/master/fast_histogram/_histogram_core.c#L166 with
ix = (tx - xmin) * normx
I also just ran into this issue and can confirm that the fix appears to work.