- Home /
Is IsNan not IsInfinity ??
Say you have some code like this:
{
....
vx = ( blah ) / ( blah );
}
obviously, in real production code you have to check somehow that you';re not creating an infinity. I have used the following code to check for such a problem:
{
....
vx = ( blah ) / ( blah );
if ( float.IsInfinity(vx) || float.IsNegativeInfinity(vx) )
{
vx = 12345.6;
Debug.Log("Saved an infinity situation! Whoo!! We rock!");
}
}
However! I have recently seen cases where that still produces a NaN error, at least in the editor. So today I do this
{
....
vx = ( blah ) / ( blah );
if ( float.IsInfinity(vx)
|| float.IsNegativeInfinity(vx)
|| float.IsNan(vx) )
{
vx = 12345.6;
Debug.Log("New technology saved an infinity! Whoox2!");
}
}
So, does anyone decisively understand, exactly, what the difference is between IsNan, IsInfinity, and so on? What's the best idiom to use in this situation?
If so, thank you!
Answer by Bunny83 · Aug 17, 2012 at 03:33 PM
Exactly, NaN(Not a Number) is not the same as infinity. floats have actually a lot different "error types", but they all result in NaN. Infinity is an actual number, at least the representation. NaN actually says something is wrong because the result is not a number. The squareroot of a negative value will produce NaN since the result it's not defined within the real numbers.
NaN also "infects" calculations and always produce NaN as result.
// C#
float A = 2.0f;
float B = 7.0f;
float C = float.NaN;
float result = A * B + C;
This will result in NaN because C is NaN. When ever you use a NaN value in a calculation the calculation will also result in NaN.
Furthermore NaN can't be compared to any value, even to itself.
bool its_A_Number = (C == C);
This will result in "false" because you can't compare NaN to anything.
So if your operands don't contain a NaN value, something went wrong in the calculation.
Some more concrete examples:
1.0 / 0.0 --> +infinity
-1.0 / 0.0 --> -infinity
0.0 / 1.0 --> 0.0f
0.0 / 0.0 --> NaN
+inf * +inf --> +infinity
-inf * +inf --> -infinity
-inf * -inf --> +infinity
+inf + +inf --> +infinity
-inf + +inf --> NaN
+inf + 5.0 --> +infinity
ATan(+inf) --> PI/2 aka 90°
sqrt( -1 ) --> NaN
log( 0 ) --> -infinity
log( -1 ) --> NaN
:D Well infinity is a special value, not just the highest / lowest possible value. They are treated mathematically correctly, so when you divide a number by infinity it becomes 0.0f and so on. NaN is actually the result of an error. It depends on your formula for which of those values you want to check and what you do in those cases.
Answer by IndieScapeGames · Aug 17, 2012 at 03:18 PM
NAN as far as I know is 'Not A Number' and I suppose if the number got too large, or too small, it could result in an NAN exception.
chad, I totally hear you, but what we need here is EXTRE$$anonymous$$ELY DECISIVE INFOR$$anonymous$$ATION
I found it hard to find any exact, compelling doco on this online....
Use IsNaN to deter$$anonymous$$e whether a value is not a number. It is not possible to deter$$anonymous$$e whether a value is not a number by comparing it to another value equal to NaN.
Answer by Weitzel · Aug 17, 2012 at 03:36 PM
When compiled, the code will be attempted to execute a potential divide by either zero, or a very near zero number, which will result in vx being set to NAN. Typically if you end up doing something like this, it is unintentional and therefore does not default to setting it to infinity.
You can always check the precondition and postcondition of your variables and deliberately set to infinity if you'd like/need, which can be represended by the mathematical representation of infinity, and can be helpful. I believe the isInfinity or isNegativeInfinity checks deal check for and compare against these.
$$anonymous$$att - in computing, be careful about comparing your divisor to zero !!!
the problem is not zero, but any relatively very small number. Checking divisor == 0.0 never works. You should edit your answer in case someone gets the wrong info in the future !
Answer by Steazy · Jul 28, 2015 at 09:52 PM
[Edited: Sarcasm and jackassery removed per Bunny83's comment - Also my answer got approved so I was 100% wrong in my assertion. Sorry!]
Useful links w/out any sarcasm:
@S$$anonymous$$zy:
Well, most people don't even know what the standard that defines the floating point format we're all using is actually called and i can't remember the IEEE number as well. Once you know that it has been specified by the IEEE it's quite easy to find more information about that.
The dedicated NaN wiki page was created 2012 so it's relatively new if you actually look at the questions date.
And yes, we don't need sarcastic posts that revives a 3 years old question. However adding those links is always appreciated. I usually add a lot links to my answer if i have them at hand. However i often write an answer on my tablet and it's a pain to search and add links there. At the moment on my PC i have about 30 UA tabs, 10 wiki pages, 10 google tabs, and about 15 other pages open.
Your answer
Follow this Question
Related Questions
Closure does not work in Unity JS functions? 1 Answer
Collide detection with tag [JS] 0 Answers
Creating a dynamic array of objects of custom class 1 Answer
Make a command run only once 1 Answer
Help Please Debug A Script - Fixed! 1 Answer