Effacer les filtres
Effacer les filtres

Why is NaN not equal to NaN

14 vues (au cours des 30 derniers jours)
Michael
Michael le 16 Jan 2013
Commenté : Matt J le 17 Sep 2021
Why is this not true?
NaN==NaN
ans =
0

Réponse acceptée

Cedric
Cedric le 16 Jan 2013
Modifié(e) : Cedric le 16 Jan 2013
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.
  2 commentaires
Matt J
Matt J le 16 Jan 2013
Modifié(e) : Matt J le 16 Jan 2013
Although more recently, isequalwithequalnans has been deprecated in favor of isequaln
Cedric
Cedric le 16 Jan 2013
Ah thank you, I still have 2011b and I wasn't aware (despite linking the doc for 2012b ;-/).

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 16 Jan 2013
Modifié(e) : Matt J le 16 Jan 2013
Because equality is a notion applied to numbers. How can 2 things be equal when they are not numbers?
  7 commentaires
math man
math man le 17 Sep 2021
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?
Matt J
Matt J le 17 Sep 2021
@math man So you think the result of NaN ~= NaN should be NaN? Maybe.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by