Thread: Math Gone Wrong
View Single Post
  #6   Spotlight this post!  
Unread 22-01-2005, 09:46
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,833
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Math Gone Wrong

Quote:
Originally Posted by psquared
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.
Sorry, I read your printf and it didn't match the order of your example.

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

[edit] 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.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 22-01-2005 at 09:56.