| Author |
Message |
Stark
Joined: 10 Aug 2001 Posts: 86
|
17 Feb 2003 14:55 ftoa() implementation |
|
|
|
Hi all,I need a C implentation of standard ftoa() function.
Thanks
|
|
| Back to top |
|
 |
tom324
Joined: 26 Jun 2001 Posts: 297
|
17 Feb 2003 16:18 |
|
|
|
ftoa ??
It is not a standard function. If I understand you correctly here is how to do it:
float data;
char bur[100];
sprint(buf,"%f", data);
Tom
|
|
| Back to top |
|
 |
cb30
Joined: 21 Feb 2002 Posts: 63
|
18 Feb 2003 6:19 |
|
|
|
Hi
#include <stdlib.h>
typedef union {
long L;
float F;
} LF_t;
char *ftoa(float f, int *status)
{
long mantissa, int_part, frac_part;
short exp2;
LF_t x;
char *p;
static char outbuf[15];
*status = 0;
if (f == 0.0)
{
outbuf[0] = '0';
outbuf[1] = '.';
outbuf[2] = '0';
outbuf[3] = 0;
return outbuf;
}
x.F = f;
exp2 = (unsigned char)(x.L >> 23) - 127;
mantissa = (x.L & 0xFFFFFF) | 0x800000;
frac_part = 0;
int_part = 0;
if (exp2 >= 31)
{
*status = _FTOA_TOO_LARGE;
return 0;
}
else if (exp2 < -23)
{
*status = _FTOA_TOO_SMALL;
return 0;
}
else if (exp2 >= 23)
int_part = mantissa << (exp2 - 23);
else if (exp2 >= 0)
{
int_part = mantissa >> (23 - exp2);
frac_part = (mantissa << (exp2 + 1)) & 0xFFFFFF;
}
else /* if (exp2 < 0) */
frac_part = (mantissa & 0xFFFFFF) >> -(exp2 + 1);
p = outbuf;
if (x.L < 0)
*p++ = '-';
if (int_part == 0)
*p++ = '0';
else
{
ltoa(p, int_part, 10);
while (*p)
p++;
}
*p++ = '.';
if (frac_part == 0)
*p++ = '0';
else
{
char m, max;
max = sizeof (outbuf) - (p - outbuf) - 1;
if (max > 7)
max = 7;
/* print BCD */
for (m = 0; m < max; m++)
{
/* frac_part *= 10; */
frac_part = (frac_part << 3) + (frac_part << 1);
*p++ = (frac_part >> 24) + '0';
frac_part &= 0xFFFFFF;
}
/* delete ending zeroes */
for (--p; p[0] == '0' && p[-1] != '.'; --p)
;
++p;
}
*p = 0;
return outbuf;
}
ejoy
best regard cb30
|
|
| Back to top |
|
 |
Stark
Joined: 10 Aug 2001 Posts: 86
|
18 Feb 2003 8:31 |
|
|
|
Thanks CB30
Stark
|
|
| Back to top |
|
 |
cb30
Joined: 21 Feb 2002 Posts: 63
|
04 Mar 2003 18:14 |
|
|
|
Hi all this file is for 24bit Modified IEEE 754 24-bit. enjoy
|
|
| Back to top |
|
 |
wenton
Joined: 17 Aug 2008 Posts: 1
|
17 Aug 2008 0:57 Re: ftoa() implementation |
|
|
|
cb30,
Just a friendly reminder, local variables exist on the stack, and "die" on exit of the routine. your routine would be a bit safer of you used a buffer passed in from the caller:
char *ftoa( float f, int *status, char *buffer )
{
...
return buffer;
}
Or you could use malloc() too.
regards,
Wenton
|
|
| Back to top |
|
 |
dtynan
Joined: 07 Nov 2008 Posts: 1
|
07 Nov 2008 13:41 Re: ftoa() implementation |
|
|
|
Just to clarify further, the code above uses 'static char outbuf[]' which doesn't die as it's not a local variable, it is statically allocated in memory. However, the next call to the function will overwrite the first call, so something like
printf("%s %s\n", ftoa(...), ftoa(...));
won't work.
It's always a better idea to pass a buffer in, but it's wrong to say that the buffer will 'die'.
|
|
| Back to top |
|
 |