채택된 답변

Torsten
Torsten 2022년 6월 14일

0 개 추천

a=0;
t=1;
while t==1
a=a+0.01;
if a >= 0.5
t=0;
end
end
a
a = 0.5000
t
t = 0
%why it only break loop when a=a+0.1

댓글 수: 3

Allen Wei
Allen Wei 2022년 6월 14일
i mean when i type a=a+0.1
t will become 0 and the loop breaks
but when a=a+0.01 loop can't break
is my matlab bugged or what happened?
a will not exactly become 0.5 because of floating point arithmetic. That's why statements as
if a == 0.5
are dangerous because they are almost never true.
Use
if a >= 0.5
to leave the while-loop as I did above.
Allen Wei
Allen Wei 2022년 6월 15일
i see
thank you so much

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

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 6월 14일

0 개 추천

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2022년 6월 14일

댓글:

2022년 6월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by