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 2 Bytes EEPROM to Float Number

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



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

Convert 2 Bytes EEPROM to Float Number
PostPosted: Mon Sep 25, 2006 8:33 pm     Reply with quote

Hi all,
I've been trying to do something that may be simple but seem to not be able to get a handle on the solution to my problem. I'm setting two values in EEPROM for one is 10 and the other 50. I'm trying to combine the two the get a float number = 10.50. I've searched the web site and gone over the manual but nothing seems to grab me. Any ideas would be greatly appreciated. Thanks.

dave
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Mon Sep 25, 2006 9:22 pm     Reply with quote

Don't know if this will work or not, but worth a try.

Code:
int8 whole = 10;
int8 decimal = 50; // these are the two values you'd load from eeprom
// make them int16 if you like (doesn't matter)

float combined;

combined = (float)whole + ((float)decimal/100);
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

Convert 2 Bytes EEPROM to Float Number
PostPosted: Mon Sep 25, 2006 9:42 pm     Reply with quote

Thanks for the feedback. Obvious as heck when you look at it but just couldn't get it. Works great. I was wondering if you know what can be done to make a float number that comes back after calculation 11.1234567 to just a float with a value of 11.12?
Thanks again for the help.

dave
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

Re: Convert 2 Bytes EEPROM to Float Number
PostPosted: Mon Sep 25, 2006 11:00 pm     Reply with quote

Iloveoakdave wrote:
Thanks for the feedback. Obvious as heck when you look at it but just couldn't get it. Works great. I was wondering if you know what can be done to make a float number that comes back after calculation 11.1234567 to just a float with a value of 11.12?
Thanks again for the help.

dave


Again, don't know if this would work but it's where I would start.

Code:
float precise = 11.1234567;
int16 decimal;

precise = 100 * precise;

decimal = (int16)precise; // decimal should be 1112 now
precise = (float)decimal/100;


You could also test for rounding in the next digit too with just a bit of tweaking.

edit: Now it should work. Bonehead mistake the first time I typed it.
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

Convert 2 Bytes EEPROM to Float Number
PostPosted: Tue Sep 26, 2006 8:45 pm     Reply with quote

I tried your routine and one would think that is should work but the last line gives me 11.1199998855591 for the answer instead of 11.12. Now why would
1112 / 100 give 11.1199998855591? I pasted your code in mine just to be sure I didn't mess anything up. Weird! Could it be the compiler itself? It's version 3.203.

dave
epideath



Joined: 07 Jun 2006
Posts: 47

View user's profile Send private message

PostPosted: Tue Sep 26, 2006 9:16 pm     Reply with quote

You can try this. You will have to include the MATH.H file though

Code:

   float precise = 11.1234567;

   precise = floor(precise*100.0);
   precise = precise/100.0;


Regards
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

Re: Convert 2 Bytes EEPROM to Float Number
PostPosted: Tue Sep 26, 2006 10:26 pm     Reply with quote

Iloveoakdave wrote:
I tried your routine and one would think that is should work but the last line gives me 11.1199998855591 for the answer instead of 11.12. Now why would
1112 / 100 give 11.1199998855591? I pasted your code in mine just to be sure I didn't mess anything up. Weird! Could it be the compiler itself? It's version 3.203.

dave


Sounds like it's just the rounding error of the actual floating point representation. My HP calculator does similar things.

Maybe try this:

Code:
int16 a, b;
int16 decimal = 1112; // this is calculated as before, just declaring it here for brevity
float precise;

a = decimal/100;
b = decimal % 100;

precise = (float)a + ((float)b/100);


Since this same approach worked when you tried to "build" 10.50 from 10 and 50, maybe it will work here too. If not, it's just a rounding issue. Not sure what you can do about that.
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

Convert 2 Bytes EEPROM to Float Number
PostPosted: Wed Sep 27, 2006 7:18 pm     Reply with quote

Thanks for the feedback all. I tried both routines and they provide the same results. Funny how this works with the little CPU's. Take care.

dave
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Fri Sep 29, 2006 3:38 am     Reply with quote

Why not just use a int16 or int32 do all the required math then at the end sprintf and insert a ".". It seems like driving a nail with a sledge hammer, but it should work.
Iloveoakdave



Joined: 16 May 2004
Posts: 16

View user's profile Send private message

PostPosted: Fri Sep 29, 2006 7:18 am     Reply with quote

I'll look into it and check it out. Thanks for the tip.

dave
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