gpt4 book ai didi

c++ - 查找 NaN 错误的原因

转载 作者:搜寻专家 更新时间:2023-10-31 01:53:06 25 4
gpt4 key购买 nike

<分区>

我在这段代码中遇到了 NaN 错误:

void Robot::updatePos(int msTime){
if(vel_left==vel_right){
pos_x-=vel_right*msTime*0.001*sin(orientation);
pos_y-=vel_right*msTime*0.001*cos(orientation);
}
else{
int sign=1;
if(vel_left<vel_right) sign=-1;
float centre_x, centre_y;

float right_rad=width/(vel_left/vel_right-1);

centre_x=pos_x-cos(orientation)*(right_rad+width/2);
centre_y=pos_y-sin(orientation)*(right_rad+width/2);
cout << "centre" << centre_x << "right_rad" << right_rad << endl;
orientation+=sign*vel_right*msTime/right_rad;


pos_x=centre_x+cos(orientation)*(right_rad+width/2);
pos_y=centre_y+sin(orientation)*(right_rad+width/2);
}
while(orientation>M_PI) orientation-=2*M_PI;
while(orientation<-M_PI) orientation+=2*M_PI;
cout << "pos_x: " << pos_x << " pos_y: " << pos_y <<
" orientation: " << orientation << endl;
}

所有类变量都是 float 。您是否知道可能导致此错误的原因?

编辑:抱歉,应该指定的。该函数在一个循环中运行)我得到以下变量的 NaN center_x(在第一次通过循环 ok 然后 nan),pos_x,center_y(在第一次通过循环 ok 然后 nan),pos_y,方向。 right_rad=0。显然问题出在“其他”部分。

好的,缩小到一行:float right_rad=width/(vel_left/vel_right-1);由于某种原因,结果为 0。

问题解决了。非常感谢大家。

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