Comparative Operator result is wrong
4 次查看(过去 30 天)
显示 更早的评论
I tried to run the if statement but this happens.
The result must be true(=1) in the last instruction but it returns false(=0)...
Anyone knows what is the problem???
1 个评论
Stephen23
2021-4-12
"Anyone knows what is the problem???"
The assumption that binary floating point numbers have infinite precision:
This is worth reading as well:
回答(3 个)
KSSV
2021-4-12
Note that you are comparing two floating point numbers and such numbers cannot be comapred using ==. Follow
tol = 10^-5 ;
idx = abs(val1-val2)<tol
Steven Lord
2021-4-12
Two numbers that are displayed the same may not represent the same stored value. See this Answers post for more information about floating point numbers.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!