Floating point error incrementing in while loop

조회 수: 9 (최근 30일)
Garrick Bruening
Garrick Bruening 2019년 7월 23일
답변: dpb 2019년 7월 23일
Noticed an odd issue while creating a while loop and incrementing the value in it.
Basically I'm iterating through time, and it seems at some points it will add an arbitrarly small number to the time increment. Simple work around is to just floor it to the nearest 0.005, but wondering if there's a specific reason this is happening.
Code below reproduces.
time = 0;
while time<100
time = time + 0.01;
if mod(time,.005)~=0
fprintf('Time added: \n');
mod(time,.005)
break
end
end
Running 2018b, on windows 10. Tested on another machine with 2019b and same result.

채택된 답변

dpb
dpb 2019년 7월 23일

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by