Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
References: <gqbsdi$2vb$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1237945621 9967 172.30.248.37 (25 Mar 2009 01:47:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 25 Mar 2009 01:47:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:527461

"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