gpt4 book ai didi

c++ - 减法和晶圆厂工作,然后在程序运行时不工作

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

好吧,我快要发疯了,想弄清楚发生了什么。我在主循环中有一个 case 语句(状态 6 指的是下面示例输出中的 case)。有一段时间,一切正常,然后我开始得到乱七八糟的结果,一个简单的减法给出了错误的结果。这是代码片段:

  case turning:
Serial.print("state: ");
Serial.print(state);
Serial.print("Waypoint: ");
Serial.print(waypointIndex);
Serial.print(" Location: ");
Serial.print(location.x);
Serial.print(" ");
Serial.print(location.y);
Serial.print(" heading: ");
Serial.print(heading);
Serial.print("target heading: ");
Serial.print(targetHeading);
Serial.print(" heading delta: ");
headingDelta = fabs(heading-targetHeading);
Serial.print(headingDelta);
Serial.print("small? ");
Serial.print((headingDelta<0.25));
Serial.print("\n");

if (headingDelta < 0.25) {
Stop();
state = travel;
}
else {
if (turn>=0) {
Left(255,255); // turn left
}
else {
Right(255,255); // turn right
}
}
break;

这是输出的一部分:

state: 6Waypoint: 2 Location: 111.98   18.12 heading: 0.95target heading: 1.57 heading delta: 0.62small? 0

state: 6Waypoint: 2 Location: 111.98 18.12 heading: 0.95target heading: 1.57 heading delta: 0.62small? 0

state: 6Waypoint: 2 Location: 111.98 18.12 heading: 1.35target heading: 1.57 heading delta: 0.22small? 1

state: 6Waypoint: 3 Location: 142.73 47.69 heading: 0.55target heading: 0.00 heading delta: 0.00small? 0

state: 6Waypoint: 3 Location: 142.73 47.69 heading: 0.55target heading: 0.00 heading delta: 0.00small? 0

state: 6Waypoint: 3 Location: 142.73 47.69 heading: 0.55target heading: 0.00 heading delta: 0.00small? 0

在第 4 个输出中,我的 headingDelta 突然打印为零,但显然它不是零,但逻辑检查它是否小却说它不小!

我正在拔头发。这是怎么回事?这是在 Romeo 上,它是 Arduino 兼容的 ATmega328 板。

最佳答案

看起来 targetHeading 变成了 NaN+/-Inf 并且 Serial.print(...) 在遇到这些值之一时只输出 0.0。在计算 targetHeading 时,您可能在某处除以 0。

关于c++ - 减法和晶圆厂工作,然后在程序运行时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959500/

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