问题描述
我试图找到可以存储在单个精度浮点数中的最小值(最接近零).使用<limits>
标头可以获取该值,但是如果我将其设置得更小,则浮点数仍然可以容纳它,并且给出正确的结果.这是一个使用g ++ 5.3.0编译的测试程序.
I'm trying to find the minimum value (closest to zero) that I can store in a single precission floating point number. Using the <limits>
header I can get the value, but if I make it much smaller, the float can still hold it and it gives the right result. Here is a test program, compiled with 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;
}
正如我预期的那样,"a"给出无穷大,但是"b"即使将最小值除以2 ^ 23后仍然保持良好的结果,之后它给出0.
As I expected, "a" gives infinity, but "b" keeps holding the good result even after dividing the minimum value by 2^23, after that it gives 0.
给出numeric_limits<float>::min()
的值是2 ^(-126),我相信这是正确的答案,但是为什么我的程序中的浮动内容持有如此小的数字呢?
The value that gives numeric_limits<float>::min()
is 2^(-126) which I belive is the correct answer, but why is the float on my progam holding such small numbers?
推荐答案
std::numeric_limits::min
对于浮点类型给出最小的非零值,该值可以表示而不会损失精度. std::numeric_limits::lowest
给出最小的可表示值.使用IEEE表示形式,该值是次标准值(以前称为非标准化).
std::numeric_limits::min
for floating-point types gives the smallest non-zero value that can be represented without loss of precision. std::numeric_limits::lowest
gives the smallest representable value. With IEEE representations that's a subnormal value (previously called denormalized).
这篇关于最小浮点数(最接近零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!