Determine if NSNumber is NaN

Objective CCocoaNanNsnumber

Objective C Problem Overview


How can I determine if a Cocoa NSNumber represents NaN (not a number)?

This emerges, for example, when I parse a string that has an invalid (non-numeric) contents.

Objective C Solutions


Solution 1 - Objective C

So, I found out that the class property [NSDecimalNumber notANumber] is just for this purpose. In some languages NaN != NaN, but this isn't the case in Cocoa.

Solution 2 - Objective C

As Mike Abdullah says, the natural way to represent a NaN in Cocoa is with nil, but [NSNumber numberWithDouble:NAN] does return a valid object. There is no NSNumber-specific way of detecting this, but the general way, isnan([foo doubleValue]), works. If you don’t like functions, you can stick it in a category.

Solution 3 - Objective C

For decimals, at least:

[[NSDecimalNumber notANumber] isEqualToNumber:myNumber]

Solution 4 - Objective C

To determine if NSNumber is a NaN, convert it to a double and use the C function isnan():

NSNumber *validNumber = [NSNumber numberWithDouble: 1.];
NSLog( @"%d", isnan(validNumber.doubleValue) ); // prints "0"

NSNumber *nanNumber = [NSNumber numberWithDouble: 0./0.];
NSLog( @"%d", isnan(nanNumber.doubleValue) ); // prints "1"

However, you should be careful, because there are other special values, for example:

NSNumber *posInfinity = [NSNumber numberWithDouble: 1./0.];
NSLog( @"%d", isnan(posInfinity.doubleValue) ); // prints "0"

If you want to check for these values as well, it's better to use isnormal() instead:

NSLog( @"%d", isnormal(validNumber.doubleValue) ); // prints "1"
NSLog( @"%d", isnormal(nanNumber.doubleValue) ); // prints "0"
NSLog( @"%d", isnormal(posInfinity.doubleValue) ); // prints "0"

Solution 5 - Objective C

I found that this works, but is it legal?

NSNumber *NaN = [NSDecimalNumber notANumber];

NSDecimalNumber *x = ... fill it somehow with NaN content ...

if ( x == NaN ) ... this works

is NaN guaranteeed to be a singleton constant value? Would be cool, but I suppose it is not, since all examples I found use the isEqual methods.

Solution 6 - Objective C

there is also the function isnan() i found it today.

Solution 7 - Objective C

We can also use a #define defined in math.h as follws

if(isnan(myNumber))
{  // myNumber is NaN .

}

Solution 8 - Objective C

Any Boolean expression with NaN will always return false. But how is that useful?

I was getting Nan back from locationInView: while handling some gestures in an iPhone app. And was very pleased to find that any Boolean expression with NaN will always return false. I put this to use liek below:

//I was using the UIPanGestureRecognizer, and seems that on TouchUp I would get Nan for the /location .x - .y - pretty reasonible since in this case the end of a touch doesnt have a location.

CGPoint location = [gestureRecognizer locationInView:self];

if ( location.x != location.x || location.y != location.y )
{
 return;
}

So then as long .x and .y or legitimate float values, of course they will never be not equal to there own value. BUT in the case of .x or .y being NaN, the comparison will be false. And I can safely avoid computations with Nan.

Solution 9 - Objective C

There isn't really such an object for NSNumber since if it's not a number, then, well, it's not an NSNumber. It's more usual to use a nil object to represent this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam ErnstView Question on Stackoverflow
Solution 1 - Objective CAdam ErnstView Answer on Stackoverflow
Solution 2 - Objective CJens AytonView Answer on Stackoverflow
Solution 3 - Objective CPeter DeWeeseView Answer on Stackoverflow
Solution 4 - Objective CJakob EggerView Answer on Stackoverflow
Solution 5 - Objective CThomasView Answer on Stackoverflow
Solution 6 - Objective CJohnny MastView Answer on Stackoverflow
Solution 7 - Objective CMak083View Answer on Stackoverflow
Solution 8 - Objective CAlan MorfordView Answer on Stackoverflow
Solution 9 - Objective CMike AbdullahView Answer on Stackoverflow