CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Declaration of a variable

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
agompert



Joined: 14 Jan 2008
Posts: 5

View user's profile Send private message

Declaration of a variable
PostPosted: Tue Feb 05, 2008 8:03 pm     Reply with quote

I know I just need to experiment a little, but, I was wondering what type of a variable to declare. All I’m doing is just counting wheel turns on a wheel and multiplying it times the distance around the wheel. My counter counting wheel turns, is a 16 bit binary number. I want to multiply it by a number like 3.1 or 1.8 etc. to give me total feet or what ever. The new variable, feet, which I will display on a LCD, would this variable be a float, to give or show the decimal point? Or would it be an Int16 or a long variable? A float number, to my understanding, is 32 bit, which is way overkill for what I’m doing. All I need is the 16 bit binary value would
be enough. But to show a decimal point, it would be a float variable.
right?
Ttelmah
Guest







PostPosted: Wed Feb 06, 2008 3:16 am     Reply with quote

In a sense, it is 'up to you'. However float operations, will always be _slow_. One possibility (if the sizes are suitable), is to use the 'scaled integer' ability of CCS (the %w output specifier in printf).
In the examples given, you would start with the int16, and instead of multiplying by 3.1, or 1.8, multiply by the value *10.
So:
Code:

int16 val;
val=321;  //test value fo example

val=val*31; //your '3.1' example

printf("%4.1w",val);

Will print '995.1', but the actual value stored will be the integer '9951', and the arithmetic, will all be integer. In particular, in the 'printf', where the divisions to get each digit will be integer.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group