MATLAB: Matlab Floating point question

fixed-step

I would like to create a time array with the same samplingrate…the top example works…the bottom does not….do you know why this happens???
start = single(0);
endit = single(199532);
samprate = single(8.333333);
%Works just fine
figure(1)
subplot(2,1,1)
plot(diff(start:endit)/samprate)
title('Sampling rate is consistently 0.12 (Correct)');
%Doesn't work just fine
array = (start:endit)/samprate;
subplot(2,1,2)
plot(diff(array))
title('Sampling rate is NOT consistently 0.12 (Incorrect)');

Best Answer

The correct term to describe this is not "incorrect" but "inaccurate". With the limited precision of floating point values, both results are absolute correct. But the 2nd example shows the effects of floating point arithmetic. See the famous example: 0.3 - 0.2 - 0.1 == 0 ==> FALSE! This has been discussed such frequently, that you find it in the frequently asked questions.