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

Convert integer to float or vice versa - how??

 
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

Convert integer to float or vice versa - how??
PostPosted: Thu Jun 21, 2007 9:32 am     Reply with quote

I need to convert a value from an int16 to a float - how can I do this? Are there any rounding options in CCS? i.e. if int16 valA = 4 and i divide it by 3 to get a float value how can i round the float value up or down?
SherpaDoug



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

View user's profile Send private message

PostPosted: Thu Jun 21, 2007 9:53 am     Reply with quote

This is standard C. Get yourself a copy of any good C book, especially K&R (The C Programming Language" by Kernighan and Ritchie, ISBN 0-13-110362-8).

For your question:
int16 valA = 4;
float valB;
valB = valA/3 returns 1.0 because 4/3 => 1 using integer math
valB = valA/3.0 returns 1.333... because 4/3.0 uses float math
valB = (float)valA/3 returns 1.333... because 4.0/3 uses float math

You will have to do rounding yourself.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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