precision problem in simple subtraction?!?

조회 수: 7 (최근 30일)
Stefan
Stefan 2015년 5월 29일
편집: John D'Errico 2015년 5월 29일
Hi everybody Just stumbled upon a "bug" i cannot really explain. Let's test a simple subtraction: 1 - 0.8 - 0.2 my matlab tells me -5.5511e-17 If I switch the numbers, 1 - 0.2 - 0.8, it correctly results in 0. Strange enough, the result is smaller than eps (2-2204e-16 in my case), so why is it not 0? In my case such a calculation inside a log function results in complex results, due to the negative sign, leading to various errors if I try to e.g. plot the data! What's happening here? I tried it in different matlab instances, different PCs, I even tried it on a mac! A similar phenomenons can be observed calculating e.g. 1 - 0.7 -0.3 Same thing happens if the subtraction is performed with 3 single variables resulting in an error of -1.4901e-08.
  댓글 수: 2
Stefan
Stefan 2015년 5월 29일
even worse: abs(100-99.9-0.1)>eps is true, so the precision error for this particular calculation is larger than eps
John D'Errico
John D'Errico 2015년 5월 29일
편집: John D'Errico 2015년 5월 29일
But that just shows you don't understand eps either.
abs(100-99.9-0.1)>eps(100)
ans =
0
If you will use floating point arithmetic, it is a good idea to understand it.

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 5월 29일

추가 답변 (1개)

Jos (10584)
Jos (10584) 2015년 5월 29일
편집: Jos (10584) 2015년 5월 29일
Simple for you, but not for a computer! Using only binary representations and a limited memory system a computer cannot store numbers like 0.99 exactly without special tricks and therefore makes small but significant round-off errors. And this makes the order of operations also important
x = 0.99 % simple number!
sprintf('%.30f', x) % see how it is stored inside your computer
% mathematical identical operations
a = 1-x-0.01
b = (x+0.01)-1
sprintf('%.30f', a)
sprintf('%.30f', b)
  댓글 수: 2
James Tursa
James Tursa 2015년 5월 29일
Note that the sprintf('%.30f', x) trick may not work on Windows. You may have to use this FEX submission instead:
Jos (10584)
Jos (10584) 2015년 5월 29일
Ah, Windows, software from the good old days ;-)

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by