温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c++ - How can the significand width in bits of float, double be determined: Is there a standard definition
c c++ linux floating-point ieee-754

c++ - 如何确定以浮点数为单位的有效宽度,加倍:是否有标准定义

发布于 2020-03-27 11:29:25

是否有标准的方法来确定C或C ++中双精度数的宽度?我知道IEEE-754的double格式以53位存储有效数字,但是我想避免在代码中使用“魔术”数字。

在Linux上,该文件usr/include/ieee754.h存在,但是它使用结构中的位字段描述了格式,我无法确定其大小(在编译时)。

仅Linux解决方案是可以接受的。

查看更多

查看更多

提问者
Jamie
被浏览
163
Eric Postpischil 2019-07-03 23:06

使用FLT_MANT_DIGDBL_MANT_DIG,定义于<float.h>

#include <float.h>
#include <stdio.h>


#if FLT_RADIX != 2
    #error "Floating-point base is not two."
#endif


int main(void)
{
    printf("There are %d bits in the significand of a float.\n",
        FLT_MANT_DIG);
    printf("There are %d bits in the significand of a double.\n",
        DBL_MANT_DIG);
}