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?
#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.
double nan = NAN;
Pretty simple. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.