Is it possible to check if a number is NaN or not?
Is it possible to check if a number is NaN or not?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, by use of the fact that a
NaNis not equal to any other number, including itself.That makes sense when you think about what
NaNmeans, the fact that you’ve created a value that isn’t really within your power to represent with “normal” floating point values.So, if you create two numbers where you don’t know what they are, you can hardly consider them equal. They may be but, given the rather large possibility of numbers that it may be (infinite in fact), the chances that two are the same number are vanishingly small 🙂
You can either look for a function (macro actually) like
isnan(inmath.hfor C andcmathfor C++) or just use the property that aNaNvalue is not equal to itself with something like:If, for some bizarre reason, your C implementation has no
isnan(it should, since the standard mandates it), you can code your own, something like: