NaN이 오류의 원인 찾기 :나는이 코드 섹션에서 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;
}
모든 클래스 변수가 수레됩니다. 이 오류의 원인을 파악하고 있습니까?
편집 : 죄송합니다. 함수는 루프에서 실행 됨) 다음 변수들에 대해 NaN을 얻습니다. centre_x (첫 번째 패스는 ok, then은 nan), pos_x는 centre_y (첫 번째 패스는 ok, then은 nan), pos_y는 오리엔테이션입니다. right_rad = 0. 분명히 문제는 'else'섹션에 있습니다.
줄까지 좁혀졌습니다 : float right_rad = width/(vel_left/vel_right-1); 어떤 이유로이 값이 0으로 나타납니다.
문제가 해결되었습니다. 고마워요.
디버거를 사용해 보셨습니까? – Howard
당신은 어디에서'NaN'을 얻고 있습니까? –
죄송합니다. 함수는 루프에서 실행 됨) 다음 변수들에 대해 NaN을 얻습니다. centre_x (첫 번째 패스는 ok, then은 nan), pos_x는 centre_y (첫 번째 패스는 ok, then은 nan), pos_y는 오리엔테이션입니다. right_rad = 0 – iramusa