2012-07-19 3 views
2

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으로 나타납니다.

문제가 해결되었습니다. 고마워요.

+4

디버거를 사용해 보셨습니까? – Howard

+3

당신은 어디에서'NaN'을 얻고 있습니까? –

+0

죄송합니다. 함수는 루프에서 실행 됨) 다음 변수들에 대해 NaN을 얻습니다. centre_x (첫 번째 패스는 ok, then은 nan), pos_x는 centre_y (첫 번째 패스는 ok, then은 nan), pos_y는 오리엔테이션입니다. right_rad = 0 – iramusa

답변

3

0 = right_rad 경우에, 당신은 여기에서 0으로 나누어됩니다보다

orientation+=sign*vel_right*msTime/right_rad; 

다른, 나는 당신이 NaN을 받고있을 거라고 어떤 이유를 볼 수 없습니다.

+0

vel_left == 0 및 vel_right == 1은 expression이 (vel_left/(vel_right-1))이 아닌'((vel_left/vel_right) -1)'로 평가되기 때문에 0으로 나누지 않습니다. vel_left가 vel_right와 같거나 (또는 ​​반올림 후 몫이 가깝기 때문에) 0으로 나누기가 발생합니다. –

+0

아, 나는 그것을 잘못 읽었다. 네가 옳아. 지금 고쳤습니다. – jrad

0

불행히도 나는 코멘트를 남길만큼 충분한 담당자가 없지만 그 기능을 입력 할 때 orientation의 값은 무엇입니까? 그것은 모든 변수의 공통 인자 인 것 같습니다. sin 또는 cos을 초기화되지 않은 변수 또는 무한대 또는 NaN 중 하나에서 수행하면 NaN이 반환됩니다.

+0

방향이 값 0으로 초기화됩니다. – iramusa

관련 문제