如何判断一个浮点数是 SNAN 还是 QNAN

How to tell if a float is SNAN or QNAN

我想知道如何打印浮点数是 QNAN 还是 SNAN。我已经将这些位分离为 signBit exponentBit 和 FractBits。

unsigned int sign = (i & 0x80000000) >> 31;
unsigned int exponent = (i & 0x7f800000) >> 23;
unsigned int fraction = (i & 0x007FFFFF);
printf("signBit %d, expBits %d, fractBits 0x%08X\n",sign, exponent, fraction);

GNU provides a facility which was recently standardized:

Macro: int issignaling (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is a signaling NaN (sNaN). It is based on draft TS 18661 and currently enabled as a GNU extension.

最终的 TS 草案提到您可能需要选择使用宏才能获得它:

#define __STDC_WANT_IEC_60559_BFP_EXT__
#include <math.h> 
int issignaling(real-floating x);

浮点实数的格式取决于处理器。在 x86 / x86-64 ISA 上,有效数的最高位 = 1 表示安静,0 表示信号。 (其余的 NaN 有效载荷仍然是任意的)。

  • float 类型:

  • double 类型:

  • long double类型(微软编译器不支持):