PDA

View Full Version : Variable Typing


Tom Line
01-11-2008, 08:24 AM
My team wants to subtract 127 from each joystick to change the range from 0 to 255 to -127 to 127. We originally had trouble with this statement:

x_joy_1 = p1_x - 127; // Makes the range -127 to 127
y_joy_1 = p1_y - 127;

p1_x is a defined term from the rcdata. x_joy_1 is an int. I'm not precisely sure why we had trouble - I suspect it's because p1_x is of the wrong type (it's a define, not an int).

Changing it as one of the team members suggested:

int(p1_x) and int(p1_y) resulted in a syntax error.

Our final result was this, which works fine:

x_joy_1 = p1_x;
y_joy_1 = p1_y;

x_joy_1 = x_joy_1 - 127; // Makes the range -127 to 127
y_joy_1 = y_joy_1 - 127;

printf("x_joy_1 is %d\r\n",x_joy_1);
printf("y_joy_1 is %d\r\n",y_joy_1);

Could someone shine some light on why this works the way it does? I'm sick of trial and error with variable int types :mad: Isn't there a much more elegant way of coding this?

tseres
01-11-2008, 08:29 AM
it works because the

x_joy_1 = p1_x;
y_joy_1 = p1_y;

makes thevariables equal to the joystick values. the second part just subtracts 127 from them. the point is that the variable took the values them manipulated them.....i hope that answered your question.

Tom Line
01-11-2008, 08:32 AM
Thank you, but that doesn't really help. I'm interested in why the first statements don't work - why isn't the value from p1_x and p1_y treated as a value I can subtract 127 from?

Alan Anderson
01-11-2008, 08:36 AM
Your first try looks fine to me. What was the error you encountered?

Tom Line
01-11-2008, 08:49 AM
When we print out the result of the first two statements using the printf's at the end, we get values that don't make sense (0-255) and don't follow the joystick values in any way.

Daniel Jones
01-11-2008, 08:52 AM
The variable p1_y is an unsigned character with a range from 0 -255.
When you subtract 127 from it, if it would result in a negative number it instead overflows, resulting in a positive number above 127. This number is then stored in your int.
There are several ways to fix this, and you were close with int(p1_y). Try using (int)p1_y - 127 instead, this forces the program to treat p1_y as an integer while it subtracts so you should not have conversion problems. This is called typecasting.
The code that did work works because you used an integer to store p1_y before doing the subtraction, achieving the same effect as typecasting.
Also keep in mind that p1_y is not of type define, it is an unsigned character. A define statement literally replaces one string with another, so the define statement just makes p1_y an alias for another variable.

Tom Line
01-11-2008, 09:04 AM
Thank you! We didn't go far enough back to discover that it was an unsigned character, though I suspected it was not the appropriate type which is why I tried to typecast it. Go figure, after a year off I tried treating it like I would an excel statement rather than a C statement.

Joe Ross
01-11-2008, 10:02 AM
My team wants to subtract 127 from each joystick to change the range from 0 to 255 to -127 to 127. We originally had trouble with this statement:

x_joy_1 = p1_x - 127; // Makes the range -127 to 127
y_joy_1 = p1_y - 127;

p1_x is a defined term from the rcdata. x_joy_1 is an int. I'm not precisely sure why we had trouble - I suspect it's because p1_x is of the wrong type (it's a define, not an int).

This statement would work in Visual Studio or another ISO/ANSI compliant compiler. Microchip's deviation is described in the C18 compiler user's guide:

2.7.1 Integer Promotions
ISO mandates that all arithmetic be performed at int precision or greater. By default,
MPLAB C18 will perform arithmetic at the size of the largest operand, even if both
operands are smaller than an int. The ISO mandated behavior can be instated via the
-Oi command-line option.

For example:
unsigned char a, b;
unsigned i;
a = b = 0x80;
i = a + b; /* ISO requires that i == 0x100, but in C18 i == 0 */
Note that this divergence also applies to constant literals. The chosen type for constant
literals is the first one from the appropriate group that can represent the value of the
constant without overflow.

For example:
#define A 0x10 /* A will be considered a char unless -Oi
specified */
#define B 0x10 /* B will be considered a char unless -Oi
specified */
#define C (A) * (B)
unsigned i;
i = C; /* ISO requires that i == 0x100, but in C18 i == 0 */


We use -Oi because it makes things a lot easier to troubleshoot and makes the code clearer without a bunch of casts.

ay2b
01-11-2008, 01:07 PM
My team wants to subtract 127 from each joystick to change the range from 0 to 255 to -127 to 127. We originally had trouble with this statement:

x_joy_1 = p1_x - 127; // Makes the range -127 to 127
y_joy_1 = p1_y - 127;

p1_x is a defined term from the rcdata. x_joy_1 is an int. I'm not precisely sure why we had trouble - I suspect it's because p1_x is of the wrong type (it's a define, not an int).

Changing it as one of the team members suggested:

int(p1_x) and int(p1_y) resulted in a syntax error.

The correct syntax is (int)p1_x. This is called "type casting".

Try this:

x_joy_1 = (int)p1_x - 127; // Makes the range -127 to 127
y_joy_1 = (int)p1_y - 127;


Could someone shine some light on why this works the way it does? I'm sick of trial and error with variable int types Isn't there a much more elegant way of coding this?

The problem you're facing is one of one of order of operations. Consider this statement: 5 + 5 * 5. What is the value? Is it 50 or 30? If you do the addition first, you'll get 10 * 5 = 50. If you do the multiplication first, you'll get 5 + 25 = 30. In C, multiplication is defined to come before addition.

In the statement above, x_joy_1 = p1_x - 127, everything to the right of the equals sign is evaluated before the assignment operator (i.e. the equals sign). First the compiler does p1_x - 127. p1_x is an unsigned char which means it has a range of 0-255. Suppose it had a value of 122. Now, when you subtract 127, you get -5. However, since that is outside of the range for its type, it wraps around, and so you get 250. Next comes the assignment operator. x_joy_1 is of type int, while the value on the right is an unsigned char. The compiler now automatically typecasts the value on the right to be the same type as the variable on the left, in order to do the assignment.

What you want to do is make sure that the value on the right is an int before the subtraction operator happens. To do this, you typecast the unsigned char to an int with the statement (int)p1_x.


p1_x is of the wrong type (it's a define, not an int)
While p1_x is "a define", that is not a type. To find out what type it is, you have to see what it's defined as. It's defined as something which has a type of unsigned char.