Go to Post I personally think that everybody on the team is important. No matter what, every single student, mentor, teacher put their effort all year long to get the team going. So in my opinion, Credit goes to everyone. - Arefin Bari [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 23-02-2008, 00:47
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 23-02-2008, 00:51
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: 8,567
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 23-02-2008, 01:09
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 23-02-2008, 01:13
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 23-02-2008, 01:25
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 23-02-2008, 01:55
Racer26 Racer26 is offline
Registered User
no team
Team Role: Alumni
 
Join Date: Apr 2003
Rookie Year: 2003
Location: Beaverton, ON
Posts: 2,229
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.
  #7   Spotlight this post!  
Unread 23-02-2008, 02:36
BornaE's Avatar
BornaE BornaE is offline
Registered User
FRC #0842 (Formerly 39)
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Gilbert, Arizona
Posts: 359
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
Re: Operator/Bit Issue

You code is working. The problem is with the printf statement.
so you can keep your code.
__________________
-Borna Emami
Team 0x27
  #8   Spotlight this post!  
Unread 23-02-2008, 14:30
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 11-10-2007 01:53
A bit of humor... 114Klutz Chit-Chat 0 01-04-2006 17:34
Operator Interface power ISSUE kacz100 Technical Discussion 11 27-02-2004 13:46
A bit unfair... Martin General Forum 61 26-02-2002 07:48
i am a bit miffed Zeinin 3D Animation and Competition 3 18-01-2002 13:39


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

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