Why is NaN not equal to NaN
17 次查看(过去 30 天)
显示 更早的评论
Why is this not true?
NaN==NaN
ans =
0
0 个评论
采纳的回答
Cedric
2013-1-16
编辑:Cedric
2013-1-16
Most times, you don't want this test to return true indeed. Imagine testing a==b and having a true result when both a and b are undefined; this would make little sense. It is the same with NaN.
While it is not the original question, note that you can test equality assuming NaN 's are equal:
But with that you usually want to test if "behaviors" are the same (and not equality per se), e.g. if two functions return the same number or both NaN do something, else do something else.
更多回答(1 个)
Matt J
2013-1-16
编辑:Matt J
2013-1-16
Because equality is a notion applied to numbers. How can 2 things be equal when they are not numbers?
7 个评论
math man
2021-9-17
You might wonder, though, why NaN ~= NaN returns true?
That is, if they are not numbers why are they compared in this case to give a misleading result?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!