浮点比较的奇怪结果 [英] Strange results with floating-point comparison

查看:107
本文介绍了浮点比较的奇怪结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的测试:

  double h; 
...
//分配h其初始值的代码,在下面使用
...
if((h> 0)&&(h < ){
//分支1 - 计算
}
else {
//分支2-无计算
}

我列出了我的值,因为我有一些非常奇怪的结果,例如如果:
h = 1那么第一个分支到达,不明白为什么,因为如果h = 1我想要branch2计算。

我很困惑的东西这么明显?






编辑:



这是我计算然后使用 h

  double * QSweep :: findIntersection(edge_t edge1,edge_t edge2){
point_t p1 = myPoints_ [edge1 [0]];
point_t p2 = myPoints_ [edge1 [1]];
point_t p3 = myPoints_ [edge2 [0]];
point_t p4 = myPoints_ [edge2 [1]];

double xD1,yD1,xD2,yD2,xD3,yD3,xP,yP,h,denom;
double * pt = new double [3];

//计算差异
xD1 = p2 [0] -p1 [0];
xD2 = p4 [0] -p3 [0];
yD1 = p2 [1] -p1 [1];
yD2 = p4 [1] -p3 [1];
xD3 = p1 [0] -p3 [0];
yD3 = p1 [1] -p3 [1];

xP = -yD1;
yP = xD1;
denom = xD2 *( - yD1)+ yD2 * xD1;
if(denom == 0){
return NULL;
}
else {
h =(xD3 *( - yD1)+ yD3 * xD1)/ denom;
}
std :: cout<<h is<< h<< endl;
if(h< 1)std :: cout<<no<< endl;
else std :: cout<<yes<< endl;
if(h == 1){
return NULL;
}
else {
if((h> 0)&&(h <1)){
pt [0] = p3 [0] + xD2 * h ;
pt [1] = p3 [1] + yD2 * h;
pt [2] = 0.00;
}
else {
return NULL;
}
}


return pt;

}






编辑:



好吧,所以很清楚如何重新拟定条件。



From:

  double h; 
if(h == 1){
//计算
}


$ b b

To:

  double h; 
if(abs(h-1)< tolerance){
//计算
}

$ b



但是如何重新格式化这个?

  double h; 
if(h <1){
//这里计算
}


c>> c>是一个双精度值,它可能已经足够接近1,打印为 1 ,但实际上它小于1,因此比较成功。 浮点数很多。


I have this simple test:

double h;
...
// code that assigns h its initial value, used below
...
if ((h>0) && (h<1)){
 //branch 1 -some computations
}
else{
 //branch 2- no computations
}

I listed my values as I got some really strange results and for example if: h=1 then the first branch is reached and I do not understand why since if h=1 I want branch2 to be computed.
Am I getting confused by something so obvious?


Edit:

This is how I compute and then use h:

double* QSweep::findIntersection(edge_t edge1,edge_t edge2) {  
point_t p1=myPoints_[edge1[0]];
point_t p2=myPoints_[edge1[1]];
point_t p3=myPoints_[edge2[0]];
point_t p4=myPoints_[edge2[1]];

double xD1,yD1,xD2,yD2,xD3,yD3,xP,yP,h,denom;
double* pt=new double[3];

// calculate differences  
xD1=p2[0]-p1[0];  
xD2=p4[0]-p3[0];  
yD1=p2[1]-p1[1];  
yD2=p4[1]-p3[1];  
xD3=p1[0]-p3[0];  
yD3=p1[1]-p3[1];    

xP=-yD1;
yP=xD1;
denom=xD2*(-yD1)+yD2*xD1;
if (denom==0) {
	return NULL;
}
else{
h=(xD3*(-yD1)+yD3*xD1)/denom;
}
std::cout<<"h is"<<h<<endl;
if (h < 1) std::cout<<"no"<<endl;
else std::cout<<"yes"<<endl;
if (h==1) {
	return NULL;
}
else{
if ((h>0)&&(h<1)){
	pt[0]=p3[0]+xD2*h;  
	pt[1]=p3[1]+yD2*h;
	pt[2]=0.00;
}
else{
	return NULL;
}
}


return pt;

}


Edit:

Okay, so it is clear how I should reformulate the condition.

From:

double h;
if (h==1){
   //computations here
}

To:

double h;
if (abs(h-1)<tolerance){
  //computations here
}

When I use double numbers.

But how do I reformulate this?

double h;
if (h<1){
   //computations here
}

解决方案

Since h is a double, it may have been close enough to 1 to print as 1, but it is actually a bit less than 1 so the comparison succeeds. Floating-point numbers do that a lot.

这篇关于浮点比较的奇怪结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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