Skip to main content
DKhan.1
Associate II
June 10, 2024
Question

Floating point calculation in STM32L431CBT6

  • June 10, 2024
  • 5 replies
  • 5033 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

5 replies

Uwe Bonnes
Chief
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.

Danish1
Lead III
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.

Andrew Neil
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...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
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 ... ?

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Tesla DeLorean
Guru
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()

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Karl Yamashita
Lead III
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? 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source