2024-06-10 09:42 AM
Dear all,
I am having some issues with precision of floating point calculations with STM32L431CBT6.
As one can see that when I do floating point calculation I am getting 1.23000002 instead of 1.23.
Can anyone suggest more efficient and accurate way to do this kind of calculation. I need very precise value for my application.
Thanks and kind regards,
Dibyendu
2024-06-10 09:53 AM
I think, convert_int() can be replace by atof(), with maybe byte[4] set to 0. Otherwise flot number have some small inaccuracy.
2024-06-10 09:54 AM
The C language has two types of floating-point number - float and double.
float uses 32-bits to represent a number and gives between 6 and 9 digits of precision.
double uses 64 bits and gives twice that.
So just use the word "double" where previously you used "float".
Be careful with scanf.
2024-06-10 09:54 AM
It is in the nature of Floating Point that you will get such errors
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Have you considered working in (scaled) integers ... ?
2024-06-10 10:01 AM
2024-06-10 10:05 AM
Use doubles to carry more "precision", but be aware that the binary number space can be very unforgiving with certain numbers, as decimal can be with 1/3
People use different methods for financial / book-keeping for example.
If this is important truncate the numbers, or use integer methods to get the rounding/forms that you want / need.
Be using atof() or atod()
2024-06-10 11:02 AM
As one can see that when I do floating point calculation I am getting 1.23000002 instead of 1.23.
Can anyone suggest more efficient and accurate way to do this kind of calculation. I need very precise value for my application.
1.23000002 is a more precise number than 1.23.
Or do you mean you want a number with no more than 2 decimal places?