53

Possible Duplicates:
Objective-C - float checking for nan
Determine if NSNumber is NaN

I have a problem with NaN values in CGFloat, how can I check if the number is valid?

the only way so far that works is:

if ([[NSString stringWithFormat:@"%f", output] isEqualToString:@"nan"]) {
    output = 0;
}

which is not a nice solution at all! :) ... and I am pretty sure that there is something else I should do instead.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ondrej Rafaj
  • 4,342
  • 8
  • 42
  • 65
  • 1
    A chain of dupes, even: [Objective C - float checking for nan](http://stackoverflow.com/questions/3471988/objective-c-float-checking-for-nan) to [isNan in objective c](http://stackoverflow.com/questions/2109257/isnan-in-objective-c) to [Determine if NSNumber is NaN](http://stackoverflow.com/questions/719417/determine-if-nsnumber-is-nan) – jscs Jun 03 '11 at 00:26
  • 2
    I believe [this question](http://stackoverflow.com/questions/3471988/objective-c-float-checking-for-nan) has the answer to your problem. – makdad Jun 02 '11 at 21:52

1 Answers1

153

There is a define for checking if a number is nan inf etc in math.h (you can use it without import I think).

isnan(myValue)

if you follow the define you will end up with

(x!=x)

there are also some other useful defines like isinf, isnormal , isfinite , ...

thomas
  • 5,637
  • 2
  • 24
  • 35