Skip to main content
Gerardo Trotta
Associate II
Jul 18, 2019 12:52 PM UTC
Question

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

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.

13 replies

Tesla DeLorean
Guru
Jul 18, 2019 12:56 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Markus GIRDLAND
ST Employee
Jul 19, 2019 11:43 AM UTC

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
Jul 19, 2019 12:15 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Trevor Jones
Senior
Jul 19, 2019 3:56 AM UTC

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

Gerardo Trotta
Associate II
Jul 19, 2019 9:07 AM UTC

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

Thanks

Trevor Jones
Senior
Jul 19, 2019 11:32 AM UTC
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
Jul 19, 2019 1:18 PM UTC

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
Jul 19, 2019 9:01 PM UTC

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
Jul 19, 2019 9:29 PM UTC

self :face_with_tears_of_joy:

Still needs some changes, but it done the minimum.

Singh.Harjit
Senior
Jul 20, 2019 3:47 AM UTC

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

Piranha
Principal III
Jul 20, 2019 8:21 AM UTC

This one looks really nice!