|
"Michael" <michael@circular-logic.com> wrote in message <gqbsdi$2vb$1@fred.mathworks.com>...
> Hi,
>
> I'm not sure of the proper terminology here, so I hope you can understand.
>
> if I create two vectors like so:
>
> vcolon = 0:0.05:100;
>
> for i=0:2000
> vfor(i+1) = i * 0.05;
> end
>
>
> then I take the difference between the two, ther are some elements with non-zero difference. These differences are less than epsilon. This seems like a machine precision or otherwise floating-point calc issue.
>
> My question is, what does the '0:0.05:100' do differently than the for loop?
>
> Thanks,
> Michael
Yes it is a binary floating point accuracy issue. The number .05 cannot be represented exactly in binary because in reduced form the fraction's denominator has a factor of 5 which is not a power of 2. Whatever error there is in its nearest binary approximation will cause the error in i*.05 to grow larger as i increases, so it is highly unlikely that your vfor(2000) would round to exactly 100. On the other hand, it seems pretty clear that matlab arranges things in the colon operator logic for a:b:c so that at least at the endpoints there is no error if a+b*i is reasonably close to c. Clearly other unspecified adjustments are made along the line also. It would be interesting to make a study of these to see if one could penetrate Mathworks' innermost secrets in the colon operator.
Roger Stafford
|