I was trying to do sub2ind on single-precision variables that I came across the following strange behavior. For example, when I try:
[a b] = ind2sub([50000 50000], sub2ind([50000 50000], single(1000), single(1000)))
I get:
a = 1001
b = 1000
Is this a bug or I am missing something? I know this can be because of an overflow somewhere in matlab’s code but it shouldn’t happen, right?
I get the same wrong behavior from 64-bit (glnxa64) R2012a, R2011a, R2010b, R2010a, but correct results from 32-bit (glnx86) R2010b.
The reason this happens is the following.
On line 35 of ind2sub.m is
where ndx is the index that is passed in. Thus ndx is 49951000 via the call from sub2ind. Now, when you pass in a single precision value, it forces matlab to evaluate all math in single precision. Thus, compare the difference in what happens on l.35.
This subtraction of a small number from a large number is the issue. So no, this isn’t a bug, it’s a limitation to the precision of single-precision floating point. This might help some.
edit: As pointed out by Rasman, a nice way of showing this is with eps
so any value added or subtracted from single(49951000) that is on the range (-4,4) will result in 495100 being returned due to the accuracy of single-precision.