我试图找到最小值(最接近零),我可以存储在一个单精度浮点数中。使用<limits>
头我可以得到的值,但如果我使它更小,浮动仍然可以容纳它,它会给出正确的结果。这是一个测试程序,用g ++ 5.3.0编译。最小浮点数(最接近零)
#include <limits>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a = numeric_limits<float>::max();
float b = numeric_limits<float>::min();
a = a*2;
b = b/pow(2,23);
cout << a << endl;
cout << b << endl;
}
正如我所料,“一个”给出无穷大,但“b”的甚至2^23除以最小值之后持续保持良好的结果,它给出0.
的值之后给出numeric_limits<float>::min()
是2 ^( - 126),我相信这是正确的答案,但为什么我的程序上的浮点数保持如此小的数字?
你在考虑反常规吗? –
您需要将FLOAT_MIN乘以FLOAT_EPSILON才能获得最小的正浮点数。否则,只需使用FLOAT_MIN,就会得到最小的正常正浮点数。 –
@void_ptr我不知道,但他们似乎是答案 – Msegade