|
|
View previous topic :: View next topic |
Author |
Message |
Freddie
Joined: 06 Sep 2003 Posts: 49
|
Break up float to 2 bytes |
Posted: Thu Apr 05, 2007 5:49 am |
|
|
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
|
|
Posted: Thu Apr 05, 2007 7:51 am |
|
|
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 |
|
|
|
|
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
|