简体   繁体   English

numeric_limits是什么意思? <double> :: digits10

[英]What is the meaning of numeric_limits<double>::digits10

What is the precise meaning of numeric_limits::digits10? numeric_limits :: digits10的确切含义是什么? Some other related questions in stackoverflow made me think it is the maximum precision of a double, but stackoverflow中的一些其他相关问题使我认为它是double的最大精度,但是

  • The following prototype starts working (sucess is true) when precision is greater that 17 ( == 2+numeric_limits::digits10) 当精度大于17(== 2 + numeric_limits :: digits10)时,以下原型开始工作(成功为真)
  • With STLPort, readDouble==infinity at the end; 使用STLPort,最后是readDouble == infinity; with microsoft's STL, readDouble == 0.0. 使用microsoft的STL,readDouble == 0.0。
  • Has this prototype any kind of meaning :) ? 这个原型有任何意义:)?

Here is the prototype: 这是原型:

#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 is the number of decimal digits that can be held without loss. numeric_limits::digits10是可以保持不丢失的小数位数。

For example numeric_limits<unsigned char>::digits10 is 2. This means that an unsigned char can hold 0..99 without loss. 例如, numeric_limits<unsigned char>::digits10是2.这意味着unsigned char可以保持0..99而不会丢失。 If it were 3 it could hold 0..999, but as we all know it can only hold 0..255. 如果它是3它可以保持0..999,但是我们都知道它只能保持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;

prints 版画

6
The float is; 100000000

numeric_limits::digits10 specifies the number of decimal digits to the left of the decimal point you can represent without a loss of precision. numeric_limits :: digits10指定您可以表示的小数点左侧的小数位数,而不会丢失精度。 Each type will have a different number of representable decimal values. 每种类型都有不同数量的可表示的十进制值。

See this very readable paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2005.pdf 请参阅这篇非常易读的文章: http//www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2005.pdf

Although DBL_MAX ( = std::numeric_limits::digits10 = 15 digits) is the minimum guaranteed number of digits for a double, the DBL_MAXDIG10 value (= 17 digits) proposed in the paper has the useful properties: 虽然DBL_MAX(= std :: numeric_limits :: digits10 = 15位)是double的最小保证位数,但本文中提出的DBL_MAXDIG10值(= 17位)具有以下有用属性:

  • Of being the minimum number of digits needed to survive a round-trip to string form and back and get the same double in the end. 这是生成往返字符串形式所需的最小位数,然后返回并获得相同的双精度数。

  • Of being the minimum number of digits needed to convert the double to string form and show different strings every time you get (A != B) in code. 是每次在代码中获得(A!= B)时将double转换为字符串形式并显示不同字符串所需的最小位数。 With 16 or fewer digits, you can get doubles that are not equal in code, but when they are converted to string form they are the same (which will give the case where they are different when compared in the code, but a log file will show them as identical - very confusing and hard to debug!) 使用16位或更少的数字,您可以获得在代码中不相等的双精度数,但是当它们转换为字符串形式时它们是相同的(这将给出在代码中进行比较时它们不同的情况,但是日志文件将将它们显示为相同 - 非常混乱且难以调试!)

When you compare values (eg by reviewing them manually by diff'ing two log files) we should remember that digits 1-15 are ALWAYS valid, but differences in the 16th and 17th digits MAY be junk. 当你比较值时(例如通过差异化两个日志文件手动查看它们),我们应该记住数字1-15始终有效,但第16和第17位的差异可能是垃圾。

The '53' is the bit width of the significand that your type (double) holds. '53'是您的类型(double)所包含的有效数字的位宽。 The '15' is the number of decimal digits that can be represented safely with that kind of precision. '15'是可以用这种精度安全表示的小数位数。

digits10 is for conversion: string → double → string digits10用于转换:string→double→string
max_digits10 is for conversion: double → string → double max_digits10用于转换:double→string→double

In your program, you are using the conversion (double → string → double). 在您的程序中,您正在使用转换(双→字符串→双精度)。 You should use max_digits10 instead of digits10 . 您应该使用max_digits10而不是digits10


For more details about digits10 and max_digits10 , you can read: 有关digits10max_digits10更多详细信息,您可以阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM