# Programmatically calculate which bin a value will fall into for a histogram

I'm trying to programmatically create a histogram. The number of bins is specified by the user. My issue in this problem is that I came up with what I thought was a reasonable way to determine which bin a value goes into. I first came up with:

$binNum=\lfloor \frac{value \cdot range}{numBins} \rfloor$

Where value is a number in the set of data, and range is the linear distance between the minimum and maximum values of the data set ($max - min$). I realized that this wouldn't work when I had negative numbers in my values for two reasons: 1) it gives me negative bins and 2) the floor function doesn't do what I want it to with negative numbers: $\lfloor \frac{-9.84 \cdot (7.94 - (-9.84))}{16} \rfloor = -11$. I would like the bins to be from 0 to n-1, so I came up with the following when min is negative:

$binNum = \lfloor \frac{value \cdot range}{numBins} + \vert \frac{min \cdot range}{numBins} \vert \rfloor$

This was used with the thought that if I add the minimum bin to the first formula, it would shift the bins to the correct range of $[0, numBins)$. This, however, has led to too many bins being represented. For instance, when min=-9.84, max=7.94, numBins=16, we get the following for the maximum value's bin: $\lfloor \frac{7.94 \cdot (7.94 - (-9.84)}{16} + \vert\frac{-9.84 \cdot (7.94-(-9.84))}{16}\vert \rfloor = 19$, which is incorrect, it should be 15 (since we're going from 0 - 15). Any suggestions on how I should calculate my bins?

• Welcome to our site! Thanks for posting an interesting question & showing where you're stuck and what you've tried so far. You don't need to post a disclaimer saying you're new. We can see that from your reputation level anyway and we try not to scare the new posters off!.If anything we'd prefer you don't put a "disclaimer: I'm new" at the start of your post, as it is just a distraction for future readers (and gets in the way in the search results, when readers would rather see the first substantive line of your question in the results). I edited it out but I do appreciate the sentiment! – Silverfish Sep 21 '16 at 20:01
• (I also don't think the "programming" tag adds much, since the question isn't fundamentally about programming but rather about how to calculate which bin a value falls in. So I've snipped that as well. Feel free to put it back if you think it adds something.) – Silverfish Sep 21 '16 at 20:02
• Can you specify what range, value and min mean here? – kush Sep 21 '16 at 20:06
• Why do you think it doesn't work with negative numbers in your values ? If the minimum value is -6 and the maximum is -2 and you need four Bins then the bin length is (-2 - (-6) )/4 ? – user83346 Sep 21 '16 at 20:07
• You might be confusing the floor function (as written) with truncation, which is what some programming environments will compute for you. It's difficult to determine what your formulas mean, because you haven't defined what "$value\cdot range$" or "$min\cdots range$" mean, but they seem to be efforts to recreate the correct formula, which for $n$ bins puts a value of any $x$ in the interval $[l, u)$ into bin number $$\lfloor n \frac{x-l}{u-l} \rfloor,$$ where bins are indexed from $0$ through $n-1$. Since both $x-l$ and $u-l$ are non-negative, there is no problem with negative numbers. – whuber Sep 21 '16 at 20:52