test for equality not working 8.0.0.783 (R2012b)
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Wayne King
am 30 Jul. 2013
Bearbeitet: Wayne King
am 30 Jul. 2013
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 Kommentare
Wayne King
am 30 Jul. 2013
Bearbeitet: Wayne King
am 30 Jul. 2013
yes, use a tolerance.
abs((x-0.15))< tol
For example:
tol = 1e-10;
abs((A-0.15)) < tol
Weitere Antworten (1)
Iain
am 30 Jul. 2013
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 Kommentare
Matt Kindig
am 30 Jul. 2013
@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.
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!