ROBOTC.net forums
http://robotc.net/forums/

order of parsing expressions
http://robotc.net/forums/viewtopic.php?f=1&t=4972
Page 1 of 1

Author:  fxlj007 [ Thu Oct 11, 2012 12:41 pm ]
Post subject:  order of parsing expressions

Hi guys, I come across a really confusing problem. I have a function as below:

Code:
void mSetInstantMotion(float vx, float vy, float r){
   int a = (int)(r + vx);
   int b = (int)(r-1*vx-2*vy);
   int c = (int)(r-1*vx+2*vy);

   motor[motorA] = a;
   motor[motorB] = b;
   motor[motorC] = c;
   while(true){EndTimeSlice();}

}


Very simple. But from a snapshot of the local variables, i see:
Quote:
vx 50
vy 50
r 0
a 50
b 50
c -150


It doesn't match what I'm expect. It should be b = 0 - 50 -2*50 = -150 right? Also wrong for c = 0 - 50 + 2*50 = 50

It becomes correct if i parenthesize each product term, as below

Code:
void mSetInstantMotion(float vx, float vy, float r){
   int a = (int)(r + vx);
   int b = (int)(r-(1*vx)-(2*vy));
   int c = (int)(r-(1*vx)+(2*vy));

   motor[motorA] = a;
   motor[motorB] = b;
   motor[motorC] = c;
   while(true){EndTimeSlice();}

}

But this should not be necessary. It seems the compiler evaluates an expression from right to left, which seems awkward. Do you have any ideas about this?

Author:  MHTS [ Thu Oct 11, 2012 1:11 pm ]
Post subject:  Re: order of parsing expressions

This has been fixed for the latest build. Please refer to the following post.
viewtopic.php?f=63&t=4839

Author:  tfriez [ Thu Oct 11, 2012 1:39 pm ]
Post subject:  Re: order of parsing expressions

Just to clarify - this issue was resolved in ROBOTC 3.51.

Thanks!

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/