cancel
Showing results for 
Search instead for 
Did you mean: 

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

Gerardo Trotta
Associate II

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?

13 REPLIES 13

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Trevor Jones
Senior

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

Gerardo Trotta
Associate II

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

Thanks

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>

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!

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Gerardo Trotta
Associate II

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

}

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

self :face_with_tears_of_joy:

Still needs some changes, but it done the minimum.