5

Is there a function like isNan (Javascript example) for Objective-C?

I just noticed this code is causing to display Nan %, so I need to detect is a value is NAN so I could change it to 0.

[portefeuillePercentWaarde setText:[NSString stringWithFormat:@"%.2f%%", verschilPrencentage]];
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Johnny Mast
  • 703
  • 1
  • 9
  • 17
  • Voted to close as duplicate. The good news is that the other question has your answer :) http://stackoverflow.com/questions/719417/determine-if-nsnumber-is-nan – e.James Jan 21 '10 at 12:54
  • @e.James: not a duplicate: your link is for NSNumber, this question is for float/double. – Cœur Oct 12 '16 at 05:57
  • Objective-C for float or double: `isnan(verschilPrencentage)`; Swift for floatingPointType: `verschilPrencentage.isNaN`. – Cœur Oct 12 '16 at 06:19

1 Answers1

22

Try just adding

#include <math.h>

and then using the standard isnan() function to test.

You can also use the "trick" that NaN is not equal to anything, including itself:

double x;

// do some operation here
x = doSomething();
if (x != x)
  printf("%g is probably NaN, it's not equal to itself\n", x);
unwind
  • 391,730
  • 64
  • 469
  • 606