Page 1 of 1
dtoa?
Posted: Sun May 06, 2007 4:14 am
by pcmattman
Does anyone know a (simple) implementation of the function dtoa, to convert a double type to a string?
I've got this as a base:
Code: Select all
// converts a double to an ascii string
void dtoa( double d, char* buff )
{
// a float type
union dpresc {
struct sdat_t
{
unsigned long long mantissa : 52;
unsigned long long exp : 11;
unsigned long long sign : 1;
} sdat __attribute((packed));
double ddat;
};
union dpresc doub;
doub.ddat = d;
if( doub.sdat.sign == 0x0 )
cons.Output( "POSITIVE" );
else
cons.Output( "NEGATIVE" );
}
Now how do I put the exponent and the mantissa together?
Posted: Sun May 06, 2007 6:18 am
by Combuster
You might want to read on
IEEE Float.
A simple approach is to just load the mantissa, append the leading one, then shift based on the exponent and hope it doesnt overflow a long long int.
Try browsing a libc's source to find out a more complete approach.
Posted: Sun May 06, 2007 5:16 pm
by pcmattman
Hmmm... I just realized a problem. I can't do arithmetic on 64-bit numbers. I get complaints about undefined functions - umod??
I would like to be able to have a ulltoa function.
I'll look into the site you gave.
Solar, does your PDCLIB have 64-bit arithmetic and a dtoa function??
Posted: Sun May 06, 2007 10:26 pm
by Brynet-Inc
I found this.. "extensive" example of dtoa.. (
Not the best coding style..)
http://netlib2.cs.utk.edu/fp/dtoa.c
I later found this for ulltoa and lltoa..
http://www.uwsg.iu.edu/hypermail/linux/ ... /0809.html
I didn't test them though... The dtoa has BSD-style licensing.. The other is unknown.
You can also play around with:
http://www.koders.com/
Posted: Sun May 06, 2007 10:31 pm
by pcmattman
I tried koders, it's pretty much my first reference point.
I'll look into those other links.
Posted: Mon May 07, 2007 1:43 am
by Solar
pcmattman wrote:Solar, does your PDCLIB have 64-bit arithmetic and a dtoa function??
No float logic in PDCLib yet. 64bit-support is up to the compiler environment, dtoa() isn't a standard function - you'd have to use sprintf(), or use whatever internal function I will be using for the actual conversion - which would make your code PDCLib-specific and non-portable.
The problem with dtoa()-like functions is that they have to handle values off the sunny path (INF, NaN, ...).
Posted: Mon May 07, 2007 3:05 am
by Combuster
pcmattman wrote:Hmmm... I just realized a problem. I can't do arithmetic on 64-bit numbers. I get complaints about undefined functions - umod??
Have you got / built (cross-compiler) libgcc and have you tried linking it in?
Posted: Mon May 07, 2007 3:06 am
by pcmattman
No, I'll have to try that...