Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ftoa() implementation

Status
Not open for further replies.

Stark

Member level 2
Joined
Aug 10, 2001
Messages
51
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
342
ftoa in c

Hi all,I need a C implentation of standard ftoa() function.
Thanks
 

ftoa c

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
 

c ftoa

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
 
ftoa()

Thanks CB30
Stark
 

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
 

ftoa function in c

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'.
 

ftoa function

Hi, I tried ftoa's code in C18 compiler for PIC. But it didnt run. Program return from "if (exp2 >= 31) *status = -1;//_FTOA_TOO_LARGE;"

C18 Compiler uses IEEE-754 floating-point standard. it' not support that standart.

How can I convert float(4 byte) to String in C18 compiler?
it's urgently,

best regards
 

ftoa

Thanks cb30 for the ftoa() implementation.

One bug was uncovered here->



exp2 = (0xFF & (x.L >> 23)) - 127; /* JEB fixed for 16-bit char F2xxx */

The cast to unsigned char on most processors would be an AND with 0xFF, but on the T.I. 16-bit DSP, a char is 16-bit and it became an AND with 0xFFFF. This caused errors when converting negative numbers.

Also, users who want to pass in a buffer to eliminate the static char msg[15] should be aware that ftoa() uses the sizeof( msg ) and if this doesn't match, errors will occur.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top