MATLAB: Anomalous histogram for small argument

bughistcounthistogramMATLAB

Running the following code from the command line with c = 1e-15 or greater leads to a normal histogram
c = 1e-15; figure; histogram(c * rand(100,1))
histogram1.JPG
Running the following code with c = 1e-16 or less leads to an incorrect histogram
c = 1e-16; figure; histogram(c * rand(100,1))
histogram2.JPG
The problem appears also in apps when the histogram function is invoked.
Is this a bug?
Gordon

Best Answer

Yes it is a BUG. I chase back and in the file
C:\Program Files\MATLAB\R2019a\toolbox\matlab\datafun\+matlab\+internal\+math\binpicker.m
line #20 (R2019a)
there is a test
if xrange > max(sqrt(eps(xscale)), realmin(class(xscale)))
To me the correct test should be (without sqrt)
if xrange > max(eps(xscale), realmin(class(xscale)))
Now if I change this line and rerun the code
c = 1e-16; figure; histogram(c * rand(100,1))
it produces correct plot
More seriously, the function binpicker is called by HISTCOUNT. So HISTCOUNT is also buggy. Not only plotting but calculation might be wrong.