2

I got stuck with a NaN error in this section of code:

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;
}

all the class variables are floats. Do you have any idea what might be causing this error?

EDIT: sorry, should have specified that. The function runs in a loop) I get NaN for the following variables centre_x(in the first pass through loop ok then nan), pos_x, centre_y(in the first pass through loop ok then nan), pos_y, orientation. right_rad=0. Clearly the problem is in the 'else' section.

OK, narrowed down to the line: float right_rad=width/(vel_left/vel_right-1); for some reason this turns out to be 0.

Problem solved. Thanks a lot guys.

4

2 に答える 2

3

= 0の場合right_rad、ここではゼロで除算します。

orientation+=sign*vel_right*msTime/right_rad;

それ以外は、NaNを取得する理由はわかりません。

于 2012-07-19T18:48:58.543 に答える
0

残念ながら、コメントを残すだけの担当者が足りませんが、orientationその機能を入力したときの価値は何ですか?これは、すべての変数に共通する要因のようです。実行中または初期化されていない変数、または無限大またはNaNのいずれかである変数を実行している場合は、NaNが返されますNaNsincos

于 2012-07-19T18:49:00.990 に答える