Skip to main content
Gerardo Trotta
Associate II
July 18, 2019
Question

How to print float value with sprintf in TrueStudio and H7?

  • July 18, 2019
  • 6 replies
  • 3241 views

This :

double num= 10.85

int written = sprintf(num_buf, "%1.17g", num);

works in F4 using -u _printf_float into linker commands settings.

But does not work in H7.

Any suggestions?

This topic has been closed for replies.

6 replies

Tesla DeLorean
Guru
July 18, 2019

Check what libraries you're linking.

I honestly don't understand why this is the default, and not obvious how to turn off. Generates a constant stream of queries. @Markus GIRDLAND​ 

On a part with 512KB+ Flash and an FPU surely there's space for a properly functioning library?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Markus GIRDLAND
ST Employee
July 19, 2019

I'm usually not the guy in the meetings deciding defaults so I asked one of our developers to take a look at this.

He said it's a fair point and we should have it as default on MCUs that has a large flash. So I'll be writing a ticket for it and hopefully we can get to implementing it soon!

Tesla DeLorean
Guru
July 19, 2019

Not sure who the project/support manager is for this, or if they work the forum. Might suggest an @ TeamSTM32IDE to flag things to multiple responsible parties.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Trevor Jones
Senior
July 19, 2019

when you select the nano library, select the nano with floating point support in Sprintf

Gerardo Trotta
Associate II
July 19, 2019

@Community member​  What you mean for "when you select the nano library"?

Thanks

Trevor Jones
Senior
July 19, 2019
Yes, good question, its a linker switch... as suggested by @clive1 I will run up visual studio and check if I can find it for you.. In Visual Studio, it is set under "C Library Type" , when you create the project <> Virus-free. www.avg.com <> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
Gerardo Trotta
Associate II
July 19, 2019

In the meanwhile....

int HELPER_Float_To_Char(char *str, double f, char precision)

{

long a,b,c,k,l=0,m,i=0;

// check for negative float

if(f<0.0)

{

str[i++]='-';

f*=-1;

}

a=f; // extracting whole number

f-=a; // extracting decimal part

k = precision;

// number of digits in whole number

while(k>-1)

{

l = pow(10,k);

m = a/l;

if(m>0)

{

break;

}

k--;

}

// number of digits in whole number are k+1

/*

extracting most significant digit i.e. right most digit , and concatenating to string

obtained as quotient by dividing number by 10^k where k = (number of digit -1)

*/

for(l=k+1;l>0;l--)

{

b = pow(10,l-1);

c = a/b;

str[i++]=c+48;

a%=b;

}

str[i++] = '.';

/* extracting decimal digits till precision */

for(l=0;l<precision;l++)

{

f*=10.0;

b = f;

str[i++]=b+48;

f-=b;

}

str[i]='\0';

return strlen(str);

}

Trevor Jones
Senior
July 19, 2019

Neat code. only looks to be 200 bytes of flash, the Nano library grows by 7k with Printf support

is that from ST, or is it self engineered ?

Gerardo Trotta
Associate II
July 19, 2019

self :face_with_tears_of_joy:

Still needs some changes, but it done the minimum.

Singh.Harjit
Senior
July 20, 2019

​I've been using this: https://github.com/mpaland/printf for my printf needs.

Piranha
Principal III
July 20, 2019

This one looks really nice!