http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610
MATLAB Central Newsreader  While loop fails
Feed for thread: While loop fails
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 21 Oct 2011 19:27:10 +0000
While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855839
Julio Gonzalez
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:<br>
<br>
Ti=2<br>
T = double(T);<br>
to= 0.0000;<br>
<br>
while (T<=to) <br>
to = to+0.1;<br>
%some code here.....<br>
end<br>
<br>
the loop end at to=2.0000; it should end at 2.1000<br>
<br>
Is there any other way to fix this problems besides T=2*(1+eps)?<br>
<br>
Regards<br>
<br>
Julio<br>
University of Liverpool

Fri, 21 Oct 2011 19:57:09 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855844
ScottB
"Julio Gonzalez" <julio@parttimescientists.com> wrote in message <j7sh2e$q77$1@newscl01ah.mathworks.com>...<br>
> 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:<br>
> <br>
> Ti=2<br>
> T = double(T);<br>
> to= 0.0000;<br>
> <br>
> while (T<=to) <br>
> to = to+0.1;<br>
> %some code here.....<br>
> end<br>
> <br>
> the loop end at to=2.0000; it should end at 2.1000<br>
> <br>
> Is there any other way to fix this problems besides T=2*(1+eps)?<br>
> <br>
> Regards<br>
> <br>
> Julio<br>
> University of Liverpool<br>
<br>
Julio,<br>
<br>
T is a double already. I think your example is supposed to look like this:<br>
<br>
T=2<br>
to= 0.0000;<br>
<br>
while (to<=T) <br>
to = to+0.1<br>
%some code here.....<br>
end<br>
<br>
The loop stops when to = 2. What are you trying to do?<br>
<br>
ScottB

Sat, 22 Oct 2011 01:15:30 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855865
Julio Gonzalez
"ScottB" wrote in message <j7siql$37t$1@newscl01ah.mathworks.com>...<br>
> "Julio Gonzalez" <julio@parttimescientists.com> wrote in message <j7sh2e$q77$1@newscl01ah.mathworks.com>...<br>
> > 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:<br>
> > <br>
> > Ti=2<br>
> > T = double(T);<br>
> > to= 0.0000;<br>
> > <br>
> > while (T<=to) <br>
> > to = to+0.1;<br>
> > %some code here.....<br>
> > end<br>
> > <br>
> > the loop end at to=2.0000; it should end at 2.1000<br>
> > <br>
> > Is there any other way to fix this problems besides T=2*(1+eps)?<br>
> > <br>
> > Regards<br>
> > <br>
> > Julio<br>
> > University of Liverpool<br>
> <br>
> Julio,<br>
> <br>
> T is a double already. I think your example is supposed to look like this:<br>
> <br>
> T=2<br>
> to= 0.0000;<br>
> <br>
> while (to<=T) <br>
> to = to+0.1<br>
> %some code here.....<br>
> end<br>
> <br>
> The loop stops when to = 2. What are you trying to do?<br>
> <br>
> ScottB<br>
Hi Scott,<br>
<br>
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 <br>
to = T = 2. <br>
<br>
If I print T, I can see only the number "2"... No decimal point. <br>
<br>
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...<br>
<br>
Julio

Sat, 22 Oct 2011 03:09:15 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855867
Roger Stafford
"Julio Gonzalez" <julio@parttimescientists.com> wrote in message <j7sh2e$q77$1@newscl01ah.mathworks.com>...<br>
> ......<br>
> to = to+0.1;<br>
> ......<br>
       <br>
Julio, after twenty executions of the line<br>
<br>
to = to+0.1;<br>
<br>
starting with 'to' equal to zero, the value of 'to' will not be an exact 2.0, but rather 2+2^(51), which is greater than 2 by a very tiny amount. You can use 'format hex' to show this. Does this explain things? This error is caused by the fact that a binary floating point number cannot be exactly equal to 0.1 = 1/10 for just the same reason that decimal fractions can never be exactly equal to 1/3. After twenty additions by this slightly erroneous value it is only in excess of an exact 2.0 by one bit at its least significant end, but that is presumably enough to cause trouble with the method you are using in your loop. You need to alter your method, perhaps by counting with integers.<br>
<br>
Roger Stafford

