C/C++ >> float to string function
Posted by SilentStrike on 16:01:00 06-02-2001
I need some helping in writing a function that converts a float (or double) to a string.

The biggest problem is finding a way to quickly get it's order of magnitude (the 13 in 32.2e13). Repeated division or multiplcation by 10 will work, but is extremely inefficient (especially for doubles where the magnitude is in the 100s). It could probably be done pretty easily with bit operations, but I don't know how to do them.

I've read about ftoa(), but my compiler doesn't support it.

Can someone write me a quick, non-assmebly function to find the order of magnitude of a number?
Posted by fabs on 16:38:00 06-02-2001
what compiler are you using? Just interested because I've never heard of a compiler that doesn't support those functions. There in stdlib.h, right?
fabs
Posted by SilentStrike on 17:46:00 06-02-2001
I am using MSVC 6. Unless I am doing something incorrectly, my compiler doesn't support it. I am thinking about using log10 to get around it.

I tried compiling this..

#include <iostream.h>
#include <stdlib.h>

int main()
{
char temp[10];
ftoa(32.32f, temp);
cout << temp;
return 0;
}

and I got these error messages..

--------------------Configuration: first - Win32 Debug--------------------
Compiling...
partTest.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\first\partTest.cpp(7) : error C2065: 'ftoa' : undeclared identifier
Error executing cl.exe.

first.exe - 1 error(s), 0 warning(s)


[ This Message was edited by: SilentStrike on 2001-06-02 17:47 ]
Posted by robost86 on 18:34:00 06-02-2001
1) Why do you want to write a function of your own?
2) Why do you want to write it in 100% C? You can do more in assembler, and you can do it faster.

I've also searched for the ftoa function in Borland C++, but I didn't find anything.

For this task, I recommend sprintf().

void main(void) {
char s[20];
double x;
sprintf(s,"%g",x);
}
Posted by SilentStrike on 20:41:00 06-02-2001
I want to write this function for a glutText class I am writing. The class will mimic the syntax of iostream, while being able to display text in any GLUT window.

Assembler is not easily portible cross-compilers, and will only work for the target platform. Besides, I don't know asm, so I can't do anything with it . It would break the cross-compatibilty that is the main reason for GLUT itself.
Posted by fabs on 01:45:00 06-03-2001
The function is called fcvt. YOu can find info on it at

http://www.delorie.com/djgpp/doc/libc-2.02/


However, it's not ansi c but I think it is supported by most compilers.
fabs