![]() |
Variable Typing
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? |
Re: Variable Typing
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. |
Re: Variable Typing
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?
|
Re: Variable Typing
Your first try looks fine to me. What was the error you encountered?
|
Re: Variable Typing
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.
|
Re: Variable Typing
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. |
Re: Variable Typing
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.
|
Re: Variable Typing
Quote:
Quote:
|
Re: Variable Typing
Quote:
Try this: Code:
x_joy_1 = (int)p1_x - 127; // Makes the range -127 to 127Quote:
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. Quote:
|
| All times are GMT -5. The time now is 23:18. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi