Problem with find or floating point issue
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lets say we have this problem,
a=-1:0.01:1;
b=-0.9:0.01:1.1;
find(a==b)
ans = 1×0 empty double row vector
Mathematically, -0.9 should lie in sequence a. I figured it out that it is something floating point issue but is it not weired that find command for this simple problem can not work? Is there some solution to work this out?
3 Kommentare
Antworten (2)
Peng Li
am 13 Apr. 2020
There is always some tricky things with floating precision. Usually it solves thing if you make the comparison by determining if the difference is smaller than a small enough value eps.
Try
find(abs(a-b) < eps)
0 Kommentare
Walter Roberson
am 14 Apr. 2020
a=-1:0.01:1;
b=-0.9:0.01:1.1;
That creates two vectors, the second one of which is algebraically 0.1 larger than the first at any given point. Because 0.01 cannot be exactly represented in binary floating point, and those errors accumulate when you use the colon operator, there will be places where the difference is not exactly the same as in other places:
>> num2str(unique(b.'-a.'), '%-.100g')
ans =
4×56 char array
'0.0999999999999998667732370449812151491641998291015625 '
'0.09999999999999997779553950749686919152736663818359375 '
'0.100000000000000033306690738754696212708950042724609375'
'0.100000000000000088817841970012523233890533447265625 '
Now, remember that a==b between two vectors of the same size compares each element to each corresponding element, so a(1) would be compared to b(1), a(2) to b(2) and so on. But we know that each b is approximately 0.1 larger than the corresponding a, so there will not be even a single match between corresponding elements.
Perhaps you want
>> num2str(a(ismember(a,b)).','%-.100g')
ans =
98×56 char array
'-0.90000000000000002220446049250313080847263336181640625'
'-0.89000000000000001332267629550187848508358001708984375'
'-0.88000000000000000444089209850062616169452667236328125'
'-0.86999999999999999555910790149937383830547332763671875'
'-0.85999999999999998667732370449812151491641998291015625'
'-0.84999999999999997779553950749686919152736663818359375'
'-0.8200000000000000621724893790087662637233734130859375 '
'-0.810000000000000053290705182007513940334320068359375 '
'-0.8000000000000000444089209850062616169452667236328125 '
'-0.79000000000000003552713678800500929355621337890625 '
'-0.7800000000000000266453525910037569701671600341796875 '
'-0.770000000000000017763568394002504646778106689453125 '
'-0.7600000000000000088817841970012523233890533447265625 '
'-0.75 '
'-0.7399999999999999911182158029987476766109466552734375 '
'-0.729999999999999982236431605997495353221893310546875 '
'-0.7199999999999999733546474089962430298328399658203125 '
'-0.70999999999999996447286321199499070644378662109375 '
'-0.6999999999999999555910790149937383830547332763671875 '
'-0.64000000000000001332267629550187848508358001708984375'
'-0.63000000000000000444089209850062616169452667236328125'
'-0.61999999999999999555910790149937383830547332763671875'
'-0.5800000000000000710542735760100185871124267578125 '
'-0.5700000000000000621724893790087662637233734130859375 '
'-0.560000000000000053290705182007513940334320068359375 '
'-0.5500000000000000444089209850062616169452667236328125 '
'-0.54000000000000003552713678800500929355621337890625 '
'-0.5300000000000000266453525910037569701671600341796875 '
'-0.520000000000000017763568394002504646778106689453125 '
'-0.5100000000000000088817841970012523233890533447265625 '
'-0.5 '
'-0.4899999999999999911182158029987476766109466552734375 '
'-0.42000000000000003996802888650563545525074005126953125'
'-0.41000000000000003108624468950438313186168670654296875'
'-0.40000000000000002220446049250313080847263336181640625'
'-0.39000000000000001332267629550187848508358001708984375'
'-0.38000000000000000444089209850062616169452667236328125'
'-0.36999999999999999555910790149937383830547332763671875'
'-0.35999999999999998667732370449812151491641998291015625'
'-0.34999999999999997779553950749686919152736663818359375'
'-0.33999999999999996891375531049561686813831329345703125'
'-0.32999999999999996003197111349436454474925994873046875'
'-0.29000000000000003552713678800500929355621337890625 '
'-0.2800000000000000266453525910037569701671600341796875 '
'-0.270000000000000017763568394002504646778106689453125 '
'-0.2600000000000000088817841970012523233890533447265625 '
'-0.25 '
'-0.2399999999999999911182158029987476766109466552734375 '
'-0.229999999999999982236431605997495353221893310546875 '
'-0.2199999999999999733546474089962430298328399658203125 '
'-0.20999999999999996447286321199499070644378662109375 '
'-0.1999999999999999555910790149937383830547332763671875 '
'-0.16000000000000003108624468950438313186168670654296875'
'-0.15000000000000002220446049250313080847263336181640625'
'-0.14000000000000001332267629550187848508358001708984375'
'-0.13000000000000000444089209850062616169452667236328125'
'-0.11999999999999999555910790149937383830547332763671875'
'-0.10999999999999998667732370449812151491641998291015625'
'-0.09999999999999997779553950749686919152736663818359375'
'-0.08999999999999996891375531049561686813831329345703125'
'-0.07999999999999996003197111349436454474925994873046875'
'-0.06999999999999995115018691649311222136020660400390625'
'-0.04000000000000003552713678800500929355621337890625 '
'-0.0300000000000000266453525910037569701671600341796875 '
'-0.020000000000000017763568394002504646778106689453125 '
'-0.0100000000000000088817841970012523233890533447265625 '
'0 '
'0.0100000000000000088817841970012523233890533447265625 '
'0.020000000000000017763568394002504646778106689453125 '
'0.0300000000000000266453525910037569701671600341796875 '
'0.04000000000000003552713678800500929355621337890625 '
'0.05999999999999994226840271949185989797115325927734375 '
'0.06999999999999995115018691649311222136020660400390625 '
'0.07999999999999996003197111349436454474925994873046875 '
'0.08999999999999996891375531049561686813831329345703125 '
'0.15000000000000002220446049250313080847263336181640625 '
'0.16000000000000003108624468950438313186168670654296875 '
'0.270000000000000017763568394002504646778106689453125 '
'0.2800000000000000266453525910037569701671600341796875 '
'0.29000000000000003552713678800500929355621337890625 '
'0.40000000000000002220446049250313080847263336181640625 '
'0.41000000000000003108624468950438313186168670654296875 '
'0.42000000000000003996802888650563545525074005126953125 '
'0.5300000000000000266453525910037569701671600341796875 '
'0.54000000000000003552713678800500929355621337890625 '
'0.5500000000000000444089209850062616169452667236328125 '
'0.560000000000000053290705182007513940334320068359375 '
'0.5700000000000000621724893790087662637233734130859375 '
'0.5800000000000000710542735760100185871124267578125 '
'0.75 '
'0.7600000000000000088817841970012523233890533447265625 '
'0.770000000000000017763568394002504646778106689453125 '
'0.7800000000000000266453525910037569701671600341796875 '
'0.79000000000000003552713678800500929355621337890625 '
'0.8000000000000000444089209850062616169452667236328125 '
'0.810000000000000053290705182007513940334320068359375 '
'0.8200000000000000621724893790087662637233734130859375 '
'1 '
As for values that do not match, see for example,
>> [d,idx] = min(abs(b-0.5))
d =
1.11022302462516e-16
idx =
141
>> fprintf('%.99g\n', b(idx))
0.50000000000000011102230246251565404236316680908203125
0 Kommentare
Siehe auch
Kategorien
Mehr zu Error Detection and Correction 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!