gpt4 book ai didi

c++ - 条件为假时执行的 if 语句

转载 作者:太空宇宙 更新时间:2023-11-04 15:07:13 26 4
gpt4 key购买 nike

我正在修补一个 projecteuler 问题,我遇到了以下非常奇怪的行为。我这辈子都搞不清楚到底发生了什么。

Screenshot

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

我觉得我快要疯了。

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

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

在 if 语句之后它们是不变的。

最佳答案

编译代码后,您调用的 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 打印不同的值。

关于c++ - 条件为假时执行的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11384754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com