why has the command window stopped working

3 次查看(过去 30 天)
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.

回答(1 个)

Walter Roberson
Walter Roberson 2013-10-19
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 个评论
sky
sky 2013-10-19
thank you. then how do 'realmin' and 'realmax' work in matlab?
Walter Roberson
Walter Roberson 2013-10-19
realmin() and realmax() pretty much return constants based on knowledge of the binary representation.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by