分割双精度时出现意外的精度损失 [英] Unexpected loss of precision when dividing doubles

查看:189
本文介绍了分割双精度时出现意外的精度损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数getSlope作为参数4加倍,并返回另一个double计算使用这个给定的参数以下面的方式:

  double QSweep :: getSlope(double a,double b,double c,double d){
double slope;
slope =(d-b)/(c-a);
return slope;问题是当使用参数调用此函数时,例如:

  getSlope(2.71156,-1.64161,2.70413,-1.72219); 

返回的结果是:

  10.8557 

这不是一个好的结果。
我使用Mathematica计算了斜率,相同参数的斜率结果是:

  10.8452 

或更精确的数字:

  10.845222072678331。 

我的程序返回的结果在我的进一步计算中不好。
此外,我不知道该程序如何从10.845222072678331(假设这是除法的近似结果)返回10.8557?
如何获得我的部门的好结果?



提前谢谢,
madalina






我使用命令行打印结果:

  std :: cout<<< slope<< endl; 

这可能是因为我的参数可能不好,因为我从另一个程序一个图形;在我从他的图中读取这些参数之后,我刚刚显示它们以查看它们的值,但是可能显示的向量对于计算的值具有不同的内部精度。我不知道它是很奇怪的。 ..)



当我读取我的参数的图形被计算时,使用用C ++(使用模板)编写的一些数字库。此计算不使用OpenGL。



谢谢你,
madalina

解决方案

ve尝试与float而不是双,我得到10.845110作为结果。它仍然看起来比madalina结果更好。



编辑:



我想我知道你为什么得到这个结果。如果你从别的地方得到a,b,c和d参数,你打印它,它给你四舍五入的值。如果你把它放到Mathemtacia(或calc;)),它会给你不同的结果。



我尝试改变一点参数。当我这样做:

  double c = 2.7041304; 

我得到10.845806。我只添加0.0000004到c!
所以我认为你的错误不是错误。以更好的精度打印a,b,c和d,然后将它们放到Mathematica。


I have a function getSlope which takes as parameters 4 doubles and returns another double calculated using this given parameters in the following way:

double QSweep::getSlope(double a, double b, double c, double d){
double slope;
slope=(d-b)/(c-a);
return slope;
}

The problem is that when calling this function with arguments for example:

getSlope(2.71156, -1.64161, 2.70413, -1.72219);

the returned result is:

10.8557

and this is not a good result for my computations. I have calculated the slope using Mathematica and the result for the slope for the same parameters is:

10.8452

or with more digits for precision:

10.845222072678331.

The result returned by my program is not good in my further computations. Moreover, I do not understant how does the program returns 10.8557 starting from 10.845222072678331 (supposing that this is the approximate result for the division)? How can I get the good result for my division?

thank you in advance, madalina


I print the result using the command line:

std::cout<<slope<<endl;

It may be that my parameters are maybe not good, as I read them from another program (which computes a graph; after I read this parameters fromt his graph I have just displayed them to see their value but maybe the displayed vectors have not the same internal precision for the calculated value..I do not know it is really strange. Some numerical errors appears..)

When the graph from which I am reading my parameters is computed, some numerical libraries written in C++ (with templates) are used. No OpenGL is used for this computation.

thank you, madalina

解决方案

I've tried with float instead of double and I get 10.845110 as a result. It still looks better than madalina result.

EDIT:

I think I know why you get this results. If you get a, b, c and d parameters from somewhere else and you print it, it gives you rounded values. Then if you put it to Mathemtacia (or calc ;) ) it will give you different result.

I tried changing a little bit one of your parameters. When I did:

double c = 2.7041304;

I get 10.845806. I only add 0.0000004 to c! So I think your "errors" aren't errors. Print a, b, c and d with better precision and then put them to Mathematica.

这篇关于分割双精度时出现意外的精度损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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