Go to Post If you want something done about it, than do something about it. - Tetraman [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 22-01-2005, 09:25
psquared psquared is offline
Registered User
no team
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Chantilly
Posts: 34
psquared is an unknown quantity at this point
Math Gone Wrong

We have a integer type value called joystick
Code:
joystick = (2)*(p1_x)+256;
printf("joystick: %d           p1_x: %d      \n", 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: 2*127+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.
  #2   Spotlight this post!  
Unread 22-01-2005, 09:32
Joshua May's Avatar
Joshua May Joshua May is offline
Go Bears!
FRC #1110 (Binary Bulldogs)
Team Role: College Student
 
Join Date: Nov 2003
Rookie Year: 2003
Location: Berkeley, CA
Posts: 1,306
Joshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond repute
Send a message via AIM to Joshua May
Re: Math Gone Wrong

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.
__________________
The FIRST Wiki - openFIRST - Ultimate Robot Challenge - URC Wiki
I currently have 50 GMail invites, PM or email me for one.
UC Berkeley Class of 2009

2005 Las Vegas Regional Autodesk Visualization Award
2005 Las Vegas Regional #8 Seeded Alliance with 988 and 1505
2006 Southern California Regional #15 seed
  #3   Spotlight this post!  
Unread 22-01-2005, 09:36
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,919
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
We have a integer type value called joystick
[code]
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...
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.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
  #4   Spotlight this post!  
Unread 22-01-2005, 09:37
psquared psquared is offline
Registered User
no team
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Chantilly
Posts: 34
psquared is an unknown quantity at this point
Re: Math Gone Wrong

Quote:
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

Last edited by psquared : 22-01-2005 at 09:41.
  #5   Spotlight this post!  
Unread 22-01-2005, 09:40
psquared psquared is offline
Registered User
no team
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Chantilly
Posts: 34
psquared is an unknown quantity at this point
Re: Math Gone Wrong

Quote:
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.
  #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,919
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.
  #7   Spotlight this post!  
Unread 22-01-2005, 11:03
steven114 steven114 is offline
Programming Wizard and Team Captain
AKA: Steven Schlansker
FRC #0114 (Eaglestrike)
Team Role: Programmer
 
Join Date: Feb 2004
Location: Los Altos, CA
Posts: 335
steven114 is a jewel in the roughsteven114 is a jewel in the roughsteven114 is a jewel in the rough
Send a message via AIM to steven114
Re: Math Gone Wrong

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
__________________
Shift to the left, shift to the right!
Pop up, push down, byte, byte, byte!
  #8   Spotlight this post!  
Unread 23-01-2005, 10:20
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Math Gone Wrong

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 2*p1_x. 2 can fit in a char, p1_x is a char, so the math is carried out as bytes. Any time 2*p1_x was greater than a byte you had overflow and the wrong answer.
  #9   Spotlight this post!  
Unread 23-01-2005, 10:38
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Math Gone Wrong

Psquared,

Please show us your declaration for the variable "joystick".
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #10   Spotlight this post!  
Unread 23-01-2005, 10:40
psquared psquared is offline
Registered User
no team
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Chantilly
Posts: 34
psquared is an unknown quantity at this point
Re: Math Gone Wrong

Thanks Brad, that makes a lot of sense, and will help us evade any future problems like that. Also I will try the casting when we are able to meet again for robotics. It is snowing here, and we haven't been able to meet since friday, and if school is out tomorrow, we can't work tomorrow either. But, when we do go back, I'll be sure to try everything out, and if it works I'll let you know. Thanks for all of the help from everyone who offered solutions.
  #11   Spotlight this post!  
Unread 23-01-2005, 12:18
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,919
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

One other solution is to use the option in MPLAB to force the default use of the integer type (at least) in any calculations. You still need to be aware of the potential for overflow, e.g., any calculation with an intermediate result > 32767 for instance.

Project -> Build Options… -> Project
MPLAB C18 tab

Check “Enable integer promotions”
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New math functions? KVermilion Programming 8 17-01-2005 10:38
I-cylinder body wrong? Daniel Brim Inventor 0 05-03-2004 20:18
Wrong Math in the site info section.. Matt Attallah CD Forum Support 9 10-06-2003 16:43
direct Drill motor drive wrong or right rcubes85 General Forum 6 11-02-2003 18:11
Whats wrong with this code? Morgoth Programming 4 19-01-2003 11:45


All times are GMT -5. The time now is 13:10.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi