Path: news.mathworks.com!not-for-mail
From: "Julio Gonzalez" <julio@part-time-scientists.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: While loop fails
Date: Sat, 22 Oct 2011 15:01:29 +0000 (UTC)
Organization: Part Time Scientists
Lines: 100
Message-ID: <j7uls9$gin$1@newscl01ah.mathworks.com>
References: <j7sh2e$q77$1@newscl01ah.mathworks.com> <j7siql$37t$1@newscl01ah.mathworks.com> <86476fac-163d-4e6e-99e0-0373558ecc9d@t38g2000prg.googlegroups.com>
Reply-To: "Julio Gonzalez" <julio@part-time-scientists.com>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1319295689 16983 172.30.248.46 (22 Oct 2011 15:01:29 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 22 Oct 2011 15:01:29 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1429082
Xref: news.mathworks.com comp.soft-sys.matlab:746786

"Parag S. Chandakkar" <parag2489@gmail.com> wrote in message <86476fac-163d-4e6e-99e0-0373558ecc9d@t38g2000prg.googlegroups.com>...
> On Oct 21, 6:15 pm, "Julio Gonzalez" <ju...@part-time-scientists.com>
> wrote:
> > "ScottB" wrote in message <j7siql$37...@newscl01ah.mathworks.com>...
> > > "Julio Gonzalez" <ju...@part-time-scientists.com> wrote in message <j7sh2e$q7...@newscl01ah.mathworks.com>...
> > > > I am trying to get a while loop to work. I have an integer T, that I converted to double using the function double. But when I print the value of double T, it shows T as an integer and not as a double. Therefore my loop fails. This is the code:
> >
> > > > Ti=2
> > > > T = double(T);
> > > > to= 0.0000;
> >
> > > > while (T<=to)
> > > >     to = to+0.1;
> > > >     %some code here.....
> > > > end
> >
> > > > the loop end at to=2.0000; it should end at 2.1000
> >
> > > > Is there any other way to fix this problems besides T=2*(1+eps)?
> >
> > > > Regards
> >
> > > > Julio
> > > > University of Liverpool
> >
> > > Julio,
> >
> > > T is a double already. I think your example is supposed to look like this:
> >
> > > T=2
> > > to= 0.0000;
> >
> > > while (to<=T)
> > >     to = to+0.1
> > >     %some code here.....
> > > end
> >
> > > The loop stops when to = 2. What are you trying to do?
> >
> > > ScottB
> >
> > Hi Scott,
> >
> > It is a simple while loop.  T is integer equal to 2; "to" increments 0.1 on each pass of the loop until it reaches 2.0. It works while (to<2). But it fails in the checking when
> > to = T = 2.
> >
> > If I print T, I can see only the number "2"... No decimal point.
> >
> > I can not make T double even if I explicitly makes it double. But if I apply T = 2*(1+eps) then I can see now T= 2.0000 and the while loop terminates correctly...
> >
> > Julio
> 
> Hi,
> 
> Don't you think that the condition T<=t0 i.e. 2<=0.1 is false on first
> iteration; therefore; it doesn't even enter into that loop. There is
> another catch in this on which there are already enough articles on
> the web. If you write the program as:
> 
> T=2
> T = double(T);
> to= 0.0000;
> while (T>=to)
>     to = to+0.1;
> to
>     %some code here.....
> end
> 
> Then logically it should give output as to=2.1 but it doesn't since
> you have defined to=0.0000, so at the end of the loop t=2.0000 + eps
> (since you have defined only 4 digits in a double precision format...
> so the difference between T and to is to-T=4.4409e-016.
> You will find a lot of articles on the web if you search this number.
> I am not discussing it here. I will give you one link:
> http://www.eng-tips.com/viewthread.cfm?qid=253704
> 
> You can write your program like this to get it working:
> 
> clear all;
> clc;
> T=2;
> T = double(T); %statement doesn't make a difference
> to= 0;
> while (T-to>=eps || abs(to-T)<=10*eps)
>     to = to+0.1;
> to
>     %some code here.....
> end
> 
> Hope this helps.
> Regards,
> - Parag Shridhar Chandakkar.

Hi Parag,

Thank you for your answer. The first paragraph you are right but it does not apply to my case. 

I will take a look at those links you gave me. Also, thanks for confirming my suspicions about the reason. The problem is the "eps" approximation...  

Regards