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:
help for while loop

Subject: help for while loop

From: Xing Li

Date: 30 Dec, 2012 21:14:08

Message: 1 of 4

I have a very simple while loop, which was supposed to break right away until n = 10. But this loop never break because n = 10.1 in the end. How could this happened?

n = 0;
flag = 0;
while (flag == 0)
if( n < 10)
    n = n + 0.1;
   if(n == 10)
    flag = 1;
   end
end
end

Subject: help for while loop

From: Roger Stafford

Date: 30 Dec, 2012 21:51:07

Message: 2 of 4

"Xing Li" <xli71@syr.edu> wrote in message <kbqar0$7co$1@newscl01ah.mathworks.com>...
> I have a very simple while loop, which was supposed to break right away until n = 10. But this loop never break because n = 10.1 in the end. How could this happened?
>
> n = 0;
> flag = 0;
> while (flag == 0)
> if( n < 10)
> n = n + 0.1;
> if(n == 10)
> flag = 1;
> end
> end
> end
- - - - - - - - - - -

  The reason is that your computer is using binary floating point to represent its numbers and can't get an exact expression for 0.1 . Consequently when you add it a hundred times you may not get an exact sum of 10 because of that tiny error. In such a situation you shouldn't be using the "==" operator. Use some other counting technique or allow some some tolerance for small errors.

Roger Stafford

Subject: help for while loop

From: Nasser M. Abbasi

Date: 30 Dec, 2012 23:24:26

Message: 3 of 4

On 12/30/2012 3:14 PM, Xing Li wrote:
> I have a very simple while loop, which was supposed to break right away until n = 10.
>But this loop never break because n = 10.1 in the end. How could this happened?
>
> n = 0;
> flag = 0;
> while (flag == 0)
> if( n < 10)
> n = n + 0.1;
> if(n == 10)
> flag = 1;
> end
> end
> end
>


this works

---------------
n = 0;
flag = 0;

while flag == 0
     if (10-(n+0.1))>realmin('double')
         n = n + 0.1;
         if abs(10-n)<realmin('double')
            flag = 1;
         end
     else
         flag = 1;
     end
end
---------------------

EDU>> n

    10.0000

EDU>> num2strexact(n)

9.9999999999999804600747665972448885440826416015625

--Nasser

Subject: help for while loop

From: Steven_Lord

Date: 31 Dec, 2012 03:46:12

Message: 4 of 4



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:kbqd0b$e6t$1@newscl01ah.mathworks.com...
> "Xing Li" <xli71@syr.edu> wrote in message
> <kbqar0$7co$1@newscl01ah.mathworks.com>...
>> I have a very simple while loop, which was supposed to break right away
>> until n = 10. But this loop never break because n = 10.1 in the end. How
>> could this happened? n = 0;
>> flag = 0;
>> while (flag == 0)
>> if( n < 10)
>> n = n + 0.1;
>> if(n == 10)
>> flag = 1;
>> end
>> end
>> end
> - - - - - - - - - - -
>
> The reason is that your computer is using binary floating point to
> represent its numbers and can't get an exact expression for 0.1 .
> Consequently when you add it a hundred times you may not get an exact sum
> of 10 because of that tiny error. In such a situation you shouldn't be
> using the "==" operator. Use some other counting technique or allow some
> some tolerance for small errors.

Or scale.

tenN = 0;
flag = 0;
while ~flag
    if tenN < 100
        tenN = tenN + 1;
        if tenN == 100
            flag = 1;
        end
    end
end

If you need to do something with n in the WHILE loop, n is tenN/10 (although
unless tenN is a multiple of 5 this too will not be exactly representable.)

Of course there are other ways to implement this in an IMO more
straightforward manner (FOR loop, for example) but since this feels like an
exercise for an introductory MATLAB or programming class, perhaps its
purpose was to teach WHILE loops.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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