Order of operations to maximize precision

此生再无相见时 提交于 2020-01-09 11:57:09

问题


I'm using floats for these operations:

Which of these two is more precise?

  • (a * b) / c

or

  • (a / c) * b

Does it matter at all or does it depend on the situation? If so, which should I choose in what cases?


回答1:


Really, if you don't use double then you are misguided, and you don't care about precision.

Otherwise, you get the best error bounds if the first result is slightly lower than the next higher power of two. For example, calculating (pi * e) / sqrt (2), you get the best error bounds by calculating (e / sqrt (2)) * pi, because e / sqrt (2) ≈ 1.922 is close below 2. Results close to the next higher power of two have a lower relative error.

For addition and subtraction of a large number of items, it's best to first subtract items of equal magnitude and opposite sign (x - y is calculated exactly if y/2 ≤ x ≤ 2y), and otherwise combining numbers giving the smallest possible results.



来源:https://stackoverflow.com/questions/45524072/order-of-operations-to-maximize-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!