MATLAB: Error using zeros Size inputs must be integers.

error using zeros

Hi all, I have a very bizarre problem with zeros. When typing
T=0.1;
fs=1000000;
sig2=zeros(10,(T+0.02)*fs);
I get the following error:
Error using zeros
Size inputs must be integers.
However,the Ordinate values are indeed integers.
I change 0.02 to 0.03,doesn't cause any problem. WHY? How to solve this problem?

Best Answer

" I have a very bizarre problem with zeros"
Not bizarre at all, you just need to learn about how floating point numbers work.
"I change 0.02 to 0.03"
Actually you don't, because neither of those values can be exactly represented using binary floating point numbers. What you see printed in the command window is the closest representation to 5 or 16 significant digits, depending on your current format setting. To see the "real" value download James Tursa's FEX submission:
Use James Tursa's num2strexact and you will see that none of those values really have the exact value 0.02 or 0.03. All you are looking at is a representation of those floating point numbers displayed in the command window, to the precision defined by your format setting. Just because you see 0.02 is displayed tells you nothing about the "real" floating point number's value.
Note that you can change how the value is displayed by changing the format.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well: