0

I have an NSMutableArray with 1 float value and 4 nan values. I want to execute some operations if the array object is nan. How can I write an if condition?

Ali Sufyan
  • 2,038
  • 3
  • 17
  • 27

4 Answers4

4

try isnan function

Iterate your array and put check

isnan([[Array objectAtIndex:i] floatValue])

(dont forget to add math.h library)

BaSha
  • 2,356
  • 3
  • 21
  • 38
1
NSNumber *num = //your number;
float value = [num floatValue];
if (isnan(value))
{
    NSLog(@"is nan");
}
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
1

Do something like this:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:1.0]];
[array addObject:[NSNull null]];
[array addObject:[NSNull null]];
[array addObject:[NSDecimalNumber notANumber]];


BOOL foundNull = NO;
for (id value in array) {
    if (!value || value == [NSNull null]) {
        foundNull = YES;
    } else if ([value isKindOfClass:[NSDecimalNumber class]]) {
        if ([value isEqualToNumber:[NSDecimalNumber notANumber]]) {
            foundNull = YES;
        }
    }
}

NSLog(@"Found null/NaN: %i", foundNull); 
Marius Kažemėkaitis
  • 1,723
  • 19
  • 22
0

You can also use

[NSDecimalNumber notANumber] method to compare and check if this a valid number or not.

RK-
  • 12,099
  • 23
  • 89
  • 155