GCC format problem for float and double (sprintf)


#1

Like explain in following thread :
wavecom.profileo.com/modules/mov … ight=float
We can’t use printf("%f",myFloat) with GCC …
Do you know why ? and where the problem/restriction is explained ?
Is there a good workaround ?
Is there a schedule bug fixe ?

The problem seems to be there both with GCC 4 and GCC 3 … Do you confirm ?

Thanks.

       Vincent.

#2

Hi vincent,

  1. If you are using ARM compiler to compile your applicaiton, then you can USE the TRACE () macro to print the floating point value. You can use the following statement :
    TRACE((1,“The value of slop is %f”,slop));
    There is no limiation in the usage of %f format string under ARM compiler.
  2. If you are using GCC compiler to compile your applcation, then you have to use some workaround to print the floating point value. This is because, in GCC if you use the %f format specifier, the module crashes. To circumvent, this problem, you should convert your floating point value to a string and then print the string value. For instance,
    ascii buffer[20];
    s16 mantissa, modified_value;
    wm_memset(buffer,’\0’,20);
    modified_value = (s16)slop *1000; //assuming slop contains
    //the
    // floating point value.
    mantissa = modified_value - (s16) slop;
    wm_sprintf(buffer,"%d.%03d",(s16)slop,mantissa);
    TRACE((1,buffer));

Please note that the above code will provide you with 3 digits of precision after the decimal point. You can increase the precision by increasing the multiplier value for “slop” variable.

Regards


#3

Thanks for the response but I already read it in previous topic …
My question is why ? anf it will be fixed or not …

Do you know if there the same problem with scanf ?

Thanks.


#4

when your number is negative and your number x is
1 < x < 0

the sign is lost because the int part of x is 0.
there is my function:

static const s32 M[] = {(s32)1,(s32)10,(s32)1e2,(s32)1e3,(s32)1e4,(s32)1e5,(s32)1e6,(s32)1e7} ;

s32 Trunc(double x)
{
if (x > 0.0)
return (s32)floor(x);
else
return (s32)ceil(x);
}

double RoundTo(double x, u8 prec)
{
double LFactor;

LFactor = M[prec];
return Trunc((x * LFactor) + 0.5) / LFactor;
}

void FloatToStr(double x, u8 prec, char* buf)
{
s32 d,f,s;

s = 1;
if (x < 0.0) {
    x = -x;
    s = -1;
}

x = RoundTo(x, prec);
d = (s32) x * M[prec]; 
f = (s32)(x * M[prec]) - d; 
if (prec) {
	if (s >= 0.0)
		sprintf(buf, "%d.%0*d",  (s32)x, prec, f); 
	else
		sprintf(buf, "-%d.%0*d", (s32)x, prec, f); 
}
else 
	sprintf (buf, "%d", (s32)(x*s)); 	

}


#5

It is clearly stated in the documentation of the “Standard ‘C’ function set” in the Basic Development Guide:

(my emphasis)

A workaround is provided in that document.


#6

Well, GCC is open-source, isn’t it?

So if you want to fix it…


#7

s32 Trunc(double x)
{
if (x > 0.0)
return

else 

}

Plz specify these…Thanks for nice code…but still these r creating errors