c++ - 如何判断一个 float 是 SNAN 还是 QNAN

标签 c++ c gcc assembly ieee-754

我想知道如何打印 float 是 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设施 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);

关于c++ - 如何判断一个 float 是 SNAN 还是 QNAN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666236/

相关文章:

c - 如果我正在制作一种新的编程语言,我应该编译成汇编还是 C?

gcc - 为什么 GCC std::atomic 增量会产生低效的非原子组装?

c - Gcc编译器C字符串赋值问题

c++ - 为什么三元运算符转 (... ? int : unsigned long long) into int when an implicit conversion is involved?

c++ - 调试多线程应用程序时的步骤行

c++ - nullptr 作为模板参数

c - 鲍勃·马丁 : "C has perfect encapsulation" HOW?

c++ - 创建一个自定义整数,强制始终在指定范围内;如何克服整数溢出?

c - 如何从 C 中的其他函数调用 main()

c - 先前的堆栈变量