- Home /
How to check for NaN
Hi there,
I'm using C# and am using some quaternions in one of my scripts. Every so often I receive a debug message saying that some of one of my objects rotation values are Nan, or 'Not a Number' (likely because under certain circumstances the square root of a negative number is being calculated / something is being divided by 0).
In any event, I want to perform a check to see if the value will be NaN before altering the object's rotation. Something like:
if(rotation == Nan)...
or
if(rotation == error)...
Is there such a way to do this? All I could find was this link, but I can't find a C# equivalent: http://answers.unity3d.com/questions/182209/Checking-for-quaternion-values-to-not-be-NaN.html
There's an older Quaternion-specific question here. I just added an answer with an extension method so you don't have to check every component of the Quaternion explicitly every time.
Answer by Stormizin · May 24, 2013 at 06:44 PM
The .NET framework already provides simple ways to achieve this. You can use the constant float.NaN if you need to actually generate a NaN in code, and float.IsNaN(value)
returns true if the value is a NaN.
There are some related things, such as constants and comparisons for infinity.
Answer by Paulo33 · May 09, 2014 at 05:31 PM
Well, i know this is a old post but, i will help others with simple soluction for check if "float variable" is a NaN or not.
if(float.IsNaN("YOURFLOATVARIABLE")){
//Your condition...
}
else{
//Your "else" condition
}
Answer by dentedpixel · Jun 02, 2013 at 08:42 PM
if(!System.Single.IsNaN( someVariable ))
// assign value
Answer by Varaughe · Aug 18, 2014 at 03:16 PM
can someone explain me ,why (float.NaN == float.NaN) is false ???
Please use answers for answers, not new questions.
To borrow from a StackExchange answer:
There was no isnan( ) predicate at the time that NaN was formalized in the 8087 arithmetic; it was necessary to provide programmers with a convenient and efficient means of detecting NaN values that didn’t depend on program$$anonymous$$g languages providing something like isnan( ) which could take many years. I’ll quote $$anonymous$$ahan’s own writing on the subject:
"Were there no way to get rid of NaNs, they would be as useless as Indefinites on CRAYs; as soon as one were encountered, computation would be best stopped rather than continued for an indefinite time to an Indefinite conclusion. That is why some operations upon NaNs must deliver non-NaN results. Which operations? … The exceptions are C predicates “ x == x ” and “ x != x ”, which are respectively 1 and 0 for every infinite or finite number x but reverse if x is Not a Number ( NaN ); these provide the only simple unexceptional distinction between NaNs and numbers in languages that lack a word for NaN and a predicate IsNaN(x)."
So, short answer: it was important to be that way when the IEEE 754 spec was written, so we're still stuck with that convention today. Fortunately, we have IsNaN() now to check for this case explicitly.
$$anonymous$$y view is that equivalency is a mathematical concept that in this context only applies to numbers. If the thing is not a number then logically all numerical mathematical operations are not defined. This includes equal. Thus the answer to Nan == Nan should be undefined, but since the operation must return either true or false and cannot return "not a boolean", false has been chosen. Although either are incorrect, false is probably 'less wrong' overall.
For example, if two calculations are performed and the results would be very different, but due to some precision or other error both get turned into Nan, then suddenly they become equal. It is better to treat both as 'unknown' and if unknown, not treat them as unequal.
Your answer
Follow this Question
Related Questions
Clarifying the ambiguities of rotation in Unity 1 Answer
Rotating local transform not rotating fully if tranform.up is not exactly at (0,1,0) 1 Answer
How to handler rotation of a Gameobject while aligning it with normals 1 Answer
Object makes random jumps in rotation 1 Answer
Match Y axis rotation. 1 Answer