简体   繁体   English

什么是Java中Double.NaN的Objective-C等价物?

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

What is the Objective-C equivalent of Double.NaN from Java? 什么是Java的Double.NaN的Objective-C等价物? I've got a function that returns a double, but it has two cases where it can return Double.NaN . 我有一个返回double的函数,但它有两种情况可以返回Double.NaN How would I implement that in Objective-C? 我将如何在Objective-C中实现它?

#include <math.h>

...
return NAN;

simple as that. 就那么简单。 If for some reason you cannot include <math.h> , you can also use 如果由于某种原因你不能包含<math.h> ,你也可以使用

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: 顺便说一句,与Objective-C的大多数低级语言特性一样,这是直接从C继承的.C规范的相关部分是§7.12:

The macro NAN is defined if and only if the implementation supports quiet NaNs for the float type. 当且仅当实现支持float类型的安静NaN时,才定义宏NAN It expands to a constant expression of type float representing a quiet NaN. 它扩展为float类型的常量表达式,表示安静的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. 在学习Objective-C时,请记住每个 C程序都是Objective-C程序,使用C语言功能解决问题并没有错。

double nan = NAN;

Pretty simple. 很简单。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM