53

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.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Adam Ernst
  • 52,440
  • 18
  • 59
  • 71

9 Answers9

75

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.

Adam Ernst
  • 52,440
  • 18
  • 59
  • 71
  • 46
    Just for clarity, to test if an NSNumber is NaN then the code would be: `[NSNumber isEqualToNumber:[NSDecimalNumber notANumber]]`. This returns YES if the NSNumber is NaN. – SimonB Mar 08 '12 at 21:28
19

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.

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
  • hey guys: I'm pretty sure nil doesn't mean the same thing as NaN. Nil means no value at all. NaN is used to represent the result of certain floating point operations (dividing by zero, for example)... so really NaN IS a number.. It's the number "Not A Number"... – nielsbot Jul 19 '11 at 01:22
  • 1
    Yes, we know that. The point remains that `nil` is the natural way to represent things like a string with non-numeric constants, as in the OP’s example. – Jens Ayton Jul 19 '11 at 13:20
15

For decimals, at least:

[[NSDecimalNumber notANumber] isEqualToNumber:myNumber]
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
12

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"
Jakob Egger
  • 11,981
  • 4
  • 38
  • 48
  • It’s worth noting that `isnormal(0)` returns 0 (same as `isnormal(NAN)`), so if zero is a potentially valid value, check both `inan` and `isinf` instead. `isfinite()` is a better option. – Demitri Dec 16 '16 at 22:01
10

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.

Daniel
  • 23,129
  • 12
  • 109
  • 154
Thomas
  • 101
  • 1
  • 2
  • Sure, no word 'singleton' in documentation, seems need to isEqual-ish all checks. (( – dmitrynikolaev Jul 14 '13 at 13:03
  • NaN is not a unique value - there are variants, including 'quiet' NaN and 'signaling' NaN, and the encoding definition allows some irrelevant bits (e.g. sign) to have any value. For this reason == NAN cannot be relied upon. – Alex Brown Oct 15 '13 at 19:26
  • Surely it's singleton behind the scenes, but it's still better to use sEqual in case if this behavior change in future. – pronebird Sep 25 '15 at 10:38
5

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

Johnny Mast
  • 703
  • 1
  • 9
  • 17
3

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

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

}

Mak083
  • 1,160
  • 1
  • 13
  • 33
  • 1
    This solution works with primitive data types and not with NSNumber objects directly, you have to get the value out of NSNumber, example: number.intValue – ObjSal Jan 11 '16 at 22:43
0

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.

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
-3

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.

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75