cancel
Showing results for 
Search instead for 
Did you mean: 

Printf with float produces random output

anonymous.8
Senior II

No matter what I try, something as simple as printf("%f\n", 0.0) or printf("%f\n, 3.14159265359 for some reason produces a text string representing a negative, extremely long and seemingly random number.

Opening project-settings on C/C++ Build -> Settings -> Tools Settings there is a checkbox with the text "Use float with printf from newlib-nano (-u _printf_float)", but enabling that doesn't change anything. Also, Eclipse still keeps complaining that there is no float-support (-u _printf_float), even though the flag is clearly visible on the command-line when compiling a project -- seemingly the only way of getting Eclipse to stop complaining is to manually add the flag to linker-settings. Doesn't fix printf, though.

This is on the STM32CubeIde 1.0.0 and a STM32F103CBT6 -- no, there is no H/W-float. I'm left wondering if this is a bug or if I'm missing something. I mean, the checkbox is pretty clear and one would assume that it does what it says, but....

(Disclaimer: this isn't an issue for me, per se, as I have a workaround for printing floats. I'm merely curious and if this were an actual bug and not just me doing things wrong again, it probably should be fixed.)

1 ACCEPTED SOLUTION

Accepted Solutions
Ethan HUANG
ST Employee

​Yes, this is a known issue. _estack in linker script is not set properly. You may have _estack in 0x2004ffff (odd alignment anyway) and please change it to 0x20050000 (by word alignment). There were couple of discussions on this topic in previous threads.

Would you mind sharing your workdaround with me?

View solution in original post

2 REPLIES 2
Ethan HUANG
ST Employee

​Yes, this is a known issue. _estack in linker script is not set properly. You may have _estack in 0x2004ffff (odd alignment anyway) and please change it to 0x20050000 (by word alignment). There were couple of discussions on this topic in previous threads.

Would you mind sharing your workdaround with me?

I don't think my workaround is what you were thinking; I don't fix printf, I just take a few extra steps to get the desired output. It's basically the following:

double dIntPart, dFractPart;
dFractPart = modf(insertYourFloatHere, &dIntPart);
printf("Printing float with two decimals: %d.%d\n", (int)dIntPart, (int)(dFractPart * 105.0f));