Can anybody help this loop question?
2 次查看(过去 30 天)
显示 更早的评论
Temp = 20;
g = 0.1;
x = 0.11;
while(Temp > 0 && x ~= g)
Temp = Temp -1
x = x -0.001
if(x==g)
print('lalala')
end
end
Running code shown above should give me a 'lalala' message in commond window at 10th loop, but I didn't get it and also it didn't break the while loop when x==g;
I added a break point inside the loop and trying to run in step, I get results in the figure below and matlab judge that x==g is logic 0 while they should equal to each other.
Can somebody help explain it?
采纳的回答
KSSV
2019-2-21
编辑:KSSV
2019-2-21
Temp = 20;
g = 0.1;
x = 0.11;
while(Temp > 0 && x ~= g)
Temp = Temp -1
x = x -0.001
if abs(x-g)<=10^-3
disp('lalala')
end
end
Read about how to compare flottant numbers.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!