Sat, 22 Oct 2011 03:02:18 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855868
Parag S. Chandakkar
On Oct 21, 6:15 pm, "Julio Gonzalez" <ju...@parttimescientists.com><br>
wrote:<br>
> "ScottB" wrote in message <j7siql$37...@newscl01ah.mathworks.com>...<br>
> > "Julio Gonzalez" <ju...@parttimescientists.com> wrote in message <j7sh2e$q7...@newscl01ah.mathworks.com>...<br>
> > > 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:<br>
><br>
> > > Ti=2<br>
> > > T = double(T);<br>
> > > to= 0.0000;<br>
><br>
> > > while (T<=to)<br>
> > > to = to+0.1;<br>
> > > %some code here.....<br>
> > > end<br>
><br>
> > > the loop end at to=2.0000; it should end at 2.1000<br>
><br>
> > > Is there any other way to fix this problems besides T=2*(1+eps)?<br>
><br>
> > > Regards<br>
><br>
> > > Julio<br>
> > > University of Liverpool<br>
><br>
> > Julio,<br>
><br>
> > T is a double already. I think your example is supposed to look like this:<br>
><br>
> > T=2<br>
> > to= 0.0000;<br>
><br>
> > while (to<=T)<br>
> > to = to+0.1<br>
> > %some code here.....<br>
> > end<br>
><br>
> > The loop stops when to = 2. What are you trying to do?<br>
><br>
> > ScottB<br>
><br>
> Hi Scott,<br>
><br>
> 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<br>
> to = T = 2.<br>
><br>
> If I print T, I can see only the number "2"... No decimal point.<br>
><br>
> 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...<br>
><br>
> Julio<br>
<br>
Hi,<br>
<br>
Don't you think that the condition T<=t0 i.e. 2<=0.1 is false on first<br>
iteration; therefore; it doesn't even enter into that loop. There is<br>
another catch in this on which there are already enough articles on<br>
the web. If you write the program as:<br>
<br>
T=2<br>
T = double(T);<br>
to= 0.0000;<br>
while (T>=to)<br>
to = to+0.1;<br>
to<br>
%some code here.....<br>
end<br>
<br>
Then logically it should give output as to=2.1 but it doesn't since<br>
you have defined to=0.0000, so at the end of the loop t=2.0000 + eps<br>
(since you have defined only 4 digits in a double precision format...<br>
so the difference between T and to is toT=4.4409e016.<br>
You will find a lot of articles on the web if you search this number.<br>
I am not discussing it here. I will give you one link:<br>
<a href="http://www.engtips.com/viewthread.cfm?qid=253704">http://www.engtips.com/viewthread.cfm?qid=253704</a><br>
<br>
You can write your program like this to get it working:<br>
<br>
clear all;<br>
clc;<br>
T=2;<br>
T = double(T); %statement doesn't make a difference<br>
to= 0;<br>
while (Tto>=eps  abs(toT)<=10*eps)<br>
to = to+0.1;<br>
to<br>
%some code here.....<br>
end<br>
<br>
Hope this helps.<br>
Regards,<br>
 Parag Shridhar Chandakkar.

Sat, 22 Oct 2011 15:01:29 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855901
Julio Gonzalez
"Parag S. Chandakkar" <parag2489@gmail.com> wrote in message <86476fac163d4e6e99e00373558ecc9d@t38g2000prg.googlegroups.com>...<br>
> On Oct 21, 6:15 pm, "Julio Gonzalez" <ju...@parttimescientists.com><br>
> wrote:<br>
> > "ScottB" wrote in message <j7siql$37...@newscl01ah.mathworks.com>...<br>
> > > "Julio Gonzalez" <ju...@parttimescientists.com> wrote in message <j7sh2e$q7...@newscl01ah.mathworks.com>...<br>
> > > > 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:<br>
> ><br>
> > > > Ti=2<br>
> > > > T = double(T);<br>
> > > > to= 0.0000;<br>
> ><br>
> > > > while (T<=to)<br>
> > > > to = to+0.1;<br>
> > > > %some code here.....<br>
> > > > end<br>
> ><br>
> > > > the loop end at to=2.0000; it should end at 2.1000<br>
> ><br>
> > > > Is there any other way to fix this problems besides T=2*(1+eps)?<br>
> ><br>
> > > > Regards<br>
> ><br>
> > > > Julio<br>
> > > > University of Liverpool<br>
> ><br>
> > > Julio,<br>
> ><br>
> > > T is a double already. I think your example is supposed to look like this:<br>
> ><br>
> > > T=2<br>
> > > to= 0.0000;<br>
> ><br>
> > > while (to<=T)<br>
> > > to = to+0.1<br>
> > > %some code here.....<br>
> > > end<br>
> ><br>
> > > The loop stops when to = 2. What are you trying to do?<br>
> ><br>
> > > ScottB<br>
> ><br>
> > Hi Scott,<br>
> ><br>
> > 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<br>
> > to = T = 2.<br>
> ><br>
> > If I print T, I can see only the number "2"... No decimal point.<br>
> ><br>
> > 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...<br>
> ><br>
> > Julio<br>
> <br>
> Hi,<br>
> <br>
> Don't you think that the condition T<=t0 i.e. 2<=0.1 is false on first<br>
> iteration; therefore; it doesn't even enter into that loop. There is<br>
> another catch in this on which there are already enough articles on<br>
> the web. If you write the program as:<br>
> <br>
> T=2<br>
> T = double(T);<br>
> to= 0.0000;<br>
> while (T>=to)<br>
> to = to+0.1;<br>
> to<br>
> %some code here.....<br>
> end<br>
> <br>
> Then logically it should give output as to=2.1 but it doesn't since<br>
> you have defined to=0.0000, so at the end of the loop t=2.0000 + eps<br>
> (since you have defined only 4 digits in a double precision format...<br>
> so the difference between T and to is toT=4.4409e016.<br>
> You will find a lot of articles on the web if you search this number.<br>
> I am not discussing it here. I will give you one link:<br>
> <a href="http://www.engtips.com/viewthread.cfm?qid=253704">http://www.engtips.com/viewthread.cfm?qid=253704</a><br>
> <br>
> You can write your program like this to get it working:<br>
> <br>
> clear all;<br>
> clc;<br>
> T=2;<br>
> T = double(T); %statement doesn't make a difference<br>
> to= 0;<br>
> while (Tto>=eps  abs(toT)<=10*eps)<br>
> to = to+0.1;<br>
> to<br>
> %some code here.....<br>
> end<br>
> <br>
> Hope this helps.<br>
> Regards,<br>
>  Parag Shridhar Chandakkar.<br>
<br>
Hi Parag,<br>
<br>
Thank you for your answer. The first paragraph you are right but it does not apply to my case. <br>
<br>
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... <br>
<br>
Regards

Sat, 22 Oct 2011 18:06:23 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855907
dpb
On 10/21/2011 8:15 PM, Julio Gonzalez wrote:<br>
...<br>
<br>
> It is a simple while loop. T is integer equal to 2; ...<br>
<br>
no, No, NO!!!<br>
<br>
T is a (Matlab default) double which _contains_ an integral value of 2.<br>
<br>
Insert<br>
<br>
whos T<br>
<br>
after the line T=2 if you don't believe it.<br>
<br>
> ... "to" increments 0.1<br>
> on each pass of the loop until it reaches 2.0. It works while (to<2).<br>
> But it fails in the checking when to = T = 2.<br>
> If I print T, I can see only the number "2"... No decimal point.<br>
> I can not make T double even if I explicitly makes it double. But if I<br>
> apply T = 2*(1+eps) then I can see now T= 2.0000 and the while loop<br>
> terminates correctly...<br>
<br>
This depends on your definition of "correctly".<br>
<br>
Use<br>
<br>
format long<br>
<br>
to see the long value of any intermediate values (or display the values <br>
of t02 to amplify the floating point remainder).<br>
<br>
As another respondent said, the "problem" is one of floating point <br>
precision and the use of t+dt is the most erroraccumulating way one can <br>
possibly find to compute a running total; if dt is under the exact value <br>
by one bit, that one bit short will accumulate every iteration through. <br>
OTOH, if you were to compute the time as t=n*dt, the error would <br>
average out.<br>
<br>
Try the two following examples at the command line...<br>
<br>
x=0;for idx=1:10000,x=x+0.1;end, x1000<br>
<br>
and for comparison<br>
<br>
x=0;for idx=1:10001,x=(idx1)*0.1;end, x1000<br>
<br>


Sun, 23 Oct 2011 15:10:28 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855958
Julio Gonzalez
dpb <none@non.net> wrote in message <j7v0n2$qof$1@speranza.aioe.org>...<br>
> On 10/21/2011 8:15 PM, Julio Gonzalez wrote:<br>
> ...<br>
> <br>
> > It is a simple while loop. T is integer equal to 2; ...<br>
> <br>
> no, No, NO!!!<br>
> <br>
> T is a (Matlab default) double which _contains_ an integral value of 2.<br>
> <br>
> Insert<br>
> <br>
> whos T<br>
> <br>
> after the line T=2 if you don't believe it.<br>
> <br>
> > ... "to" increments 0.1<br>
> > on each pass of the loop until it reaches 2.0. It works while (to<2).<br>
> > But it fails in the checking when to = T = 2.<br>
> > If I print T, I can see only the number "2"... No decimal point.<br>
> > I can not make T double even if I explicitly makes it double. But if I<br>
> > apply T = 2*(1+eps) then I can see now T= 2.0000 and the while loop<br>
> > terminates correctly...<br>
> <br>
> This depends on your definition of "correctly".<br>
> <br>
> Use<br>
> <br>
> format long<br>
> <br>
> to see the long value of any intermediate values (or display the values <br>
> of t02 to amplify the floating point remainder).<br>
> <br>
> As another respondent said, the "problem" is one of floating point <br>
> precision and the use of t+dt is the most erroraccumulating way one can <br>
> possibly find to compute a running total; if dt is under the exact value <br>
> by one bit, that one bit short will accumulate every iteration through. <br>
> OTOH, if you were to compute the time as t=n*dt, the error would <br>
> average out.<br>
> <br>
> Try the two following examples at the command line...<br>
> <br>
> x=0;for idx=1:10000,x=x+0.1;end, x1000<br>
> <br>
> and for comparison<br>
> <br>
> x=0;for idx=1:10001,x=(idx1)*0.1;end, x1000<br>
> <br>
> <br>
<br>
Hi dpb,<br>
<br>
THis was my confusion. When I printed T, I got T=2, and I printed to, I got 0.1000... then I incorrectly though that T was an integer. <br>
<br>
Now I understand my problem... the accumulation error in MATLAB. Next time I will me more careful when designing loops such that.<br>
<br>
Thanks Scott, dpb and Srivana...

Sun, 23 Oct 2011 15:38:05 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855960
dpb
On 10/23/2011 10:10 AM, Julio Gonzalez wrote:<br>
...<br>
<br>
> Now I understand my problem... the accumulation error in MATLAB....<br>
<br>
<br>
Again, no, No, NO, _NO_!!!! :(<br>
<br>
You apparently don't appreciate the basis of the problem at all. :(<br>
<br>
It has absolutely _nothing_ to do with Matlab but _everything_ to do <br>
with floating point representation.<br>
<br>
Read (thoroughly) Goldberg's exposition at<br>
<br>
<<a href="http://download.oracle.com/docs/cd/E1995701/8063568/ncg_goldberg.html">http://download.oracle.com/docs/cd/E1995701/8063568/ncg_goldberg.html</a>><br>
<br>


Sun, 23 Oct 2011 18:40:10 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855977
Julio Gonzalez
With all due respect, <br>
<br>
I do appreciate the nature of the problem, , I thought MATLAB will be more intelligent and will deal correctly with the answer. Other programming languages can handle this problem in much better way. I tried the same loop in other programming languages and all of them give the correct answer!<br>
<br>
Even a simple PERL script can handle the problem... IMHO, MATLAB could do a better job that PERL.. <br>
<br>
I understand MATLAB is an engineer tool and it is the engineer that must handle the issue.. <br>
<br>
Thanks for the link.. next time I will pay more attention to this problem while working with MATLAB! Thank you for shearing the information..<br>
<br>
dpb <none@non.net> wrote in message <j81cd0$8uc$1@speranza.aioe.org>...<br>
> On 10/23/2011 10:10 AM, Julio Gonzalez wrote:<br>
> ...<br>
> <br>
> > Now I understand my problem... the accumulation error in MATLAB....<br>
> <br>
> <br>
> Again, no, No, NO, _NO_!!!! :(<br>
> <br>
> You apparently don't appreciate the basis of the problem at all. :(<br>
> <br>
> It has absolutely _nothing_ to do with Matlab but _everything_ to do <br>
> with floating point representation.<br>
> <br>
> Read (thoroughly) Goldberg's exposition at<br>
> <br>
> <<a href="http://download.oracle.com/docs/cd/E1995701/8063568/ncg_goldberg.html">http://download.oracle.com/docs/cd/E1995701/8063568/ncg_goldberg.html</a>><br>
> <br>
> 

Sun, 23 Oct 2011 19:04:15 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855978
Roger Stafford
"Julio Gonzalez" <julio@parttimescientists.com> wrote in message <j81n2a$j0b$1@newscl01ah.mathworks.com>...<br>
> I do appreciate the nature of the problem, , I thought MATLAB will be more intelligent and will deal correctly with the answer. Other programming languages can handle this problem in much better way. I tried the same loop in other programming languages and all of them give the correct answer!<br>
> <br>
> Even a simple PERL script can handle the problem... IMHO, MATLAB could do a better job that PERL.. <br>
> <br>
> I understand MATLAB is an engineer tool and it is the engineer that must handle the issue.. <br>
> .........<br>
         <br>
Julio, if by "the problem" you mean that your 'to' variable doesn't end up an exact value of 2 after twenty iterations of "to = to+0.1", then you are quite wrong in singling out the matlab language as being responsible. Any programming system that uses binary floating point numbers will of necessity exhibit a similar behavior. It is inherent in the nature of binary representation of floating point numbers with a finite number of digits. You would do well to study up on the subject. Understanding these principles is fundamental to writing good mathematical programs.<br>
<br>
Roger Stafford

Sun, 23 Oct 2011 21:10:31 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#855980
Julio Gonzalez
"Roger Stafford" wrote in message <j81off$mrp$1@newscl01ah.mathworks.com>...<br>
> "Julio Gonzalez" <julio@parttimescientists.com> wrote in message <j81n2a$j0b$1@newscl01ah.mathworks.com>...<br>
> > I do appreciate the nature of the problem, , I thought MATLAB will be more intelligent and will deal correctly with the answer. Other programming languages can handle this problem in much better way. I tried the same loop in other programming languages and all of them give the correct answer!<br>
> > <br>
> > Even a simple PERL script can handle the problem... IMHO, MATLAB could do a better job that PERL.. <br>
> > <br>
> > I understand MATLAB is an engineer tool and it is the engineer that must handle the issue.. <br>
> > .........<br>
>          <br>
> Julio, if by "the problem" you mean that your 'to' variable doesn't end up an exact value of 2 after twenty iterations of "to = to+0.1", then you are quite wrong in singling out the matlab language as being responsible. Any programming system that uses binary floating point numbers will of necessity exhibit a similar behavior. It is inherent in the nature of binary representation of floating point numbers with a finite number of digits. You would do well to study up on the subject. Understanding these principles is fundamental to writing good mathematical programs.<br>
> <br>
> Roger Stafford<br>
<br>
Hi Roger,<br>
<br>
You are right. I will. Precisely I was doing some tests in C++ and it has the same behaviour. I will take a look to the webpage dbd sent to me and will be more careful to consider the errors introduced by the roundoff.<br>
<br>
Thanks to all.

Mon, 24 Oct 2011 03:22:14 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#856003
Steven_Lord
<br>
<br>
"Julio Gonzalez" <julio@parttimescientists.com> wrote in message <br>
news:j81n2a$j0b$1@newscl01ah.mathworks.com...<br>
> With all due respect,<br>
> I do appreciate the nature of the problem, , I thought MATLAB will be more <br>
> intelligent and will deal correctly with the answer.<br>
<br>
It does. It returns the correct answer using standard IEEE double precision <br>
floatingpoint arithmetic.<br>
<br>
<a href="http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf">http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf</a><br>
<br>
> Other programming languages can handle this problem in much better way. I <br>
> tried the same loop in other programming languages and all of them give <br>
> the correct answer!<br>
<br>
If they're using some form of arbitrary precision arithmetic (for example) <br>
they could add (exactly) 1/10 twenty times and that would give (exactly) 2. <br>
That's fine.<br>
<br>
Now try writing that exact same loop in another language that uses IEEE <br>
arithmetic, like C or C++ using the double data type. The results should <br>
agree with those returned by MATLAB.<br>
<br>
Welcome to the world of floatingpoint arithmetic.<br>
<br>
*snip*<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Tue, 25 Oct 2011 09:44:13 +0000
Re: While loop fails
http://www.mathworks.com/matlabcentral/newsreader/view_thread/313610#856181
Bill Whiten
"Julio Gonzalez" <julio@parttimescientists.com> wrote in message <j7sh2e$q77$1@newscl01ah.mathworks.com>...<br>
> 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:<br>
> <br>
> Ti=2<br>
> T = double(T);<br>
> to= 0.0000;<br>
> <br>
> while (T<=to) <br>
> to = to+0.1;<br>
> %some code here.....<br>
> end<br>
> <br>
> the loop end at to=2.0000; it should end at 2.1000<br>
> <br>
> Is there any other way to fix this problems besides T=2*(1+eps)?<br>
> <br>
> Regards<br>
> <br>
> Julio<br>
> University of Liverpool<br>
<br>
T=2;<br>
s=0.1;<br>
n=round(T/s)<br>
for i=1:n<br>
to=T*i/n;<br>
. . .