Thread: Variable Typing
View Single Post
  #6   Spotlight this post!  
Unread 11-01-2008, 08:52
Daniel Jones Daniel Jones is offline
Registered User
AKA: Hanes
FRC #0190 (Gompei and the HERD)
Team Role: Mentor
 
Join Date: Mar 2005
Rookie Year: 2005
Location: Shrewsbury MA
Posts: 13
Daniel Jones is on a distinguished road
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.
__________________
"there are 10 types of people in this world, those who understand binary and those who don't"