Unexpected result from linspace function
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Why the result of this code
B1 = 0:0.1:1.8;
B2 = linspace(0,1.8,19)
B1(4)-0.3
B2(4)-0.3
is 5.5511e-17? I found a workaround, which is
B1 = 0:01:18;
B1 = B1/10;
But I am still wondering why the first code does not produce what it should?
채택된 답변
Rik
2018년 8월 15일
2 개 추천
Welcome to the wondrous world of floating point numbers. Matlab is a computer program, so it works with binary. Some values are impossible to represent in a binary expansion, so they get rounded. This can be different for each algorithm, so even if the end result can be represented, rounding errors can build up. That is the use case for the eps function: it shows you what the magnitude is of possible errors.
As a decimal example, consider this: (1/3)*3. Assuming I can only store 2 decimal digits, that would be 0.33*3=0.99, even if the correct answer doesn't need 2 decimals.
댓글 수: 9
Rostislav Teryaev
2018년 8월 15일
편집: Rostislav Teryaev
2018년 8월 15일
But this issue can be overcomed by using another algorithm inside 0:0.1:1.8 notation. Why this has not been already implemented? It seems to be obvious. Just like this:
array = zeros(1,floor(1.8/0.1))
for i = 1:length(array)+1
array(i) = (i-1)*0.1;
end
Rostislav Teryaev
2018년 8월 15일
편집: Rostislav Teryaev
2018년 8월 15일
hm, I just tried my code and it seems that algorithm inside a:step:b implemented in the same way, because I also got 5.5511e-17(
Rik
2018년 8월 15일
It isn't a problem with the minus function either, as array(4)==0.3 returns false. That is the reason why you shouldn't use equality in such cases, but abs(array(4)-0.3)<eps (or use functions that do this internally, like ismembertol and uniquetol).
"But this issue can be overcomed by using another algorithm inside 0:0.1:1.8 notation. Why this has not been already implemented? It seems to be obvious. Just like this:"
No, that does not overcome the "issue" at all. The first thing is to understand that 0.1 cannot be stored exactly by binary floating point numbers, in exactly the same way that you cannot write 1/3 as a finite decimal. Also understand that different calculations can produce different floating point error.
Your "algorithm" does not solve anything, because the floating point error will always be there. And no matter what way you calculate those values, someone will compare those values with the results of a different calculation that results in a different floating point error and so the comparison will fail in exactly the same way. You have not solved anything.
The actual solution is to compare the absolute value against a tolerance:
abs(A-B)<tol
In any case, this has all been discussed in lots of detail before. Start by reading these:
This is worth reading as well:
Thank you!
Is there any work around to deal with floating point arithmetic problems in matlab ?
Rik
2023년 12월 8일
@VIGNESH BALAJI what did you have in mind? This is a fundametal side effect of floats. So only not using a float can avoid this problem.
Stephen23
2023년 12월 9일
"Is there any work around to deal with floating point arithmetic problems in matlab ?"
The problem is not "in MATLAB", it is simply how your computer stores floating point numbers. MATLAB runs on your computer: if there was a simple "work around" then everyone would use it, not just MATLAB.
You can certainly use symbolic or high precision classes, but then your code will be much slower.
"Is there any workaround to deal with floating point arithmetic problems?"
@VIGNESH BALAJI - There, I fixed the question for you.
Certainly, one option is to use a computer that runs on a decimal system. There might be a lot of complications, though.
As long as you use computers that use the binary system, you will encounter floating point errors.
Rik and Stephen have already provided suggestions as to what you can do as workarounds, I'll just reiterate them -
You can avoid using floats.
Well, what to use then? Symbolic numbers or high precision classes, which work at the cost of the code performance.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
