numeric_limits< double> :: digits10的含义是什么? [英] What is the meaning of numeric_limits<double>::digits10

查看:1142
本文介绍了numeric_limits< double> :: digits10的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numeric_limits :: digits10的确切含义是什么?
stackoverflow中的其他一些相关问题让我认为这是double的最大精度,但

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


  • 以下原型开始工作(sucess is true)当精度大于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< unsigned char> :: digits10 是2.这意味着unsigned char可以保存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;

print

6
The float is; 100000000

这篇关于numeric_limits&lt; double&gt; :: digits10的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