Mse*_*ade 4 c++ floating-point gcc
我正在尝试找到可以存储在单个精确浮点数中的最小值(最接近零).使用<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;
}
Run Code Online (Sandbox Code Playgroud)
正如我所预料的那样,"a"给出无穷大,但是"b"在将最小值除以2 ^ 23之后仍然保持良好的结果,之后它给出0.
给出的值numeric_limits<float>::min()是2 ^( - 126),我相信这是正确答案,但为什么我的程序上的浮点数保持这么小的数字?
std::numeric_limits::min对于浮点类型,给出了可以在不损失精度的情况下表示的最小非零值.std::numeric_limits::lowest给出最小的可表示值.IEEE表示是一个次正规值(以前称为非规范化).
来自维基百科https://en.wikipedia.org/wiki/Single- precision_floating-point_format :
\n\n\n\n\n最小正正常值为 2^\xe2\x88\x92126 \xe2\x89\x88 1.18 \xc3\x97 10^\xe2\x88\x9238,\n 最小正(非正常)值为 2^\xe2\x88 \x92149 \xe2\x89\x88 1.4 \xc3\x97 10^\xe2\x88\x9245。
\n
因此对于
\n\ncout << (float)pow(2,-149) \n << "-->" << (float)pow(2,-150) \n << "-->" << (float)pow(2,-151) << endl;\nRun Code Online (Sandbox Code Playgroud)\n\n我越来越:
\n\n1.4013e-45-->0-->0\nRun Code Online (Sandbox Code Playgroud)\n