cancel
Showing results for 
Search instead for 
Did you mean: 

Floating point calculation in STM32L431CBT6

DKhan.1
Associate II

Dear all,

I am having some issues with precision of floating point calculations with STM32L431CBT6.

DKhan1_0-1718037438449.png

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

6 REPLIES 6
Uwe Bonnes
Principal III

I think, convert_int() can be replace by atof(), with maybe byte[4] set to 0. Otherwise flot number have some small inaccuracy.

Danish1
Lead II

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.

Andrew Neil
Evangelist III

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 ... ?

 


@Danish1 wrote:

So just use the word "double" where previously you used "float".


@DKhan.1 but note that the Cortex-M4's floating-point hardware is only 'float' - so 'double' will be done in software...

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()

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

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?