# Math Gone Wrong

We have a integer type value called joystick

``````
joystick = (2)*(p1_x)+256;
printf("joystick: %d           p1_x: %d
", joystick, p1_x);

``````

When this code runs here is a sample output:

p1_x joystick
0 256 works like it should (2)(0)+256
50 356 works fine: 2
50+256=100+256=356
127 510 works fine: 2127+256=254+256=510
128 256 DOESN"T WORK

200 400 DOESN’T WORK*
250 500 DOESN’t WORK*

for some reason the code only works for p1_x is less than 128, otherwise after it is greater than or equal to 128 it just doubles it. Can anyone explain why, and what a solution might be, thanks.

I can’t remember correctly right at the moment, but I think integers are limited to 128 as the highest (really 127 because of zero). If I’m correct, that would explain the problem. Try using a different type variable, like a long or something, and see if that works.

p1_x is an unsigned char and can only hold a value equal to 0-255.
Therefore it cannot hold 256, 400, or 500 in the above example.

I can’t remember correctly right at the moment, but I think integers are limited to 128 as the highest (really 127 because of zero). If I’m correct, that would explain the problem. Try using a different type variable, like a long or something, and see if that works.

I tried longs yesterday as well, and got the same results, and i think integers can go from -32768 to 32768, so i don’t think that is the problem, but i’ll check again, next time we work on the robot, and unfortunately snow has closed school today, so we can’t work, any other ideas, thanks-again

p1_x is an unsigned char and can only hold a value equal to 0-255.
Therefore it cannot hold 256, 400, or 500 in the above example.

could you explain. We are not trying to make p1_x greater than 255, we are just trying to use it to make an int greater than 255…

value = (2)*(p1_x)+256;

I would think that is ok, but if not, how could we correct it to get the same result.

thanks.

try typecasting
value = 2*(int) p1_x + 256;

 What’s happening is the operation on the right will use the size of the largest type in the equation to store the result temporarily. In this case an unsigned char (p1_x).
The 2* calculation overflows the unsigned char temporary storage.
Adding the (int) forces the compiler to use the larger integer type to temporarily store the result.

To clear up the confusion here, the standard C types have the following ranges:
char: 2^8 (256)
int: 2^16 (65536)
long: 2^32 (4294967296)

The unsigned variety go 0 to (range-1)
The signed go from (-(range/2)) to ((range/2)-1)

Just a FYI

Just to clarify further (and we had this problem too):

The C standards says that integer arithmetic will be automatically promoted to integer size, that is 16 bits. So for signed integers the range is -32768 to 32767 and for unsigned the range is 0 to 65536.

The Microchip compiler has a “feature” that will try to optimize the size and speed of the program by keeping expressions at the smallest size possible. What you’re seeing is a result of this quirk in the compiler.

If you multiply two values together (even constants!) where the values can be represented as characters, then the math is done with byte arithmetic. So writing this expression:

8* 25

Gives you an answer of -56 because both 8 and 25 can fit in chars and so the math is done with chars (instead of the usual standard promotion to ints), the answer overflows the char range, and you get -56.

In your case you were multiplying 2p1_x. 2 can fit in a char, p1_x is a char, so the math is carried out as bytes. Any time 2p1_x was greater than a byte you had overflow and the wrong answer.

Psquared,