Go to Post if you do make a marshmallow robot, that would be pretty sweet. - smurfgirl [more]
Home
Go Back   Chief Delphi > Technical > Programming
Team 51   CD-Events   CD-Media   CD-Swap   CD-Spy   FRC-Spy   Unsung FIRST Heroes   WFA
portal register members calendar search Today's Posts Mark Forums Read FAQ rules
VEXpro
The Chief Delphi Forums are sponsored by Innovation First International, Inc.
Team 221 LLC
ADVERTISEMENT

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-22-2008, 11:47 PM
1jbinder 1jbinder is offline
Registered User
FRC #0852
 
Join Date: Jan 2008
Location: Athenian School
Posts: 35
1jbinder is an unknown quantity at this point
Operator/Bit Issue

Hi,

We are having a really unique problem that has stumped the whole programing team, all of the mentors, and the team leader. We are trying to write some code in MPLab to calculate the number of encoder ticks needed to reach six feet. We have found that fourteen encoder clicks is the same as one inch. The problem comes in when we try to multiply 72(six feet in inches) by 14(clicks in an inch). When this happens we get -16 which is 32 bits off. We are using the variable type long. Below is the code that we have written:

long atemp=0;
atemp=14*72;//should be 1008
printf("atemp=%ld\r\n", atemp);

This code did not work and gave us a value of -16 for atemp. We also tried this:

long atemp=0;
atemp=72+72+72+72+72+72+72+72+72+72+72+72+72+72;
printf("atemp=%ld\r\n", atemp);

This also did not work and gave us a value of -16. Adding smaller numbers works fine and multiplying smaller numbers also works. We have pretty much exhausted any ideas.
Thanks,
Julian
  #2   Spotlight this post!  
Unread 02-22-2008, 11:51 PM
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 6,597
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Operator/Bit Issue

C18 diverges from the ISO standard in that when all operands will fit in a char, it will do the math in a char. If you cast one operand to an int, it will do the math the way you want. You can also enable integer promotion in the project file.
  #3   Spotlight this post!  
Unread 02-23-2008, 12:09 AM
tdlrali tdlrali is offline
Registered User
FRC #0469 (Las Guerrillas)
Team Role: Programmer
 
Join Date: Sep 2006
Rookie Year: 2006
Location: MI
Posts: 377
tdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud of
Re: Operator/Bit Issue

Or write

Code:
atemp=14L*72L;
which will make sure the numbers are treated as longs.

Also, i'm not sure if you can print longs...
  #4   Spotlight this post!  
Unread 02-23-2008, 12:13 AM
Guy Davidson Guy Davidson is offline
Registered User
AKA: formerly sumadin
FRC #0008 (Paly Robotics)
Team Role: Alumni
 
Join Date: Mar 2005
Rookie Year: 2005
Location: Ra'anana, Israel
Posts: 660
Guy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to behold
Send a message via ICQ to Guy Davidson Send a message via AIM to Guy Davidson Send a message via MSN to Guy Davidson
Re: Operator/Bit Issue

Quote:
Originally Posted by tdlrali View Post
Also, i'm not sure if you can print longs...
I believe that both casting them to an int or using %ld should work.
__________________
  #5   Spotlight this post!  
Unread 02-23-2008, 12:25 AM
tdlrali tdlrali is offline
Registered User
FRC #0469 (Las Guerrillas)
Team Role: Programmer
 
Join Date: Sep 2006
Rookie Year: 2006
Location: MI
Posts: 377
tdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud of
Re: Operator/Bit Issue

Quote:
I believe that both casting them to an int or using %ld should work.
int wont print the whole long though (only the low 8 bits), and i can't remember whether %ld actually works.
  #6   Spotlight this post!  
Unread 02-23-2008, 12:55 AM
Racer26 Racer26 is offline
Formerly known as 1075guy
FRC #4343 (MaxTech)
Team Role: Mentor
 
Join Date: Apr 2003
Rookie Year: 2003
Location: Beaverton, ON
Posts: 1,747
Racer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond repute
Re: Operator/Bit Issue

I think %ld does work in later revisions of the default code (kevins?)

Also, an alternative method of getting the math the way you want it:

Code:
 atemp = (long)14*(long)72;
Yay for C-style casting... in my opinion, SO much easier than C++ casts.
__________________
During my time with 4343 (2013-?):
2013 - ONTO Finalists, #2 Alliance Captain (Thanks 1241, 216), ONTO2 Quarter-Finalists (Thanks 190, 3161)

During my time with 1075 (2003-2012):
2011 - WAT Imagery Award, ON Rockwell Automation Innovation in Control Award, ON Finalists (Thanks 188, 1006)
2010 - WAT Imagery Award
2009 - ON Chrysler Team Spirit Award, ON Rockwell Automation Innovation in Control Award
2008 - BE7 Champions (Thanks 25, 1302), Kettering Kickoff Champions (Thanks 910, 67, 2619)
2007 - ON Rockwell Automation Innovation in Control Award (for our pneumatic multi-position arm)
2004 - Wonderland Invitational Champions (Thanks 1241, 1114)
2003 - ON Finalists (Thanks 188, 306)
  #7   Spotlight this post!  
Unread 02-23-2008, 01:36 AM
BornaE's Avatar
BornaE BornaE is offline
2009 Control System Beta Test Team
FRC #0842 (Formerly 39)
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Gilbert, Arizona
Posts: 341
BornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant future
Send a message via Skype™ to BornaE
Re: Operator/Bit Issue

You code is working. The problem is with the printf statement.
so you can keep your code.
__________________
-Borna Emami
Team 39 LADS Video || Team 0x27 || NURC Entry Video
2007: AZ Champions, AZ GM Industrial Design Award, NV Finalist
2008: AZ Champions, AZ GM Industrial Design Award, AZ Delphi "Driving Tomorrow's Technology Award", NV Champions, NV GM industrial Design Award
2009: AZ Champions
Programmer
  #8   Spotlight this post!  
Unread 02-23-2008, 01:30 PM
1jbinder 1jbinder is offline
Registered User
FRC #0852
 
Join Date: Jan 2008
Location: Athenian School
Posts: 35
1jbinder is an unknown quantity at this point
Re: Operator/Bit Issue

Thanks to everyone for the help. Typecasting made the code work. The printf statement was fine.
Julian
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
pic: A "Bit" of a problem. Herodotus Extra Discussion 26 10-11-2007 12:53 AM
A bit of humor... 114Klutz Chit-Chat 0 04-01-2006 04:34 PM
Operator Interface power ISSUE kacz100 Technical Discussion 11 02-27-2004 12:46 PM
A bit unfair... Martin General Forum 61 02-26-2002 06:48 AM
i am a bit miffed Zeinin 3D Animation and Competition 3 01-18-2002 12:39 PM


All times are GMT -5. The time now is 06:26 AM.

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


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright © Delphi and Pontiac Central High School