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 support@ccsinfo.com

Break up float to 2 bytes

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



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

Break up float to 2 bytes
PostPosted: Thu Apr 05, 2007 5:49 am     Reply with quote

I want to save a float as 2 bytes. One byte representing the part before the decimal, one representing the part after the decimal.

How can I extract the parts into seperate bytes?
Ttelmah
Guest







PostPosted: Thu Apr 05, 2007 7:51 am     Reply with quote

Obviously, you will be limited to numbers 255 and below.
If you simply take:
Code:


int main;
float val;

val=123.45;

main=val;

You will get the part in front of the decimal place stored in the integer.
Now, to do the whole thing, you can use:
Code:


int decimal;
int main;
float val;
float decimalf;

val=123.45;

decimal = 100*modf(val,&decimalf);
main=decimalf;

However 'beware' of the inaccuracies in this...
Code:

int main;
int decimal;
float val;

val=123.45;

main=val;
decimal=(val*100)-(main*100L);

Will probably be faster, and may be better.

I have to ask, 'why have a float at all'?. Consider using a larger scaled integer in the first place. Makes arithmetic much faster, and makes this a lot easier...

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