x


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!

more ▼

asked Aug 17 '12 at 03:13 PM

Fattie gravatar image

Fattie
19k 57 86 148

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Aug 17 '12 at 03:33 PM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

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
Aug 17 '12 at 03:37 PM Bunny83

: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.

Aug 17 '12 at 04:02 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 17 '12 at 03:18 PM

ChadM gravatar image

ChadM
208 1 4 8

chad, I totally hear you, but what we need here is EXTREMELY DECISIVE INFORMATION

I found it hard to find any exact, compelling doco on this online....

Aug 17 '12 at 03:21 PM Fattie

Use IsNaN to determine whether a value is not a number. It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN.

Aug 17 '12 at 03:38 PM ChadM
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 17 '12 at 03:36 PM

Weitzel gravatar image

Weitzel
170 4

Matt - 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 !

Aug 17 '12 at 03:46 PM Fattie

fixed, cheers

Aug 17 '12 at 03:55 PM Weitzel
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3459
x288
x9

asked: Aug 17 '12 at 03:13 PM

Seen: 728 times

Last Updated: Dec 07 '12 at 06:24 PM