![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
ccdart
Joined: 12 Jan 2005 Posts: 3 Location: Seattle, WA, USA
|
Question about sprintf and float variables |
Posted: Tue Jan 25, 2005 4:02 pm |
|
|
Here is a short test program:
// fuses, etc. not shown
void main(void)
{
signed int8 c=100;
float value=0;
char ascout[20];
value = (float)c;
sprintf(ascout,"%f",value);
}
Now, I'm using MPLAB 7.00 with CCS as the designated tool suite. The CCS version (IDE/PCB/PCM) is 3.215
When I run the above program in the MPLAB simulator and examine the variable contents afterwards, I see:
c = 0x64 (as expected)
value = "100.0000" which, read from memory, is 0x85, 0x48, 0x00, 0x00 (again as expected)
BUT:
ascout contains "99.999999" (!) [followed by terminating 0x00, as expected].
If I change the sprintf specification to "%3.0f", ascout is then "99". If, instead, I use "%3.1f", ascout then contains "99.9".
I tried adding this line: value = value + 0.000001; before the sprintf. Now I get the (predictable, at least) result of "100.000001". Grrr...
It appears to me as if the conversion from signed int8 to float is working okay, but sprintf is not. Of course, what I want is an ascii string of "100.000000"
What must I do to get this result?
TIA |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
Re: Question about sprintf and float variables |
Posted: Tue Jan 25, 2005 4:26 pm |
|
|
ccdart wrote: | Here is a short test program:
// fuses, etc. not shown
void main(void)
{
signed int8 c=100;
float value=0;
char ascout[20];
value = (float)c;
sprintf(ascout,"%f",value);
}
Now, I'm using MPLAB 7.00 with CCS as the designated tool suite. The CCS version (IDE/PCB/PCM) is 3.215
When I run the above program in the MPLAB simulator and examine the variable contents afterwards, I see:
c = 0x64 (as expected)
value = "100.0000" which, read from memory, is 0x85, 0x48, 0x00, 0x00 (again as expected)
BUT:
ascout contains "99.999999" (!) [followed by terminating 0x00, as expected].
If I change the sprintf specification to "%3.0f", ascout is then "99". If, instead, I use "%3.1f", ascout then contains "99.9".
I tried adding this line: value = value + 0.000001; before the sprintf. Now I get the (predictable, at least) result of "100.000001". Grrr...
It appears to me as if the conversion from signed int8 to float is working okay, but sprintf is not. Of course, what I want is an ascii string of "100.000000"
What must I do to get this result?
TIA |
Unfortunately, you won't.
The problem is that when you use floating point values, you only have about 6.5 digits of 'real' accuracy (actually 1 in 8388608). With three digits in front of the DP, anything beyond the third digit after the DP, is really just 'noise', inherent in the conversion processes. You are asking too much of a 4 byte float. If you know that the values will never exceed a known value, then consider instead using a 32 bit integer, and just 'scaling' the result, by adding a suitable decimal point to the output string. This gives just over 9 digits of accuracy available.
If you limit yourself to 3 decimal places, and add 0.00049 to the value, you will get nice repeatable results.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
ccdart
Joined: 12 Jan 2005 Posts: 3 Location: Seattle, WA, USA
|
|
Posted: Tue Jan 25, 2005 4:36 pm |
|
|
Okay, that's pretty much what I was expecting to hear. I'll try your suggestion ASAP... if true, it should be good enough. |
|
![](templates/subSilver/images/spacer.gif) |
ccdart
Joined: 12 Jan 2005 Posts: 3 Location: Seattle, WA, USA
|
|
Posted: Tue Jan 25, 2005 4:46 pm |
|
|
Yup! That does it. I also tried four decimal places and adding .000049, and that seems to work as well. (Though I wonder if that's treading on thin ice...)
Thanks again for the solution! |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|