I have this code which is behaving differently for decimal and integer values. Where am I missing??
Ältere Kommentare anzeigen
x=0.1;
y=0.1;
a=[x,y];
b=[0.2,0.2];
tf=isequal(a,b);
while tf==0
x=x+(0.01);
y=y+(0.01);
a=[x,y];
tf=isequal(a,b);
end
disp(a);
if i replace the x, y and incremental values by integers, it works !! for ex as shown below
x=1000;
y=1000;
a=[x,y];
b=[2000,2000];
tf=isequal(a,b);
while tf==0
x=x+1;
y=y+1;
a=[x,y];
tf=isequal(a,b);
end
disp(a);
5 Kommentare
EDIT: See Stephen's answer below.
A possible workaround is to convert the floating point numbers to characters to compare them:
tf=(isequal(sprintf('%c',a),sprintf('%c',b)));
dpb
am 31 Mai 2018
Paolo
am 31 Mai 2018
Thanks for the clarification Stephen, I forgot to take into consideration the accumulating error.
akash sonnad
am 31 Mai 2018
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!