0

我正在修补一个投影仪问题,我遇到了以下非常奇怪的行为。而且我一生都无法弄清楚发生了什么。

截屏

如屏幕截图所示。该条件的计算结果为假,并且 if 语句就好像它为真一样执行。

我觉得我在这里要疯了。

更新代码:http: //ideone.com/L7KMu

编辑:条件之前的 next.x 和 next.y 的实际值为 0.324583、9.97891

在 if 语句之后没有变化。

4

2 回答 2

6

编译代码后,您调用的 abs 似乎不是 std::abs,为浮点重载,而是整数 abs。尝试对代码进行此修改:

// get the root not relating to this point
nextX = (abs(solved.first - next.x) < EPSILON) ? solved.second : solved.first;
nextY = k * nextX + m;
prev = next;
next = point(nextX, nextY);

cout << abs(next.x) << ", " << std::abs(next.x) << endl;
if ( std::abs(next.x) < 0.01){
    cout << "Here1" << endl;
    if ( next.y > 0){
        cout << "Here2" << endl;
        foundExit = true;
    }
}

您会注意到 cout 行正在为 abs 和 std::abs 打印不同的值。

于 2012-07-08T16:49:43.743 回答
2

abs(next.x) 被评估为 0,因为它介于 0 和 1 之间。这种舍入将取决于所使用的编译器。

于 2012-07-08T16:50:46.163 回答