How to catch a Nan?

How to catch a Nan?

NaN is not equal to any value, including NaN, use this property instead of the equal-to operator (==) or not-equal-to operator (!=) to test whether a value is or is not NaN.

 

Nan represents Not a number

When it happens to get a Nan as a result of an expression you have to find a way to catch it before.

Let’s think an if statement and inside this the appropriate check.

A working code is the following:

Objective-C

The class property [NSDecimalNumber notANumber] is just for this purpose. In some languages NaN != NaN

 

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

 

SWIFT