C 中的 NaN 文字?

发布于 2024-11-02 19:14:04 字数 40 浏览 2 评论 0原文

如何用 C 语言编写 NaN 浮点文字?

How do you write an NaN floating-point literal in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青巷忧颜 2024-11-09 19:14:05

在C99的

 7.12 数学 
   [#5] 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.           |

In C99's <math.h>

  7.12  Mathematics <math.h>
   [#5] 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.           |
过潦 2024-11-09 19:14:05

5.2.4.2.2/3:

浮动类型可以包含
其他类型的浮点数,
例如……无穷大和 NaN。 NaN 是一种编码
表示非数字。一个安静的 NaN 几乎通过传播
每个算术运算都没有
引发浮点异常;一个
发出 NaN 信号通常会引发
发生时浮点异常
作为算术操作数。

7.12/5(数学.h):

NAN 被定义当且仅当
该实现支持安静的 NaN
对于浮动类型。它扩展为一个
float 类型的常量表达式
代表一个安静的 NaN。

因此,如果您的实现完全支持 NaN,并且如果部分或全部 NaN 是安静的,您就可以获得一个值。如果失败,您就进入了实现定义的领域。

对于其中一些浮点宏,还存在一个轻微的担忧,即编译器前端可能不知道它是否支持该功能,因为它生成的代码可以在架构的多个版本上运行支持情况各不相同。我不知道这是否适用于这个宏,但这是另一种情况,您进入实现定义的领域 - 预处理器可能保守地声称它不支持该功能,而实际上整个实现,就像您一样重新使用它,确实如此。

5.2.4.2.2/3:

floating types may be able to contain
other kinds of floating-point numbers,
such as ... infinities and NaNs. A NaN is an encoding
signifying Not-a-Number. A quiet NaN propagates through almost
every arithmetic operation without
raising a floating-point exception; a
signaling NaN generally raises a
floating-point exception when occurring
as an arithmetic operand.

7.12/5 (math.h):

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.

So you can get a value if your implementation supports NaNs at all, and if some or all of those NaNs are quiet. Failing that you're into implementation-defined territory.

There's also a slight worry with some of these floating-point macros that the compiler front-end might not know whether it supports the feature or not, because it produces code that can run on multiple versions of an architecture where support varies. I don't know whether that applies to this macro, but it's another situation where you're into implementation-defined territory - the preprocessor might conservatively claim that it doesn't support the feature when actually the implementation as a whole, as you're using it, does.

黑寡妇 2024-11-09 19:14:05

使用 NAN 更好,但如果您使用的是具有 NaN 的系统,0.0/0.0 是获取 NaN 的简单方法...

Using NAN is better, but if you're on a system that has NaNs, 0.0/0.0 is an easy way to get one...

你爱我像她 2024-11-09 19:14:05

在 C 中,您可以通过以下方式编写 NaN 浮点文字。

const unsigned long dNAN[2] = {0x00000000, 0x7ff80000};
const double LITERAL_NAN = *( double* )dNAN;

请注意,这不是标准方法。在 Microsoft C 上,它工作得很好。

In C you can write a NaN floating point literal in the following way.

const unsigned long dNAN[2] = {0x00000000, 0x7ff80000};
const double LITERAL_NAN = *( double* )dNAN;

Please note that, this is not a standard way. On Microsoft C, it works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文