Jump to content

Conditional Assignment Operator Failed With Long Division

Recommended Posts

Bug description:


In the code below, the "ratio = (x==0) ? 0xffff : BigY/BigX;" returns the result of division the previous time this function was called when fixed_x is set to zero (which makes x 0). I attached a simple project which demonstrates the failure. I am able to work around this bug with an extra if statement "if(fixed_x==0) ratio = 0xffff;". Also, I still get the failure if I swap x for fixed_x in the original case.


The code produced fails in the simulator as well as on Silicon (18F2620). It also fails with large and small memory models. In the simple example, the main function makes two calls to FixedAtan2 with different values but the second call gets the result of the first call which is -49.7 degrees instead of -90.0 degrees.


int FixedAtan2(int fixed_y,int fixed_x) // Value returned is in 9.7 fixed point format.
    unsigned int x,y;
    unsigned int Above,Below,Frac;
    unsigned int ratio;
    int angle;
    unsigned char NegX,NegY;
    unsigned char TableIndex,Half;
    unsigned long BigY,BigX;

    if(fixed_x<0){ NegX=1; x = -fixed_x; }  else{ NegX=0; x = fixed_x; }
    if(fixed_y<0){ NegY=1; y = -fixed_y; }  else{ NegY=0; y = fixed_y; }

	BigX = x;
	BigY = (unsigned long)y<<8;
    ratio = (x==0) ? 0xffff : BigY/BigX;
// Work around for complier bug    
    //if(fixed_x==0) ratio = 0xffff;
Steps to reproduce:

Build the attached projects and run to the while(1) in main. You will see AngleAboutX and AngleAboutY be the same values. If you single step into the second call, you can see the ratio equation produce the wrong answer.


Expected behaviour:

AngleAboutY should return 0xd300 which is -90.0 in 9.7 fixed point format.


Is the problem 100% reproduceable:

Yes. If fails every time.


IDE version: 7.22

Compiler: Compiler BoostC

Compiler version: 7.22

Target device: PIC18F2620

OS: Windows 7 and Windows 8.1


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...