Moshe
Moshe

Reputation: 58097

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?

Upvotes: 5

Views: 3057

Answers (2)

Stephen Canon
Stephen Canon

Reputation: 106297

#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.

Upvotes: 11

Dave DeLong
Dave DeLong

Reputation: 243156

double nan = NAN;

Pretty simple. :)

Upvotes: 11

Related Questions