订购操作最大化双精度 [英] Ordering operation to maximize double precision

查看:218
本文介绍了订购操作最大化双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些可以在最坏情况下计算出可以接近 1e-25 的数据的工具,并将它们与Java进行比较。我显然使用双精度。



我已阅读另一个答案我不应该期望超过 1e-15 1e-17 precision,这个其他问题处理在以更好的顺序订购操作时获得更高的精度。 / p>

哪些双精度操作更趋向于精确度偏差?我应该尝试尽可能大的数字尽可能的数字吗?在进行乘法处理之前首先进行分割?



我宁愿不使用 BigDecimal 类或等价物,因为代码已经够慢了;)(除非他们不要影响速度太多,当然)。



任何信息将不胜感激!



EDIT :绝对值(1e-25)数字小的事实无关紧要,双重可以下降到1e-324。但重要的是,当它们非常相似(两者都在1e-25)时,我必须比较,我们来说说,4.64563824048517606458e-21到4.64563824048517606472e-21(区别在于第19和第20位数)。当计算这些数字时,区别是如此之小,以至于我可能会遇到舍入误差,其余的是填充随机数。



问题是:如何排序计算,以减少精度损失?。它可能是在乘法之前进行分割,或者先添加。

解决方案

感谢@John指出一个关于浮点运算的非常完整的文章。



事实证明,如果需要精度,应该重新排序操作,并且配置适合于避免精度损失的公式,如取消章:当比较彼此非常接近(这是我的情况)的数字时,灾难性的取消可能会发生,导致精度的巨大损失。通常,根据您的操作数值的知识,重新编写公式或重新排序操作可能会导致更高的微积分精度。



我从这篇文章中记住的是:




  • 在减少两个几乎相同的数量时要小心

  • 尝试重新安排操作以避免灾难性取消



对于后一种情况,请记住,计算(x - y)*(x + y)给出比 x * x - y * y 更准确的结果。 p>

I'm working on some tool that gets to compute numbers that can get close to 1e-25 in the worst cases, and compare them together, in Java. I'm obviously using double precision.

I have read in another answer that I shouldn't expect more than 1e-15 to 1e-17 precision, and this other question deals with getting better precision when ordering operations in a "better" order.

Which double precision operations are more keen to loose precision along the way? Should I try to work with number as big as possible or as small as possible? Do divisions first before multiplications?

I'd rather not use the BigDecimal classes or equivalent, as the code is already slow enough ;) (unless they don't impact speed too much, of course).

Any information will be greatly appreciated!

EDIT: The fact that numbers are "small" in absolute value (1e-25) does not matter, as double can go down to 1e-324. But what matters is that, when they are very similar (both in 1e-25), I have to compare, let's say 4.64563824048517606458e-21 to 4.64563824048517606472e-21 (difference is the 19th and 20th digits). When computing these numbers, the difference is so small that I might hit the "rounding error", where remainder is filled with random numbers.

The question is: "how to order computation so that this loss of precision is minimized?". It might be doing divisions before multiplications, or additions first.

解决方案

Thanks to @John for pointing out a very complete article about floating point arithmetics.

It turns out that, when precision is needed, operations should be re-ordered, and formulas adapted to avoid loss of precision, as explained in the Cancellation chapter: when comparing numbers that are very close to each other (which is my case), "catastrophic cancellation" may occur, inducing a huge loss of precision. Often, re-writing the formula, or re-ordering operations according to your à-priori knowledge of the operands values can lead to achieving greater accuracy in calculus.

What I'll remember from this article is:

  • be careful when substracting two nearly-identical quantities
  • try to re-arrange operations to avoid catastrophic cancellation

For the latter case, remember that computing (x - y) * (x + y) gives more accurate results than x * x - y * y.

这篇关于订购操作最大化双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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