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.
댓글 수: 0
답변 (1개)
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
Walter Roberson
2013년 10월 19일
realmin() and realmax() pretty much return constants based on knowledge of the binary representation.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!