numeric_limits<double>::digits10是什么意思
numeric_limits::digits10 的确切含义是什么?stackoverflow 中的一些其他相关问题让我认为它是双精度的最大精度,但是
What is the precise meaning of numeric_limits::digits10? Some other related questions in stackoverflow made me think it is the maximum precision of a double, but
- 当精度大于 17 ( == 2+numeric_limits::digits10) 时,以下原型开始工作(成功为真)
- 使用STLPort,最后readDouble==infinity;使用微软的 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::digits10
is the number of decimal digits that can be held without loss.
例如 numeric_limits
是 2.这意味着无符号字符可以保持 0..99 而不会丢失.如果是 3,它可以容纳 0..999,但众所周知,它只能容纳 0..255.
For example numeric_limits<unsigned char>::digits10
is 2. This means that an unsigned char can hold 0..99 without loss. If it were 3 it could hold 0..999, but as we all know it can only hold 0..255.
本手册页有一个浮点数示例,其中(缩短时)表明
This manual page has an example for floating point numbers, which (when shortened) shows that
cout << numeric_limits<float>::digits10 <<endl;
float f = (float)99999999; // 8 digits
cout.precision ( 10 );
cout << "The float is; " << f << endl;
印刷品
6
The float is; 100000000
相关文章