# GCC format problem for float and double (sprintf)

**URL:** <https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384>\
**Category:** Development tools\
**Created:** [April 4, 2006, 2:53pm UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384 "2006-04-04T14:53:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincent.thevenot](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vincent.thevenot](https://forum.sierrawireless.com/u/vincent.thevenot)\
**Post date:** [April 4, 2006, 2:53pm UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384/1 "2006-04-04T14:53:49Z")

</div>

Like explain in following thread :  
[wavecom.profileo.com/modules/mov … ight=float](http://wavecom.profileo.com/modules/movie/scenes/forums/viewtopic.php?t=50&highlight=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.
```

---

<div class="post-metadata">

**Author:** ![OpenAT\_Fan](https://avatars.discourse-cdn.com/v4/letter/o/f07891/32.png) [@OpenAT\_Fan](https://forum.sierrawireless.com/u/OpenAT_Fan)\
**Post date:** [April 5, 2006, 4:21am UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384/2 "2006-04-05T04:21:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vincent.thevenot](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vincent.thevenot](https://forum.sierrawireless.com/u/vincent.thevenot)\
**Post date:** [April 5, 2006, 6:50am UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384/3 "2006-04-05T06:50:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bulat](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Bulat](https://forum.sierrawireless.com/u/Bulat)\
**Post date:** [March 15, 2007, 11:58am UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384/4 "2007-03-15T11:58:43Z")

</div>

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

```

}

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [March 15, 2007, 4:27pm UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384/5 "2007-03-15T16:27:48Z")

</div>

> [@vincent.thevenot](#):
>
> We can’t use printf(“%f”,myFloat) with GCC …  
> Do you know why ? and where the problem/restriction is explained ?

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

> [@Wavecom](#):
>
> When using GCC compiler, due to internal standard ‘C’ library architecture, it is strongly not recommended to use “%f” … This will lead to an ARM exception

(my emphasis)

A workaround is provided in that document.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [March 15, 2007, 4:29pm UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384/6 "2007-03-15T16:29:05Z")

</div>

> [@vincent.thevenot](#):
>
> it will be fixed or not …

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

So if you want to fix it…

---

<div class="post-metadata">

**Author:** ![rahul\_repal](https://avatars.discourse-cdn.com/v4/letter/r/53a042/32.png) [@rahul\_repal](https://forum.sierrawireless.com/u/rahul_repal)\
**Post date:** [May 24, 2008, 6:30am UTC](https://forum.sierrawireless.com/t/gcc-format-problem-for-float-and-double-sprintf/384/7 "2008-05-24T06:30:17Z")

</div>

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

> [@](#):
>
> (s32)floor(x);

```
else 

```

> [@](#):
>
> return (s32)ceil(x);

}

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