gpt4 book ai didi

c++ - 电脑精度: when should I have to worry about it?

转载 作者:行者123 更新时间:2023-11-30 01:22:26 29 4
gpt4 key购买 nike

在C++编程中,什么时候需要担心精度问题?举个小例子(虽然可能不完美),

std::vector<double> first (50000, 0.0);
std::vector<double> second (first);

有没有可能second[619] = 0.00000000000000000000000000001234 (我的意思是一个非常小的值)。或者 SUM = second[0]+second[1]+...+second[49999] => 1e-31 ?或者 SUM = second[0]-second[1]-...-second[49999] => -7.987654321e-12

我的问题:

  1. 在使用 double 时会不会遇到一些小干扰?键入数字?
  2. 是什么导致了这些小干扰?即舍入误差变大?你能列出来吗?如何采取预防措施?
  3. 如果在某些操作中可能会有小干扰,那么是否意味着在这些操作之后,使用 if (SUM == 0) 危险吗?人们应该总是使用 if (SUM < SMALL)相反,哪里 SMALL被定义为一个非常小的值,例如1E-30
  4. 最后,小干扰会导致负值吗?因为如果可能的话,我应该更好地使用 if (abs(SUM) < SMALL)相反。

有什么经验吗?

最佳答案

这是一个很好的浮点精度引用文档:What Every Computer Scientist Should Know About Floating-Point Arithmetic

其中一个更重要的部分是灾难性取消

Catastrophic cancellation occurs when the operands are subject to rounding errors. For example in the quadratic formula, the expression b2 - 4ac occurs. The quantities b2 and 4ac are subject to rounding errors since they are the results of floating-point multiplications. Suppose that they are rounded to the nearest floating-point number, and so are accurate to within .5 ulp. When they are subtracted, cancellation can cause many of the accurate digits to disappear, leaving behind mainly digits contaminated by rounding error. Hence the difference might have an error of many ulps. For example, consider b = 3.34, a = 1.22, and c = 2.28. The exact value of b2 - 4ac is .0292. But b2 rounds to 11.2 and 4ac rounds to 11.1, hence the final answer is .1 which is an error by 70 ulps, even though 11.2 - 11.1 is exactly equal to .16. The subtraction did not introduce any error, but rather exposed the error introduced in the earlier multiplications.

Benign cancellation occurs when subtracting exactly known quantities. If x and y have no rounding error, then by Theorem 2 if the subtraction is done with a guard digit, the difference x-y has a very small relative error (less than 2).

A formula that exhibits catastrophic cancellation can sometimes be rearranged to eliminate the problem. Again consider the quadratic formula

关于c++ - 电脑精度: when should I have to worry about it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16345385/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com