From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: colon operator vs for loop for equally-spaced elements
Date: Wed, 25 Mar 2009 01:47:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 24
Message-ID: <gqc2el$9nf$>
References: <gqbsdi$2vb$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1237945621 9967 (25 Mar 2009 01:47:01 GMT)
NNTP-Posting-Date: Wed, 25 Mar 2009 01:47:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:527461

"Michael" <> wrote in message <gqbsdi$2vb$>...
> 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