![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
agompert
Joined: 14 Jan 2008 Posts: 5
|
Declaration of a variable |
Posted: Tue Feb 05, 2008 8:03 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Feb 06, 2008 3:16 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|