Skip to main content
rrnicolay
Associate II
September 27, 2019
Question

Possible bug formatting floating point numbers in STM32CubeIDE

  • September 27, 2019
  • 5 replies
  • 6713 views

I'm facing a problem trying to format floating point numbers in STM32CubeIDE. The uC is STM32F103. I've checked the "use newlib-nano to print floating numbers" in the project settings.

To narrow things down, I used the following code snippet to reproduce the error:

char strP[100];
uint16_t cx = 0;
cx = snprintf(strP, 100, "%6.2f, %6.2f, %6.2f, %6.2f", 0.0115697104, -0.0796313286, -0.0220388453, -0.111881733);

The error doesn't appear on every iteration of the example code. It works most of the time, actually. When it fails, it prints:

26815634999686289245754584189029181710324847797922429453525901675501503951146118153956426275383940846635237101275691881352391832390627762028461487571337216.00, 0.00, -0.00, 0.00"

Or:

0.00 2.00 0.00 2.00

Things I tried to fix the problem:

  • Increased the stack from 0x400 to 0x500 through STM32CubeIDE interface.
  • Changed the linker scripts like told in this link.

I'm sending some floating point numbers to the uart to plot inclinometer data on a PC. This issue is annoying me for some days. Any help would be appreciated. Thanks!

This topic has been closed for replies.

5 replies

Tesla DeLorean
Guru
September 27, 2019

They are not float's they are double's, use the right formatting

cx = snprintf(strP, 100, "%6.2lf, %6.2lf, %6.2lf, %6.2lf", 0.0115697104, -0.0796313286, -0.0220388453, -0.111881733);

OR

cx = snprintf(strP, 100, "%6.2f, %6.2f, %6.2f, %6.2f", 0.0115697104f, -0.0796313286f, -0.0220388453f, -0.111881733f);

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Dave Nadler
Senior III
September 27, 2019

@Community member​ - It doesn't matter due to variadic argument promotion.

Thus I don't think its possible to only use float (and single-only float hardware) via sprintf;

gets promoted to double. Similarly char to integer are promoted?

Right ???

Tesla DeLorean
Guru
September 27, 2019

I suppose it depends on the library implementation, floats can be passed as 32-bit values (generally they don't get promoted), and there is enough half-assery in these assorted reduced foot-print libraries, and CubeIDE, that anything is within the realm of possibility. It does look to me as if it is pulling the wrong values off the stack here. This could be an issue with va_arg's or the point on the stack it thinks it has established for the ... parameters, vs local/auto variables.

On Keil I might suspect stack depth, or stack/heap collision, but GNU/GCC generally provides a lot wider berth..

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
rrnicolay
rrnicolayAuthor
Associate II
September 27, 2019

Thanks for the reply, @Community member​ . But in my real code I use floats instead of the hardcoded values.

Dave Nadler
Senior III
September 27, 2019
rrnicolay
rrnicolayAuthor
Associate II
September 27, 2019

Thanks for the reply @Dave Nadler​ . No, I'm not using FreeRTOS. Sorry, forgot to mention that.

rrnicolay
rrnicolayAuthor
Associate II
January 9, 2020

@Sebastian​ I moved a long way to solve this one.

Firstly, I switched from baremetal to FreeRTOS.

After that, the app was crashing while printing floating point numbers. To solve that one, check this topic.