c++数值分析 准确的数据结构?
使用双类型我制作了三次样条插值算法。 这项工作看起来很成功,但是当计算非常小的值时,相对误差约为 6%。
双数据类型足以进行精确的科学数值分析吗?
Using double type I made Cubic Spline Interpolation Algorithm.
That work was success as it seems, but there was a relative error around 6% when very small values calculated.
Is double data type enough for accurate scientific numerical analysis?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于大多数应用来说,Double 具有足够的精度。当然它是有限的,但使用糟糕的算法总是可能浪费任何精度。事实上,这应该是你的第一个嫌疑人。仔细检查你的代码,看看你是否正在做一些让舍入误差累积得比必要的更快的事情,或者做一些危险的事情,比如减去彼此非常接近的值。
Double has plenty of precision for most applications. Of course it is finite, but it's always possible to squander any amount of precision by using a bad algorithm. In fact, that should be your first suspect. Look hard at your code and see if you're doing something that lets rounding errors accumulate quicker than necessary, or risky things like subtracting values that are very close to each other.
科学的数值分析很难做到正确,这就是为什么我把它留给专业人士。您是否考虑过使用数字库而不是自己编写? Eigen 是我目前最喜欢的: http://eigen.tuxfamily.org/index.php? title=Main_Page
我手边总是有最新的《数值食谱》(nr.com),其中有关于插值的精彩章节。 NR 拥有限制性许可,但作者知道他们在做什么,并针对每种数值技术提供了简洁的文章。其他值得关注的库包括:ATLAS 和 GNU Scientific Library。
为了回答你的问题,对于大多数科学应用来说, double 应该绰绰有余,我同意之前的海报,它应该像一个算法问题。您是否考虑过发布您正在使用的算法的代码?
Scientific numerical analysis is difficult to get right which is why I leave it the professionals. Have you considered using a numeric library instead of writing your own? Eigen is my current favorite here: http://eigen.tuxfamily.org/index.php?title=Main_Page
I always have close at hand the latest copy of Numerical Recipes (nr.com) which does have an excellent chapter on interpolation. NR has a restrictive license but the writers know what they are doing and provide a succinct writeup on each numerical technique. Other libraries to look at include: ATLAS and GNU Scientific Library.
To answer your question double should be more than enough for most scientific applications, I agree with the previous posters it should like an algorithm problem. Have you considered posting the code for the algorithm you are using?
double 是否足以满足您的需求取决于您正在使用的数字类型。正如亨宁建议的那样,最好检查一下您正在使用的算法并确保它们在数值上稳定。
对于初学者来说,这里有一个很好的加法算法:Kahan 求和算法。
If double is enough for your needs depends on the type of numbers you are working with. As Henning suggests, it is probably best to take a look at the algorithms you are using and make sure they are numerically stable.
For starters, here's a good algorithm for addition: Kahan summation algorithm.
双精度最适合任何问题,但如果多项式或函数快速振荡或重复或具有相当高的维度,则三次样条将无法正常工作。
在这种情况下,最好使用勒让德多项式,因为它们可以处理指数的变体。
举一个简单的例子,如果您使用欧拉、梯形或辛普森规则在三阶多项式中进行插值,则不需要很大的采样率来获得插值(曲线下的面积)。但是,如果将这些应用于指数函数,则可能需要大幅提高采样率以避免损失大量精度。勒让德多项式可以更轻松地满足这种情况。
Double precision will be mostly suitable for any problem but the cubic spline will not work well if the polynomial or function is quickly oscillating or repeating or of quite high dimension.
In this case it can be better to use Legendre Polynomials since they handle variants of exponentials.
By way of a simple example if you use, Euler, Trapezoidal or Simpson's rule for interpolating within a 3rd order polynomial you won't need a huge sample rate to get the interpolant (area under the curve). However, if you apply these to an exponential function the sample rate may need to greatly increase to avoid loosing a lot of precision. Legendre Polynomials can cater for this case much more readily.