简体   繁体   English

有没有办法确定两种语言中的哪一种计算更准确?

[英]Is there a way to determine which of two languages is calculating more accurately?

Situation: I'm running identical calculations in two separate languages (C++ & Scilab). 情况:我正在使用两种不同的语言(C ++和Scilab)运行相同的计算。 The results are varying slightly when using transcendental function like sin() and exp(). 使用诸如sin()和exp()之类的超越函数时,结果略有不同。 I'm assuming this small difference is due to the following: 我假设这种小的差异是由于以下原因:

The IEEE standard does not require transcendental functions to be exactly rounded because of the table maker's dilemma. 由于制表者的困境,IEEE标准不要求先验功能要精确四舍五入

Source: What Every Computer Scientist Should Know About Floating-Point Arithmetic . 资料来源: 每位计算机科学家都应该了解的浮点算法

Question: Is there a way for me to determine which (if either) of these languages is more accurate on my specific calculations, without manually performing the calculation myself? 问题:是否可以通过一种方法来确定这些语言中的哪一种(如果有的话)对我的特定计算更为准确, 而无需自己亲自执行计算?

I was thinking of simply putting the calculations into Wolfram Alpha and seeing which of the two holds accurately longer. 我当时只是想简单地将计算结果放入Wolfram Alpha中,然后看看两者中哪一个精确地保持更长的时间。 Unfortunately, I'm waiting on this question (about the accuracy of Wolfram Alpha) to be answered. 不幸的是,我正在等待这个问题(关于Wolfram Alpha的准确性)得到回答。

Note: After posting this question I'm still going to attempt the Wolfram way mentioned above, but is there some easier, more efficient way to determine which of the two languages is outputting more accurate results? 注意:发布此问题后,我仍将尝试上述的Wolfram方法,但是是否有一些更简单,更有效的方法来确定两种语言中的哪种语言输出的结果更准确?

Technically speaking, there is a preliminary discussion on this: precision is different than accuracy. 从技术上讲,对此进行了初步讨论:精度不同于精度。 Accuracy is a measurement of how close your calculation is to the true value, whatever it is; 准确性是衡量您的计算与真实值的接近程度,无论它是多少。 precision is a measurement of how close are many calculations to each other. 精度是对许多计算彼此之间的接近程度的度量。 You understand better if you think in terms of probability distribution of a set of calculation results. 如果您考虑一组计算结果的概率分布,您会更好地理解。 Therefore, you could have poor accuracy with high precision, for example. 因此,例如,您的精度可能会很差,而高精度。 The title of your post is about accuracy, but in the body you mention precision. 您的帖子标题与准确性有关,但在正文中您提到了准确性。 I guess you meant accuracy . 我想你的意思是准确性

That said, there is no general answer. 也就是说,没有普遍的答案。 There are many methods you could use, actually infinite algorithms could fit your needs. 您可以使用许多方法,实际上无限算法可以满足您的需求。 You need: 你需要:

  • a library like GMP, as already suggested 像GMP这样的库
  • paper and pencil to evaluate the accuracy of your algorithm 纸和铅笔来评估算法的准确性

Why this? 为什么这个? You need to evaluate your sin(x) with higher accuracy than the two results you want to compare. 您需要比要比较的两个结果更高的准确度来评估sin(x) And algorithms for transcendental functions are series. 超越函数的算法是系列的。 So you have to estimate how the algorithm error - and in some cases, still the representation errors - influence you calculation. 因此,您必须估算算法错误-在某些情况下,仍然是表示错误-对计算的影响。 At that point, implement the method of your choice, and evaluate the distance between your result and the other two, being sure that the total error affecting your result is however smaller than some controllable constant. 此时,实施您选择的方法,并评估结果与其他两个方法之间的距离,并确保影响结果的总误差小于某个可控常数。

As an example, evaluating exp(10) can be done using the usual exponential Taylor expansion around zero, and using many terms; 例如,可以使用通常在零附近的泰勒展开式并使用许多项来完成对exp(10)的求值。 but if you use the same method for exp(-10) , you may fail, as its expansion has alternating sign terms. 但是如果对exp(-10)使用相同的方法,则可能会失败,因为其扩展具有交替的符号项。 This means that you may have cancellation effects that could accumulate. 这意味着您可能会积累一些抵消效果。 You do not want this, otherwise you may be precise, but biased, inaccurate in your calculation. 您不希望这样做,否则您的计算可能很精确但有偏差。 Usually GMP is used because it is designed for this. 通常使用GMP,因为它是为此目的而设计的。

So, for example, you may discover that Scilab is best in calculating exp(-10) , and C++ in exp(10) , just because they could use different implementations. 因此,例如,您可能会发现Scilab在计算exp(-10)和C ++在exp(10)是最好的,因为它们可以使用不同的实现。

In summary: use GMP, and look for an accurate algorithm. 总结:使用GMP,并寻找一种准确的算法。 This may work, or be the only solution, if you need an accurate result of some particular Fourier summation, or a series in which other transcendental terms have to be evaluated, for example. 例如,如果您需要某些特定傅立叶求和的准确结果,或者需要评估其他先验项的序列,则这可能是唯一的解决方案。 But if you just need this answer for simple sin and exp , you should look for tables, and that's it. 但是,如果您只需要简单的sinexp这个答案,就应该寻找表格,仅此而已。

If you want consistent accuracy of transcendental math functions I suggest you write your own. 如果您希望先验数学函数具有一致的准确性,建议您编写自己的函数。

There are various methods for implementing functions such as sin . 有多种实现诸如sin功能的方法。 Some use a table of values (eg small embedded systems platforms) to hardware calculations. 有些使用值表(例如小型嵌入式系统平台)进行硬件计算。

The implementation of the transcendental math functions is usually dependent on the language implementor. 先验数学函数的实现通常取决于语言实现者。

BTW, the general implementation is a middle ground between space, speed and accuracy. 顺便说一句,一般的实现是空间,速度和准确性之间的中间立场。 A highly accurate version may require a lot of space or a lot of time. 高度准确的版本可能需要大量空间或大量时间。

At least if you have you own implementation, you can control the accuracy rather than guessing at the accuracy of a language's implementation. 至少如果您拥有自己的实现,则可以控制准确性,而不用猜测语言实现的准确性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM