This question already has answers here:
How does this program work?
                                
                                    (13个回答)
                                
                        
                        
                            Detecting and adjusting for negative zero
                                
                                    (5个答案)
                                
                        
                                5年前关闭。
            
                    
我正在研究确定圆和射线是否相交的函数。该函数的基础来自this website特别是this page。在作者讨论圆与射线相交的第一页的注释中,他列出了有关确定圆与射线是否相交的内容。


  确切的行为由正方形内的表达式确定
  根b * b-4 * a * c
  
  
  如果小于0,则该行不
  与球体相交。
  如果等于0,则该线是与球相交的切线
  它在某一点,即u = -b / 2a。
  如果大于0,则线与球在两个点处相交。
  


这就是我的代码中的问题所在。它位于下面的函数中double bb4ac的比较。

bool CircleRayIntersect(double ip1_x, double ip1_y, double ip2_x, double ip2_y,
                                double isc_x, double isc_y, double isc_r,
                                double &out1_x, double &out1_y,
                                double &out2_x, double &out2_y)
{
    double a,b,c;
    double bb4ac;
    double r = isc_r;
    double dp_x;
    double dp_y;
    double mu_1;
    double mu_2;
    double p1_x = ip1_x;
    double p1_y = ip1_y;
    double p2_x = ip2_x;
    double p2_y = ip2_y;
    double sc_x = isc_x;
    double sc_y = isc_y;

    dp_x = p2_x - p1_x;
    dp_y = p2_y - p1_y;
    a = dp_x * dp_x + dp_y * dp_y;
    b = 2 * (dp_x * (p1_x - sc_x) + dp_y * (p1_y - sc_y));
    c = sc_x * sc_x + sc_y * sc_y ;
    c += p1_x * p1_x + p1_y * p1_y;
    c -= 2 * (sc_x * p1_x + sc_y * p1_y);
    c -= r * r;
    bb4ac = b * b - 4 * a * c;

    // Checks to make sure that the line actually intersects
    TRACE("  -- Checking     a: %f\n", a);
    TRACE("  -- Checking bb4ac: %f\n", bb4ac);

    if (abs(a) < 1E-9 || bb4ac < 0.0)
    {

        if(bb4ac < 0.0)
        {
            TRACE("bb4ac is less than zero: %d < 0.0 = %d\n", bb4ac, (bb4ac < 0.0));
            TRACE("bb4ac is less than zero: %f < 0.0 = %f\n", bb4ac, (bb4ac < 0.0));
        }
        if (abs(a) < 1E-9)
            TRACE("abs(a) is less than zero\n");

        mu_1 = 0;
        mu_2 = 0;
        TRACE("Ray does not intersect with circle!\n");
        return FALSE;
    }

    mu_1 = (-b + sqrt(bb4ac)) / (2 * a);
    mu_2 = (-b - sqrt(bb4ac)) / (2 * a);

    out1_x = p1_x + (mu_1*(p2_x-p1_x));
    out1_y = p1_y + (mu_1*(p2_y-p1_y));
    out2_x = p1_x + (mu_2*(p2_x-p1_x));
    out2_y = p1_y + (mu_2*(p2_y-p1_y));

    return TRUE;
}


在某些时候,当使用某些参数调用此代码时,bb4ac最终等于-0.0。发生这种情况时,对照上述规则对bb4ac进行检查的操作将失败。这是TRACEbb4ac-0.0语句的一些示例输出。

  -- Checking     a: 129.066667
  -- Checking bb4ac: -0.000000
bb4ac is less than zero: 0 < 0.0 = -1114636288
bb4ac is less than zero: -0.000000 < 0.0 = 0.000000


TRACE的输出判断,看起来将if(..)bb4ac进行比较的0语句无法正确解释bb4ac。看到用bb4ac标志解释时-1114636288被打印为%d,而用-0.000标志解释时%f被打印为bb4ac,我不得不认为-1114636288if语句而不是-0.000

如何编写if语句以正确地将bb4ac解释为-0.000?为什么首先看到它呢?

最佳答案

假设TRACE是类似printf的varargs函数,将%ddouble参数一起使用,或将%fintbool参数一起使用是未定义的行为。使用正确的格式说明符或将参数转换为正确的类型。

09-06 13:52