gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:59 24 4
gpt4 key购买 nike

我想知道如何打印 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com