Determine if NSNumber NaN

How to determine if Cocoa NSNumber NaN (not a number) exists?

This occurs, for example, when analyzing a string with invalid (non-numeric) content.

+40
cocoa nsnumber
Apr 05 '09 at 18:24
source share
9 answers

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

+61
Apr 05 '09 at 18:33
source share

As Mike Abdullah says, the natural way to represent NaN in Cocoa is nil , but [NSNumber numberWithDouble:NAN] returns a valid object. There is no NSNumber way to detect this, but the general way, isnan([foo doubleValue]) , works. If you don't like the features, you can paste it into a category.

+15
Apr 6 '09 at 22:15
source share

For decimal places, at least:

 [[NSDecimalNumber notANumber] isEqualToNumber:myNumber] 
+10
Jun 29 2018-11-18T00:
source share

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

 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 have to 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 also want to check these values, it is 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" 
+8
Nov 15 '12 at 19:01
source share

I found this to work, but is it legal?

 NSNumber *NaN = [NSDecimalNumber notANumber]; NSDecimalNumber *x = ... fill it somehow with NaN content ... if ( x == NaN ) ... this works 

is NaN guaranteed as a constant singleton value? It would be great, but I believe that this is not so, since all the examples I found use isEqual methods.

+6
Aug 03 '12 at 20:22
source share

there is also the isnan () function that I found today.

+5
Jan 21 '10 at 18:21
source share

We can also use #define defined in math.h as described below

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

}

+2
Aug 4 '14 at 7:20
source share

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

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

// I used UIPanGestureRecognizer , and it seems that on TouchUp I would get Nan for /location.x -.y - pretty reasonable, since in this case the end of touch has no location.

 CGPoint location = [gestureRecognizer locationInView:self]; if ( location.x != location.x || location.y != location.y ) { return; } 

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

0
Oct. 25 '10 at 19:08
source share

Actually there is no such object for NSNumber, because if it is not a number, then, well, it is not NSNumber. This is a more common use of the nil object to represent this.

-2
Apr 06 '09 at 12:37
source share



All Articles