# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.88178724,"math_prob":0.9514274,"size":1459,"snap":"2019-51-2020-05","text_gpt3_token_len":421,"char_repetition_ratio":0.118900344,"word_repetition_ratio":0.0,"special_character_ratio":0.30911583,"punctuation_ratio":0.118589744,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99621964,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-05T19:33:06Z\",\"WARC-Record-ID\":\"<urn:uuid:f95d5e59-e8e8-44e8-a69c-09e86d7cb3c0>\",\"Content-Length\":\"133770\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:484a5730-8e41-43e4-8d22-38cdbb082200>\",\"WARC-Concurrent-To\":\"<urn:uuid:1b24b5aa-3156-44a1-b248-1c10a51e68db>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://stats.stackexchange.com/questions/236205/programmatically-calculate-which-bin-a-value-will-fall-into-for-a-histogram\",\"WARC-Payload-Digest\":\"sha1:VL2FD723EQIPBKM5RYMOVFU2UHJNVYUB\",\"WARC-Block-Digest\":\"sha1:KIGVFFY3HXJUTVJVJDF5PUJVNNELJW5N\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540482038.36_warc_CC-MAIN-20191205190939-20191205214939-00247.warc.gz\"}"}