numeric_limits::digits10 的含义是什么

发布于 2024-07-16 17:43:49 字数 926 浏览 8 评论 0原文

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;
}

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

  • The following prototype starts working (sucess is true) when precision is greater that 17 ( == 2+numeric_limits::digits10)
  • With STLPort, readDouble==infinity at the end; with microsoft's 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情话已封尘 2024-07-23 17:43:49

numeric_limits::digits10 是可以无损保留的小数位数。

例如,numeric_limits::digits10 为 2。这意味着 unsigned char 可以保存 0..99 而不会丢失。 如果是 3,它可以容纳 0..999,但众所周知,它只能容纳 0..255。

本手册页有一个浮点数示例,(当缩短时) ) 显示

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

numeric_limits::digits10 is the number of decimal digits that can be held without loss.

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;

prints

6
The float is; 100000000
人间不值得 2024-07-23 17:43:49

numeric_limits::digits10 指定在不损失精度的情况下可以表示的小数点左边的小数位数。 每种类型都有不同数量的可表示的十进制值。

numeric_limits::digits10 specifies the number of decimal digits to the left of the decimal point you can represent without a loss of precision. Each type will have a different number of representable decimal values.

所谓喜欢 2024-07-23 17:43:49

请参阅这篇非常可读的论文:
http://www.open-std.org/jtc1/sc22/ wg21/docs/papers/2006/n2005.pdf

虽然 DBL_MAX ( = std::numeric_limits::digits10 = 15 位数字) 是双精度数的最小保证位数,但建议使用 DBL_MAXDIG10 值 (= 17 位数字)论文中的 具有有用的属性:

  • 是从字符串形式往返并最终获得相同的双精度所需的最小位数。

  • 是转换双精度数所需的最少位数
    转换为字符串形式,并在代码中每次得到 (A != B) 时显示不同的字符串。
    使用 16 位或更少的数字,您可以获得代码中不相等的双精度数,
    但是当它们转换为字符串形式时它们是相同的
    (这将给出在代码中比较时它们不同的情况,
    但日志文件会将它们显示为相同的 - 非常混乱且难以调试!)

当您比较值时(例如,通过比较两个日志文件来手动检查它们),我们应该记住数字 1-15 始终有效,但存在差异第 16 和第 17 位可能是垃圾。

See this very readable paper:
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:

  • 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.
    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!)

When you compare values (e.g. 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.

策马西风 2024-07-23 17:43:49

'53' 是您的类型(双精度)所保存的 有效数 的位宽度。 “15”是可以以这种精度安全表示的十进制位数。

The '53' is the bit width of the significand that your type (double) holds. The '15' is the number of decimal digits that can be represented safely with that kind of precision.

窝囊感情。 2024-07-23 17:43:49

digits10 用于转换: string → double → string
max_digits10 用于转换:double → string → double

在您的程序中,您正在使用转换(double → string → double)。 您应该使用 max_digits10 而不是 digits10

有关 digits10max_digits10 的更多详细信息,您可以阅读:

digits10 is for conversion: string → double → string
max_digits10 is for conversion: double → string → double

In your program, you are using the conversion (double → string → double). You should use max_digits10 instead of digits10.

For more details about digits10 and max_digits10, you can read:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文