numeric_limits::digits10 的含义是什么
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
numeric_limits::digits10
是可以无损保留的小数位数。例如,
numeric_limits::digits10
为 2。这意味着 unsigned char 可以保存 0..99 而不会丢失。 如果是 3,它可以容纳 0..999,但众所周知,它只能容纳 0..255。本手册页有一个浮点数示例,(当缩短时) ) 显示
打印
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
prints
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.
请参阅这篇非常可读的论文:
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.
'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.
digits10
用于转换: string → double → stringmax_digits10
用于转换:double → string → double在您的程序中,您正在使用转换(double → string → double)。 您应该使用
max_digits10
而不是digits10
。有关
digits10
和max_digits10
的更多详细信息,您可以阅读:digits10
is for conversion: string → double → stringmax_digits10
is for conversion: double → string → doubleIn your program, you are using the conversion (double → string → double). You should use
max_digits10
instead ofdigits10
.For more details about
digits10
andmax_digits10
, you can read: