简体   繁体   English

C ++浮点比较

[英]C++ floating point comparison

Suppose you have have a rectangle, bottom-left point 0,0 and upper-right point is 100,100. 假设您有一个矩形,左下角为0,0,右上角为100,100。 Now two line intersects the rectangle. 现在,两条线与矩形相交。 I have to find out the coordinate of the intersection point. 我必须找出交点的坐标。 I have done that. 我已经做到了。 Now the problem is I can't tell whether it is inside the rectangle or not. 现在的问题是我不知道它是否在矩形内。 I used double comparison. 我使用了双重比较。 But I think it is giving me wrong answer. 但是我认为这给了我错误的答案。 Suppose the intersection point is ( x , y ). 假设交点为(x,y)。 I used this checking for comparison : if( x >= 0.0 && x <= 100.0 && y >= 0.0 && y <= 100.0 ). 我使用此检查进行比较:if(x> = 0.0 && x <= 100.0 && y> = 0.0 && y <= 100.0)。 What should I do? 我该怎么办?

//this function generates line
line genline( int x1 , int y1 , int x2 , int y2 ){
    line l ;
    l.A = y2 - y1 ;
    l.B = x1 - x2 ;
    l.C = l.A * x1 + l.B * y1 ;
    return l ;
}
//this function checks intersection
bool intersect( line m ,line n ) {
    int det = m.A * n.B - m.B * n.A ;
    if( det == 0 ){
        return false ;
    }
    else {
        double x = ( n.B * m.C - m.B * n.C ) / ( det * 1.0 ) ;
        double y = ( m.A * n.C - n.A * m.C ) / ( det * 1.0 ) ;        
        if( x >= 0.0 && x <= L && y >= 0.0 && y <= W ) { return true ; }
        else{ return false ; }
    }
}

EDIT: Both the line are stretched to infinity. 编辑:两条线都延伸到无穷大。

Your math looks like it's right. 您的数学看起来很正确。 By the way, If a line intersects something, it is always inside that something. 顺便说一句,如果一条线与某物相交,则它总是在该物内。

Checking to see if a point is inside a rectangle is relatively easy. 检查点是否在矩形内比较容易。 However, the challenge is to find the intersection between two line segments. 但是,挑战在于找到两条线段之间的交点。 There are a large number of corner cases to that problem and limited accuracy of floating point numbers play a huge roll here. 这个问题有很多极端的情况,并且浮点数的有限精度在这里起了很大的作用。

Your algorithm seems to be overly simplistic. 您的算法似乎过于简单。 For a deeper discussion about this topic you can look at this and this . 有关此主题的更深入讨论,您可以查看thisthis This two parts article investigates the problem of finding the intersection of two lines using floating point numbers. 这两个部分的文章探讨了使用浮点数查找两条线的交点的问题。 Notice that they are about MATLAB not C++ though that does not change the problem and the algorithms are easily translatable to any language. 请注意,尽管它们不会改变问题,并且算法很容易转换为任何语言,但它们都是关于MATLAB而非C ++的。

Depending on application, even with clever tricks floating point representation might not simply cut it for some geometry problems. 根据应用程序的不同,即使有一些巧妙的技巧,浮点表示也可能不会因为某些几何问题而简单地切掉。 CGAL is a C++ library dedicated to computational geometry that deals with these kind problems. CGAL是致力于处理几何的C ++库,用于处理这些类型的问题。 When necessary it uses arbitrary precision arithmetic to handle degenerate cases. 必要时,它使用任意精度算术来处理退化的情况。

When you're dealing with floating point (or double), testing for equality is naïve and will fail in edge cases. 当您处理浮点数(或双精度数)时,对相等性的测试很幼稚,在极端情况下会失败。 Every comparison you make should be in reference to "epsilon", an extremely small quantity that doesn't matter. 您所做的每个比较都应参考“ε”,这无关紧要。 If two numbers are within epsilon for each other, then they are considered equal. 如果两个数字在ε内,则认为它们相等。

For example, instead of "if(a == b)", you need: 例如,代替“ if(a == b)”,您需要:

bool isEqual(double a, double b, double epsilon = 1.E-10)
{    return fabs(a - b) <= epsilon;
}

Pick a suitable value for epsilon depending on your problem domain. 根据您的问题域,为epsilon选择一个合适的值。

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

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