Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
A simple but confusing bug in while loop_Please help

Subject: A simple but confusing bug in while loop_Please help

From: Mehdi bahonar

Date: 8 Mar, 2010 01:29:06

Message: 1 of 3

Hi Everyone,
I designed below loop somehow that after running it, the time should show endtime (i.e. time=endtime). The output of below code is time=2 which was my desire and correct.

time=0;
ts=0.1;
endtime=2;
while time<endtime
        time=time+ts;
end
time

However if I change the endtime to 1 (i.e. endtime=1), the output result will be "time=1.1" which is not correct (I expected to get time=1). Same thing will happen if I use "endtime=5" and probably other numbers!!!

time=0;
ts=0.1;
endtime=1;
while time<endtime
        time=time+ts;
end
time

Would you please tell me what is wrong with the code and how can I fix it?
Many Thanks,
Mehdi

Subject: A simple but confusing bug in while loop_Please help

From: Sadik

Date: 8 Mar, 2010 01:41:05

Message: 2 of 3

Dear Mehdi,

Please see the following post for the cause and the remedy:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/275625

Best.


"Mehdi bahonar" <mehdiuoc@yahoo.com> wrote in message <hn1jt2$71c$1@fred.mathworks.com>...
> Hi Everyone,
> I designed below loop somehow that after running it, the time should show endtime (i.e. time=endtime). The output of below code is time=2 which was my desire and correct.
>
> time=0;
> ts=0.1;
> endtime=2;
> while time<endtime
> time=time+ts;
> end
> time
>
> However if I change the endtime to 1 (i.e. endtime=1), the output result will be "time=1.1" which is not correct (I expected to get time=1). Same thing will happen if I use "endtime=5" and probably other numbers!!!
>
> time=0;
> ts=0.1;
> endtime=1;
> while time<endtime
> time=time+ts;
> end
> time
>
> Would you please tell me what is wrong with the code and how can I fix it?
> Many Thanks,
> Mehdi

Subject: A simple but confusing bug in while loop_Please help

From: Mehdi bahonar

Date: 8 Mar, 2010 02:18:05

Message: 3 of 3

Very Helpful link.
Thank you very much




"Mehdi bahonar" <mehdiuoc@yahoo.com> wrote in message <hn1jt2$71c$1@fred.mathworks.com>...
> Hi Everyone,
> I designed below loop somehow that after running it, the time should show endtime (i.e. time=endtime). The output of below code is time=2 which was my desire and correct.
>
> time=0;
> ts=0.1;
> endtime=2;
> while time<endtime
> time=time+ts;
> end
> time
>
> However if I change the endtime to 1 (i.e. endtime=1), the output result will be "time=1.1" which is not correct (I expected to get time=1). Same thing will happen if I use "endtime=5" and probably other numbers!!!
>
> time=0;
> ts=0.1;
> endtime=1;
> while time<endtime
> time=time+ts;
> end
> time
>
> Would you please tell me what is wrong with the code and how can I fix it?
> Many Thanks,
> Mehdi

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us