Skip to main content
Explorer II
June 10, 2024
Question

Floating point calculation in STM32L431CBT6

  • June 10, 2024
  • 5 replies
  • 3389 views

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

    This topic has been closed for replies.

    5 replies

    Graduate II
    June 10, 2024

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

    Graduate
    June 10, 2024

    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.

    Super User
    June 10, 2024

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

    Super User
    June 10, 2024

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

     

    Graduate II
    June 10, 2024

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

    Graduate II
    June 10, 2024

    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?