cancel
Showing results for 
Search instead for 
Did you mean: 

atof convension error

mikemanzano.javier9
Associate II
Posted on December 15, 2015 at 15:44

Hi clive,

I encounter a problem in converting float string array to float then back to float string array.

I have this array acValue = ''1000.1'';

then float fValue = atof(acValue);

then what I got in fValue is 1000.0997

when I convert it again to string, for example

sprintf(acValue, ''%f'', fValue);

acValue became now 1000.0997

Do you have any idea why this happen?

I tried many ways to trick it, like strtok the string to two,

then convert the whole number and floating number.

converting the float alone will result to 0.1, but when I add 1000 to it, it now becomes 1000.0997

Im using STM32F103, IAR EWARM and NORTi for my OS.

Regards,

Mike

#atof-convertion-error-float
4 REPLIES 4
John F.
Senior
Posted on December 15, 2015 at 15:53

Floating point representation. For example see,

http://floating-point-gui.de/

Posted on December 15, 2015 at 17:05

People who want precision don't use 32-bit floats, the are a trap for the unwary.

In Windows on a Microsoft compiler..

#include <
windows.h
>
#include <
stdio.h
>
#include <
math.h
>
int main(int argc, char **argv)
{
float x = 1000.1;
printf(''%f
'', x);
return(1);
}
1000.099976

If you print to 3 decimal places, it will look like you have the number you expect. There are quite a number of decimal fractions that don't represent well in binary, a bit like how 1/3 is expressed in decimal. A float can represent about 7 decimal digits, constrain your printing to reflect that.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mikemanzano.javier9
Associate II
Posted on December 16, 2015 at 03:51

Thanks for the information. 

Now my understanding of float is, it is an inherent characteristic of float,

that it cannot represent some floating point number well.

In the specification which I am following, the floating point number of digit must be flexible depends on the number of digit inputted by the user. But now, i think I have to suggest to make it fix so I can use formatter such as %.03f. Also I am thinking of something like, from its ASCII value, I will move the point 3 digits to the right, something like that or multiply float by 1000. But any way, thanks.

Another thought of mine, mind if I will say, if ST Micro can developed something to improve representation of float, it can be additional value for STM.

Posted on December 16, 2015 at 04:41

The

https://en.wikipedia.org/wiki/IEEE_floating_point

pretty much constrains/defines the representation. If you want more precision you'd want to use 64-bit doubles

The precision is constrained by the number of bits in the mantissa. Floating point most often falls over when making very small changes to very large numbers. You have to understand what your math is doing, as if flows through your calculations and the range of numbers it's expected to handle. This is especially important in these ARM implementation because it doesn't carry higher precision intermediate values. This is where PC test code might yield different, more accurate, answers.

printf() defaults to 6 decimal places, but it doesn't have a clean way of handling the number of significant digits. Using scientific notation (%e) might be one way, but probably not suitable for your case and user expectations.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..