필터 지우기
필터 지우기

issue about ismember wrong output

조회 수: 3 (최근 30일)
jason lee
jason lee 2020년 3월 24일
편집: Stephen23 2020년 3월 24일
a=0.02:0.02:2;
b=0:0.01:5;
ismember(a(6),b)
ans =
logical
0
But it is clear that all elements in a belong to b, so where is the problem?

채택된 답변

Walter Roberson
Walter Roberson 2020년 3월 24일
You forgot that in binary floating point representation there is no exact equivalent to 0.1 or 0.01 or 0.02, only approximations of those. When you add up those approximations of 0.01 you are not necessarily going to get exactly an approximation of 0.02 especially since you start the 0.02 accumulation at a different start point.
Consider the analogy in decimal of 1/3 to two decimal places. 1/3 decimal is 0.33333333 with the 3 infinitely repeated. To two decimals, 0.33. Now add another of the same to that and you get 0.66. Is that the same as the 2 decimal approximation of 2/3? No, the 2 decimal approximation of 2/3 is 0.67. This illustrates that when you add up truncated approximations that you do not necessarily get the same as the direct value.
The moral of the story is to avoid exact comparisons in floating point. See ismembertol
  댓글 수: 3
Stephen23
Stephen23 2020년 3월 24일
편집: Stephen23 2020년 3월 24일
"but unfortunately, 0.9999999 is a LITTLE smaller than 1."
According to standard mathematics, 0.999... is equal to 1 (different representations of exactly the same number is very common in mathematics).
Walter Roberson
Walter Roberson 2020년 3월 24일
The .9 repeated == 1 is however only true for infinite precision. If you were to stop at 10^43 decimal places, then you would indeed have a value mathematically less than one.
Physically, if you were to stop at 1e-43 metres, it is unclear what the result would be. There have been serious proposals that space as we know it does not exist below about that small, that everything is a quantum foam.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 C Matrix API에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by