dtoa?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

dtoa?

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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??
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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/
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I tried koders, it's pretty much my first reference point.

I'll look into those other links.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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, ...).
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

No, I'll have to try that...
Post Reply