OCCRA [more] FRC Kickoff : 15d 22h 21m ...
Success is realizing that anything is possible, persistence is the key. - SlamminSammy [more]
 Chief Delphi Operator/Bit Issue
 User Name Remember Me? Password
 CD-Events CD-Media CD-Spy FRC-Spy
The Chief Delphi Forums are sponsored by Innovation First International, Inc.

#1
02-23-2008, 12:47 AM
 1jbinder Registered User FRC #0852 Join Date: Jan 2008 Location: Athenian School Posts: 35
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
02-23-2008, 12:51 AM
 Joe Ross Registered User FRC #0330 (Beachbots) Team Role: Engineer Join Date: Jun 2001 Rookie Year: 1997 Location: Los Angeles, CA Posts: 7,017
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
02-23-2008, 01:09 AM
 tdlrali Registered User FRC #0469 (Las Guerrillas) Team Role: Programmer Join Date: Sep 2006 Rookie Year: 2006 Location: MI Posts: 377
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
02-23-2008, 01:13 AM
 Guy Davidson 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
Re: Operator/Bit Issue

Quote:
 Originally Posted by tdlrali 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
02-23-2008, 01:25 AM
 tdlrali Registered User FRC #0469 (Las Guerrillas) Team Role: Programmer Join Date: Sep 2006 Rookie Year: 2006 Location: MI Posts: 377
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
02-23-2008, 01:55 AM
 Racer26 Formerly known as 1075guy FRC #4343 (MaxTech) Team Role: Mentor Join Date: Apr 2003 Rookie Year: 2003 Location: Beaverton, ON Posts: 1,885
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
02-23-2008, 02:36 AM
 BornaE 2009 Control System Beta Test Team FRC #0842 (Formerly 39) Team Role: Engineer Join Date: Jan 2007 Rookie Year: 2007 Location: Gilbert, Arizona Posts: 347
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
02-23-2008, 02:30 PM
 1jbinder Registered User FRC #0852 Join Date: Jan 2008 Location: Athenian School Posts: 35
Re: Operator/Bit Issue

Thanks to everyone for the help. Typecasting made the code work. The printf statement was fine.
Julian

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     User Announcements FIRST     General Forum         FIRST E-Mail Blast Archive     Rumor Mill     Career     Robot Showcase     Moderated Discussion Technical     Technical Discussion     Robotics Education and Curriculum     Motors     Electrical         CAN     Programming         NI LabVIEW         WindRiver C++         Java         MATLAB & Simulink         Python     intelitek easyC/REC information         easyC v2 for Vex         easyC for FRC         REC/Learnmate         WPILib         easyC PRO     Control System         IFI Control System         FRC Control System         Sensors         USB Chicklet     Pneumatics     Kit & Additional Hardware     CAD         Inventor         SolidWorks         Pro ENGINEER / Creo     IT / Communications         3D Animation and Competition         Website Design/Showcase         Videography and Photography         Computer Graphics     National Instruments LabVIEW and Data Acquisition         LabView and Data Acquisition Competition     Awards         Chairman's Award     Rules/Strategy         Scouting         You Make The Call     Team Organization         Fundraising         Starting New Teams         Finding A Team         College Teams     Championship Event     Regional Competitions     FIRST in Michigan     Mid-Atlantic Robotics     Off-Season Events         Chief Delphi Invitational     Thanks and/or Congrats     FRC Game Design     OCCRA         OCCRA Q&A         OCCRA Programming Other     Unsung FIRST Heroes     Chit-Chat         Games/Trivia             Fantasy FIRST     Car Nack's Corner     College & University Education     Dean Kamen's Inventions     FIRST-related Organizations         Western Region Robotics Forum         Robot Chicks Union (RCU)         Southern California Regional Robotics Forum         FIRST Interactive Rural SupporT         FIRST Scouting Network         The Grand FIRST Team         FloridaFIRST         FIRSTwiki         Ultimate Robot Challenge         Team San Diego         Dream FIRST         The Blue Alliance             Video Archives     FIRST Historians     FIRST-A-Holic's Anonymous Mailbox     FIRST In the News...     FIRST Lego League         Lego Mindstorm Discussion     FIRST Tech Challenge     VEX         VEX Robotics Competition         VEX IQ     Televised Robotics     Math and Science         NASA Discussion ChiefDelphi.com Website     CD Forum Support     Extra Discussion Old Forum Archives     1999     2000     2001

 Similar Threads Thread Thread Starter Forum Replies Last Post pic: A "Bit" of a problem. Herodotus Extra Discussion 26 10-11-2007 01:53 AM A bit of humor... 114Klutz Chit-Chat 0 04-01-2006 05:34 PM Operator Interface power ISSUE kacz100 Technical Discussion 11 02-27-2004 01:46 PM A bit unfair... Martin General Forum 61 02-26-2002 07:48 AM i am a bit miffed Zeinin 3D Animation and Competition 3 01-18-2002 01:39 PM

All times are GMT -5. The time now is 01:38 AM.

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

 -- English (12 hour) -- English (24 hour) Contact Us - Chief Delphi - Rules - Archive - Top