Infinite loop when decimals are added

조회 수: 1 (최근 30일)
Leonel Sternberg
Leonel Sternberg 2022년 7월 30일
이동: Walter Roberson 2023년 1월 22일
I am making a simple program to calculate the number of times a decimal number has to be multiplied (by positive integers) so that the decimal part (mod1) will = 0.
Example of the code:
%Decimal
ro=0.5854;
n=1;
while mod(ro*n,1)~=0
n=n+1;
end
n
In this case the answer is 5000.
I want to be able to change the last digit of the decimal point. But I am having a problem when I make a small change in the program. I get an infinite loop. The changed program is shown below:
%Decimal
ro=0.585+0.0004;
n=1;
while mod(ro*n,1)~=0
n=n+1;
end
n
I can't understand why I get an infinite loop with this small change - the value of ro changed acordingly but I get an infinite "while" loop. Please explain.

채택된 답변

Stephen23
Stephen23 2022년 7월 30일
이동: Walter Roberson 2023년 1월 22일
  댓글 수: 1
Leonel Sternberg
Leonel Sternberg 2022년 7월 30일
이동: Walter Roberson 2023년 1월 22일
Thanks ..This helps

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by