Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Objective-C equivalent of Double.NaN in Java?

What is the Objective-C equivalent of Double.NaN from Java? I've got a function that returns a double, but it has two cases where it can return Double.NaN. How would I implement that in Objective-C?

like image 998
Moshe Avatar asked Mar 23 '11 19:03

Moshe


2 Answers

double nan = NAN;

Pretty simple. :)

like image 93
Dave DeLong Avatar answered Nov 17 '22 22:11

Dave DeLong


#include <math.h>

...
return NAN;

simple as that. If for some reason you cannot include <math.h>, you can also use

return __builtin_nan("");

with either GCC or clang.

Incidentally, like most low-level language features of Objective-C, this is inherited directly from C. The relevant portion of the C spec is §7.12:

The macro NAN is defined if and only if the implementation supports quiet NaNs for the float type. It expands to a constant expression of type float representing a quiet NaN.

As you learn Objective-C, keep in mind that every C program is an Objective-C program, and there's nothing wrong with using C language features to solve your problem.

like image 11
Stephen Canon Avatar answered Nov 17 '22 22:11

Stephen Canon