View previous topic :: View next topic |
Author |
Message |
barata
Joined: 07 Feb 2008 Posts: 6 Location: Portugal
|
Problem with float or printf or atof |
Posted: Tue Aug 10, 2010 4:59 am |
|
|
Good Morning all,
I have the following problem:
1 - I receive a string from rs232 with float numbers.
2 - I do the parsing from the string to separate the values.
3 – I convert string to float.
Now is the problem, when I print with printf the number, to confirm the storage I verify this:
Example of the problem:
0.01
0.02
0.04
0.05
Work fine
0.03 and 0.05
Don’t work fine. 0.03 appear 0.02 and 0.06 appear 0.05.
If I increase the decimal places I see that the 0.03 is 0.029 and the 0.06 is 0.059:
Code: |
//sup04
str_aux[0]=str_rs232_buf[25];
str_aux[1]=str_rs232_buf[26];
str_aux[2]=str_rs232_buf[27];
str_aux[3]=str_rs232_buf[28];
str_aux[4]='\0';
aux_float=atof(str_aux);
printf("<%s>\n\r",str_aux);
if(aux_float>= 0.00 && aux_float<= 1.00)//Se é válido - guardar
{
Toler_SUP_04=aux_float;
}
printf("<%f>\n\r",aux_float);
|
In the printf of the string I see the correct value, but in the printf of the float I see the wrong value.
Why this happen? What is the better way to solve/turn around this problem.
Compiler Version: 4.107
Thank you very much.
Best regards, |
|
|
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
|
Posted: Tue Aug 10, 2010 5:27 am |
|
|
Floating point cannot perfectly represent every number. 0.3 might really be something crazy like 0.297345646436346 in floating point. Floating point sucks even worse on a PIC chip as there is no FPU so the floating point has to be emulated in software.
The short answer is: don't do it. Scale your values back into integer range. So, if you will have a maximum of three numbers after the decimal then scale the whole thing by 1000 so that it's an integer. This will require more coding on your part but it does have the added benefit of being smaller in terms of total code space (as the floating point routines will not have to be added to the code), much faster, and more precise. When you see 0.03 it will really be 0.03 (well, it will be 30 because of the 1000x scale)
CCS has built in support for scaling values this way and still printing them as if they were the original number. Use the %w type in a printf. Like so:
Code: |
int16 value = 30; //This is 0.03 scaled up 1000x
printf("Value: %04.2w", value);
|
It prints: 0.03 |
|
|
barata
Joined: 07 Feb 2008 Posts: 6 Location: Portugal
|
|
Posted: Tue Aug 10, 2010 6:13 am |
|
|
Thank you very much collink.
I appreciate you help. Very useful for me. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Tue Aug 10, 2010 11:32 am |
|
|
Most of us have learned the decimal notation for numbers. In the middle ages many didn't learn decimal notation and often used multiplying by scales which is equivalent to binary notation. Binary even today is the preferred notation for CPU computation. Hand held calculators use a binary coded decimal mode of computation as did some of the original main frame computers. This created an impression that somehow decimal notation contains the correct answer and other notations lack precision. Translating between notations creates issues . The only numbers always immune to notational translation issues are integers. |
|
|
loupan
Joined: 22 Oct 2007 Posts: 21
|
|
Posted: Thu Aug 12, 2010 1:43 pm |
|
|
A trick you can use if you want to remain in the "floating point domain" is to add .5 * value of the least sig digit to the value operated on by printf.
Ex:
Code: |
float x;
x = .03;
printf ("%.2f", x+.005); // result will be .03
|
if you had
you would add 0.00005. |
|
|
|