在C ++中做数学运算时如何浮点错误传播? [英] How does floating point error propagate when doing mathematical operations in C++?

查看:100
本文介绍了在C ++中做数学运算时如何浮点错误传播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我们已经声明了以下变量

浮动= 1.2291;

浮动B = 3.99;

浮动变量有precision 6,它(如果我理解正确)表示数之间的差异,计算机实际存储和实际数字,你希望将小于 10 ^ -6

这意味着两个 A B 有一些误差小于 10 ^ -6

所以,在电脑里面 A 实际上可能是 1.229100000012123 B 可能是 3.9900000191919

现在让我们说,你有以下code

 浮法C = 0;
的for(int i = 0; I< 1000;我++)
      C + = A + B;

我的问题是,

C 的最终结果有precision误差小于 10 ^ -6 还有没有?

如果答案是否定的,我们如何才能真正知道这个precision错误,如果你使用任何类型的业务究竟发生,如你所愿多次,以任意顺序?


解决方案

  

浮点型变量有precision 6,它(如果我理解正确)表示数之间的区别在于,你要的实际上的电脑卖场和实际数目将少于10 ^ -6


  
  

这意味着,a和b有一些误差小于10 ^ -6


10 -6 数字是重新presenting任意常数作​​为花车的时候在相对的准确性粗略衡量。并非所有的数字会随着10 -6 的绝对误差psented重新$ P $。数8765432.1,例如,可以预期重新presented大约到装置。如果你是至少有一点点的幸运,你将重新presenting它作为一个浮动时,得到8765432。另一方面, 1E-15F 可以预期重新用的绝对误差psented $ P $至多约10 -21


  

所以,在电脑里面其实可以1.229100000012123和B可能是3.9900000191919


没有,不好意思,它的工作方式是不是你写的整个数字并添加六个零为可能出现的错误。可以通过从领先数字计数六个零,而不是最后的数字来估计的错误。在这里,你可以期望1.22910012123或3.990000191919。

(其实你会得到完全相同1.2290999889373779296875和3.9900000095367431640625。不要忘了,再presentation错误可以是负以及积极的,因为它是第一个数字。)


  

现在让我们说,你有以下code [...]


  
  

我的问题是,


  
  

C 的最终结果有precision误差小于10 ^ -6以及与否?


没有。总的绝对误差会全部重新presentation错误的总和 A B 为每个您使用千倍他们,再加上你做了2000增补的错误。这是4000不同的误差来源!他们中许多人将是相同的,他们中的一些会发生互相补偿,但最终的结果可能不会是10 -6 的相对精度,更像是10 -5 相对精度(建议不计数完成)。

Let's say that we have declared the following variables

float a = 1.2291;

float b = 3.99;

float variables have precision 6, which (if I understand correctly) means that the difference between the number that the computer actually stores and the actual number that you want will be less than 10^-6

that means that both a and b have some error that is less than 10^-6

so inside the computer a could actually be 1.229100000012123 and b could be 3.9900000191919

now let's say that you have the following code

float c = 0;
for(int i = 0; i < 1000; i++)
      c += a + b;

my question is,

will c's final result have a precision error that is less than 10^-6 as well or not?

and if the answer is negative, how can we actually know this precision error and what exactly happens if you apply any kind of operations, as many times you wish and in any order?

解决方案

float variables have precision 6, which (if I understand correctly) means that the difference between the number that the computer actually stores and the actual number that you want will be less than 10^-6

that means that both a and b have some error that is less than 10^-6

The 10-6 figure is a rough measure of the relative accuracy when representing arbitrary constants as floats. Not all numbers will be represented with an absolute error of 10-6. The number 8765432.1, for instance, can be expected to be represented approximately to the unit. If you are at least a little bit lucky, you will get 8765432 when representing it as a float. On the other hand, 1E-15f can be expected to be represented with an absolute error of at most about 10-21.

so inside the computer a could actually be 1.229100000012123 and b could be 3.9900000191919

No, sorry, the way it works is not that you write the entire number and add six zeroes for the possible error. The error can be estimated by counting six zeroes from the leading digit, not from the last digit. Here, you could expect 1.22910012123 or 3.990000191919.

(Actually you would get exactly 1.2290999889373779296875 and 3.9900000095367431640625. Don't forget that representation error can be negative as well as positive, as it is for the first number.)

now let's say that you have the following code […]

my question is,

will c's final result have a precision error that is less than 10^-6 as well or not?

No. The total absolute error will be the sum of all the representation errors for a and b for each of the thousand times you used them, plus the errors of the 2000 additions you did. That's 4000 different sources of error! Many of them will be identical, some of them will happen to compensate each other, but the end result will probably not be to 10-6 relative accuracy, more like 10-5 relative accuracy (suggestion done without counting).

这篇关于在C ++中做数学运算时如何浮点错误传播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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