Path: news.mathworks.com!not-for-mail
From: "Mehdi bahonar" <mehdiuoc@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: A simple but confusing bug in while loop_Please help
Date: Mon, 8 Mar 2010 01:29:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 24
Message-ID: <hn1jt2$71c$1@fred.mathworks.com>
Reply-To: "Mehdi bahonar" <mehdiuoc@yahoo.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1268011746 7212 172.30.248.38 (8 Mar 2010 01:29:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 8 Mar 2010 01:29:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2254054
Xref: news.mathworks.com comp.soft-sys.matlab:614739

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