1.1 + 0.1 == 1.2 returns false

조회 수: 5 (최근 30일)
N/A
N/A 2014년 1월 13일
댓글: James Tursa 2014년 1월 31일
Very strange.
Apparently this is related to the precision of the double type as
1.1 + 0.1 - 1.2 = 2.2204e-16
What can I do to counter this behavior? Thanks!
  댓글 수: 1
James Tursa
James Tursa 2014년 1월 31일
As a learning aid you can use this FEX submission to see the exact numbers involved:
E.g., for your example,
>> num2strexact(1.1)
ans =
1.100000000000000088817841970012523233890533447265625
>> num2strexact(0.1)
ans =
0.1000000000000000055511151231257827021181583404541015625
>> num2strexact(1.1+0.1)
ans =
1.20000000000000017763568394002504646778106689453125
>> num2strexact(1.2)
ans =
1.1999999999999999555910790149937383830547332763671875

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

채택된 답변

Image Analyst
Image Analyst 2014년 1월 13일
편집: Image Analyst 2014년 1월 13일
See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F It has strategies to deal with this situation, such as comparing the value to a tolerance.
% instead of a == b
% use:
areEssentiallyEqual = abs(a-b) < tol
% for some small value of tol relative to a and b
% perhaps defined using eps(a) and/or eps(b)
  댓글 수: 2
N/A
N/A 2014년 1월 30일
Interesting, Surprising that there isn't some native function that takes care of this. Thanks!
Roger Stafford
Roger Stafford 2014년 1월 31일
편집: Roger Stafford 2014년 1월 31일
In both matlab and on my calculator these quantities come out unequal:
(sqrt(2))^2 ~= 2
(3/14+15/14)+3/14 ~= 1.5
The reason is quite clear. The quantities on the left hand side involve fractional values that cannot be represented exactly either on matlab's computer or on a decimal calculator. But when it comes to
11/10+1/10 and 12/10,
as in the case you mentioned, the calculator has them exactly equal but matlab doesn't. Why is that? It is because the calculator is using decimal digits and the computer is using binary digits - even though matlab normally displays numbers in decimal form, it is actually performing the computation with numbers in binary form. There is no way matlab could be programmed to obtain an exact equality in all such cases when division by something other than powers of 2 are involved. For that reason you cannot expect exact equality where, for example, division by 10 is involved. This is the binary computing world you have entered.

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 1월 13일
  댓글 수: 1
N/A
N/A 2014년 1월 30일
Thanks Azzi.

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


Mischa Kim
Mischa Kim 2014년 1월 13일
편집: Mischa Kim 2014년 1월 13일
MATLAB is a numerical software tool, not an algebraic one. What you are seeing there is the numerical accuracy measure called eps. In other words, this is as accurate as your results will get.
  댓글 수: 1
N/A
N/A 2014년 1월 30일
Thanks Mischa.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by