numeric_limits::digits10 的确切含义是什么?
stackoverflow中的一些其他相关问题让我认为这是双精度的最大精度,但是
- 当精度大于 17 ( == 2+numeric_limits::digits10) 时,以下原型开始工作(成功为真)
- 使用STLPort,最后readDouble==无穷大;对于微软的STL,readDouble == 0.0。
- 这个原型有什么意义吗:)?
这是原型:
#include <float.h>
#include <limits>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
int main(int argc, const char* argv[]) {
std::ostringstream os;
//int digit10=std::numeric_limits<double>::digits10; // ==15
//int digit=std::numeric_limits<double>::digits; // ==53
os << std::setprecision(17);
os << DBL_MAX;
std::cout << os.str();
std::stringbuf sb(os.str());
std::istream is(&sb);
double readDouble=0.0;
is >> readDouble;
bool success = fabs(DBL_MAX-readDouble)<0.1;
}
numeric_limits::digits10
是可以无损保存的小数位数。
例如numeric_limits<unsigned char>::digits10
是 2。这意味着一个 unsigned char 可以保存 0..99 而不会丢失。If它是 3,它可以容纳 0..999,但众所周知,它只能容纳 0..255。
本手册页 http://msdn.microsoft.com/en-us/library/af6x78h6.aspx有一个浮点数的例子,它(缩短时)表明
cout << numeric_limits<float>::digits10 <<endl;
float f = (float)99999999; // 8 digits
cout.precision ( 10 );
cout << "The float is; " << f << endl;
prints
6
The float is; 100000000
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)