Numerical issue in simple summation/addition

조회 수: 1 (최근 30일)
Ali
Ali 2016년 1월 23일
댓글: Image Analyst 2016년 1월 23일
below code prints 1. But if you have b = 0.2 at the second line, it does not print 1.
a = 0;
b = 0.1;
c = a;
while c < 1
c
c = c+b;
end

채택된 답변

Star Strider
Star Strider 2016년 1월 23일

추가 답변 (2개)

Image Analyst
Image Analyst 2016년 1월 23일
It has nothing to do with the version. It has everything to do with digitization/quantization differences as discussed in the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Ali
Ali 2016년 1월 23일
편집: Ali 2016년 1월 23일
So, what is the fix? how can i have such a while loop with 0.1 increments and stop before 1?
  댓글 수: 4
Star Strider
Star Strider 2016년 1월 23일
My pleasure.
Image Analyst
Image Analyst 2016년 1월 23일
"What's the fix?" Did you read the FAQ like both Star and I pointed you to? In there is the "fix" or way to deal with the reality of it -- you check against a tolerance.

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

카테고리

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