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.

gobernador
  • 5,659
  • 3
  • 32
  • 51
iramusa
  • 193
  • 1
  • 11
  • 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 – iramusa Jul 19 '12 at 18:43
  • When `right_rad` = 0 you divide by zero. See my answer below. – jrad Jul 19 '12 at 19:03
  • If it is solved, please add [SOLVED] prefix to the title, and vote for some answer below, as well as selecting it as an answer. – spacediver Jul 19 '12 at 19:30

2 Answers2

3

If right_rad = 0, then you will be dividing by zero here:

orientation+=sign*vel_right*msTime/right_rad;

Other than that, I can't see any reason you'd be getting NaN.

jrad
  • 3,172
  • 3
  • 22
  • 24
  • vel_left == 0 and vel_right == 1 do not cause division by zero, because the expression is evaluated as `((vel_left/vel_right)-1)`, not `(vel_left/(vel_right-1))`. vel_left equal to vel_right (or so close that the quotient after rounding is one) causes division by zero. – Eric Postpischil Jul 19 '12 at 19:22
  • Ah, I misread it. You're right. Fixed it now. – jrad Jul 19 '12 at 19:25
0

Unfortunately I haven't enough rep to just leave a comment, but what is the value of orientation when you enter that function? That seems to be the common factor of all the variables that are NaN.If you are performing sin or cos on a variable that hasn't been initialized, or is either infinity or NaN, you'll get NaN back.

Chris Stauffer
  • 352
  • 1
  • 8