Visual C++中双精度类型的数值精度2008 快速调试器

发布于 2024-08-26 06:01:31 字数 367 浏览 6 评论 0原文

我正在使用 Visual C++ 2008 Express Edition,当我调试代码时:

double x = 0.2;

我在 x 0.20000000000000001 上的调试工具提示中看到

,但是:

typedef numeric_limits< double > double_limit; 
int a = double_limit::digits10

给了我: a = 15

为什么调试器中的结果比正常的 c++ 精度长? 这种奇怪的精度是基于什么?

我的CPU是Intel Core 2 Duo T7100

I'm using Visual C++ 2008 Express Edition and when i debug code:

double x = 0.2;

I see in debugging tooltip on x 0.20000000000000001

but:

typedef numeric_limits< double > double_limit; 
int a = double_limit::digits10

gives me: a = 15

Why results in debugger are longer than normal c++ precision?
What is this strange precision based on?

My CPU is Intel Core 2 Duo T7100

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

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

发布评论

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

评论(3

此岸叶落 2024-09-02 06:01:31

您所看到的情况是由于实数(读取浮点数)无法在二进制计算机中以完美的精度和准确度表示的事实造成的。这是生活的事实。相反,计算机近似该值并以定义的格式将其存储在内存中。

对于大多数现代计算机(包括运行 MSVC Express 的任何计算机),此格式为 IEEE 754

长话短说,这就是实数在 IEEE 754 中的存储方式:有 1 个符号位、8 个指数位和 23 个小数位(对于 float 数据类型 - double相应地使用更多位,但格式相同)。因此,您永远无法达到完美的精度和准确度。幸运的是,您几乎可以为任何应用程序(包括关键的金融系统和科学系统)实现足够的准确性和精度。

您无需了解有关 IEEE754 的所有知识即可在代码中使用浮点。但有一些事情您必须知道:

1) 由于浮点计算和计算中固有的舍入误差,您永远无法比较 2 个浮点值是否相等。贮存。相反,您必须执行以下操作:

double d = 0.2;
double compare = 0.000000001;

double d2 = something;

if( (d - d2 < compare) && (d2 - d < compare) )
{
  // numbers are equal
}

2) 舍入误差复合。对浮点值执行运算的次数越多,精度损失就越大。

3) 不能将两个大小相差很大的浮点数相加。例如,您不能将 1.5x10^30 和 1.5x10^-30 加起来并期望 60 位精度。

What you are seeing is caused by the fact that real numbers (read floating-point) cannot be expressed with perfect precision and accuracy in binary computers. This is a fact of life. Instead, computers approximate the value and store it in memory in a defined format.

In the case of most modern machines (including any machine your'e running MSVC Express on), this format is IEEE 754.

Long story short, this is how real numbers are stored in IEEE 754: there is one sign bit, 8 exponent bits and 23 fraction bits (for float data type -- doubles use more bits accordingly but the format is the same). Because of this, you can never achieve perfect precision and accuracy. Fortunately you can achieve plenty of accuracy and precision for almost any application including critical financial systems and scientific systems.

You don't need to know everything there is to know about IEEE754 in order to be able to use floating-points in your code. But there are a few things you must know:

1) You can never compare 2 floating point values for equality because of the rounding error inherent in floating point calulation & storage. Instead, you must do something like this:

double d = 0.2;
double compare = 0.000000001;

double d2 = something;

if( (d - d2 < compare) && (d2 - d < compare) )
{
  // numbers are equal
}

2) Rounding errors compound. The more times you perform operations on a floating point value, the greater the loss of precision.

3) You cannot add two floating points of vastly different magnitude. For example, you can't add 1.5x10^30 and 1.5x10^-30 and expect 60 digits of precision.

追风人 2024-09-02 06:01:31

双文本 0.2 给出的确切值是 0.200000000000000011102230246251565404236316680908203125。

大多数输出​​双精度数的函数都会在一定数量的小数位后进行截断,这就是为什么您会产生 0.2 实际上产生 0.2 的错觉,

以下是我获得准确值的方法:

public static void main(String[] args)
{
    System.out.println(new java.math.BigDecimal(0.2));
}

The exact value that the double literal 0.2 gives you is 0.200000000000000011102230246251565404236316680908203125.

Most functions that output doubles cut after a certain amount of decimal digits, that's why you are under the illusion that 0.2 actually yields 0.2

Here is how I got the exact value:

public static void main(String[] args)
{
    System.out.println(new java.math.BigDecimal(0.2));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文