필터 지우기
필터 지우기

test for equality not working 8.0.0.783 (R2012b)

조회 수: 1 (최근 30일)
Hassan F
Hassan F 2013년 7월 30일
I encountered a weird issue when I was trying for test of equality in two array (eq function). I tried to reproduced it as below. Does anybody know what is going on? I have searched for it online but couldn't find a proper solution. I know it is a precision problem, but for test of equality one thinks it should at least work properly without worrying about these type of issues. My OS is OSX 10.8. A colleague of mine reproduced it in MATLAB 2011a (OS: Linux) as well.
>> A = 0.05:0.05:0.3
A =
0.050000000000000 0.100000000000000 0.150000000000000 0.200000000000000 0.250000000000000 0.300000000000000
>> A==0.15
ans =
0 0 0 0 0 0
>> A==(0.05+0.1)
ans =
0 0 1 0 0 0
>> (A(3)-0.15)*1e16
ans =
0.277555756156289
UPDATE (unique function is not working either):
>> unique([A, 0.15])
ans =
0.050000000000000 0.100000000000000 0.150000000000000 0.150000000000000 0.200000000000000 0.250000000000000 0.300000000000000

채택된 답변

Wayne King
Wayne King 2013년 7월 30일
편집: Wayne King 2013년 7월 30일
This is the well-known and often responded to in this forum (and others) problem of trying to compare floating point numbers.
This is not unique to MATLAB
In R, the same thing happens:
> x <-seq(0.05,0.3,by = 0.05);
> x == 0.15
[1] FALSE FALSE FALSE FALSE FALSE FALSE
In Python too:
>>> import numpy as np
>>> test = np.linspace(0.05,.3,6);
>>> test == 0.15
array([False, False, False, False, False, False], dtype=bool)
  댓글 수: 2
Hassan F
Hassan F 2013년 7월 30일
Indeed. I tried it in R as well and it doesn't work only for 0.15. It works ,however, for other array elements (0.05, 0.1, 0.2, 0.25, 0.3). When it comes to floating point comparison, I think I should give up and use exact comparison. But I thought maybe there is a way around it that I am not aware of.
Wayne King
Wayne King 2013년 7월 30일
편집: Wayne King 2013년 7월 30일
yes, use a tolerance.
abs((x-0.15))< tol
For example:
tol = 1e-10;
abs((A-0.15)) < tol

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

추가 답변 (1개)

Iain
Iain 2013년 7월 30일
Actually, for the test of equality, you should check for exact equality, and you should control the tolerance you are willing to accept.
cmp = abs(A(3)-0.15) < 5*eps(A(3))
Unique does what it says on the tin - it DOES work.
  댓글 수: 4
Matt Kindig
Matt Kindig 2013년 7월 30일
@Hassan,
Try this. At the command line, type:
format hex
And now inspect A and 0.15, e.g.
>> A = 0.05:0.05:0.3
A =
3fa999999999999a 3fb999999999999a 3fc3333333333334 3fc9999999999999 3fd0000000000000 3fd3333333333333
>> 0.15
ans =
3fc3333333333333
This formatting displays the hexadecimal representation of each number, which is how Matlab internally stores each distinct value. Notably, since there is a finite number of bits that can be stored for each number (64 for a double), each number can be separated by another number by no less than one hexadecimal digit (which corresponds to one computer bit). Note that the hex representation of 0.15, which is 3fc3333333333333, is NOT contained exactly in the hex representation of A-- the last digit '3' does not match. This is why unique() is picking up both--internally, Matlab considers them distinct values.
Hassan F
Hassan F 2013년 7월 31일
@Iain: Thanks that can indeed solve the issue.
@Matt: It is interesting to see it in this format. So do you what is it that only 0.2 and 0.15 are stored differently? When I try 0.05:0.05:0.15 I get different result than what I get for 0.05:0.05:0.3. I mean the value for 0.15 is stored differently. Why is it like this?
>> 0.05:0.05:0.15
ans =
3fa999999999999a 3fb999999999999a 3fc3333333333333
>> 0.15
ans =
3fc3333333333333
the value for 0.15 is same in this case, but not in my initial question.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by