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

Integer to float problem

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



Joined: 13 May 2007
Posts: 34

View user's profile Send private message

Integer to float problem
PostPosted: Fri Jul 06, 2007 9:36 am     Reply with quote

I have a bit of code like this:

PulseCount = maxMove/yMove;

where all values are currently int16; However I really need pulseCount to be a rounded float. I have tried the following:

float fpCount = (float)maxMove/(float)yMove;

but my system seems to just drop out when I try and step through that line even though it all compiles fine. Can someone help me understand what I should be doing here?!

Thank you.
Ttelmah
Guest







PostPosted: Fri Jul 06, 2007 9:43 am     Reply with quote

Probably the 'float' declaration in front of the first variable.
You can't declare a variable 'inline' in standard C. The 'cast' (variable type in brackets), is normal, and fine (converts the variable on the fly).

So:
Code:

//at the start of the function.
float fpCount;

fpCount = (float)maxMove/(float)yMove;

In the type declaration (at the start of the function), values asigned, must be 'constants', not 'variables'.

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Jul 06, 2007 11:07 am     Reply with quote

Do you really want a float for the output or would a scaled integer be better? How about
Code:
PulseCounttimes100 = (maxMove * 100)/yMove;

Then you can use string functions to insert a decimal point when you print. Generally avoid using floats if at all possible as they are so slow and code intensive.
_________________
The search for better is endless. Instead simply find very good and get the job done.
jemly



Joined: 13 May 2007
Posts: 34

View user's profile Send private message

PostPosted: Mon Jul 09, 2007 8:16 am     Reply with quote

Thanks to both of you for your replies - really useful and will be trying out the suggestions later this week. Thanks!
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Mon Jul 09, 2007 8:20 am     Reply with quote

"Generally avoid using floats if at all possible as they are so slow and code intensive."

I strongly disagree - this is like saying "generally avoid using C and program in assembler only" - same reasoning.

There are exceptions to every rule, but you can generally work with floats and real-world uints, making your code much easier to write and to later understand (think maintenance). Keep your ISRs short and fast, and let your main loop do whatever is needed. Use printf for user output. I've measured "workload" on many many projects, and generally, the microprocessor is idle far more than one would think.

Happy coding Smile

Ken
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