View previous topic :: View next topic |
Author |
Message |
Iloveoakdave
Joined: 16 May 2004 Posts: 16
|
Convert 2 Bytes EEPROM to Float Number |
Posted: Mon Sep 25, 2006 8:33 pm |
|
|
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
|
|
Posted: Mon Sep 25, 2006 9:22 pm |
|
|
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
|
Convert 2 Bytes EEPROM to Float Number |
Posted: Mon Sep 25, 2006 9:42 pm |
|
|
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
|
Re: Convert 2 Bytes EEPROM to Float Number |
Posted: Mon Sep 25, 2006 11:00 pm |
|
|
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
|
Convert 2 Bytes EEPROM to Float Number |
Posted: Tue Sep 26, 2006 8:45 pm |
|
|
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
|
|
Posted: Tue Sep 26, 2006 9:16 pm |
|
|
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
|
Re: Convert 2 Bytes EEPROM to Float Number |
Posted: Tue Sep 26, 2006 10:26 pm |
|
|
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
|
Convert 2 Bytes EEPROM to Float Number |
Posted: Wed Sep 27, 2006 7:18 pm |
|
|
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
|
|
Posted: Fri Sep 29, 2006 3:38 am |
|
|
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
|
|
Posted: Fri Sep 29, 2006 7:18 am |
|
|
I'll look into it and check it out. Thanks for the tip.
dave |
|
|
|