why has the command window stopped working

2 views (last 30 days)
sky
sky on 19 Oct 2013
Commented: Walter Roberson on 19 Oct 2013
i am new to matlab, so i can't figure out what's wrong.. i've been trying to write a code to find the largest number matlab can hold and i wrote this code in the script:
////////
n=1;
while n>0
n=n+1;
end
n
///////
but after running it everything stops working and other codes won't run neither. i know it seems silly to say so, but the '>>' sign in command window is gone after this while.

Answers (1)

Walter Roberson
Walter Roberson on 19 Oct 2013
When you command
n=1
then you are creating n as a binary floating point number. Floating point numbers have different spacings between adjacent representable numbers, getting more dense towards 0 and less dense towards the largest representable number. As you keep adding 1, you get to the point where n is not representable distinctly from n+1 . At that point your code is not going to make any progress.
Adding 1 until a number goes negative is something that is done in binary integers, and only in systems that are defined to overflow from the largest representable integer to the most negative representable integer. MATLAB does have binary integers available, but they are defined to "saturate" instead of overflowing. For example for 8 bit signed integers, the value after +127 is not -128 as would often be the case: instead it stays at +127 -- the same way that adding 1 to infinity stays at infinity instead of overflowing to negative infinity.
  2 Comments
sky
sky on 19 Oct 2013
thank you. then how do 'realmin' and 'realmax' work in matlab?
Walter Roberson
Walter Roberson on 19 Oct 2013
realmin() and realmax() pretty much return constants based on knowledge of the binary representation.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!