小编Sha*_*ang的帖子

直接计算的最小正浮点值与<float.h>中的FLT_MIN不同

我想直接计算该float类型的最小值,这是我的算法(假设浮点数的编码符合IEEE 754标准):

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

float float_min()
{
    int exp_bit = CHAR_BIT * sizeof(float) - FLT_MANT_DIG;
    float exp = 2 - pow(2, exp_bit - 1);

    float m = pow(2, -(FLT_MANT_DIG - 1));

    return m * pow(2, exp);
}

int main()
{
    printf("%g\n", float_min());
}
Run Code Online (Sandbox Code Playgroud)

输出是1.4013e-45.不过,我觉得值了FLT_MIN的C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\float.h是1.175494351e-38F.谁错了?

c floating-point

18
推荐指数
2
解决办法
1429
查看次数

标签 统计

c ×1

floating-point ×1