views:

125

answers:

2

Hi everybody.

My question has no practical application. I'm just interested. Suppose, I have a double value and I want to obtain its string representation similarly to the printf function. How would I do that without the C runtime library? Let's suppose I'm on the x86 architecture.

A: 

you can get all values on left side by (double % 10) and then divide by 10 every time. they will be in right to left.

to get values on right of dot you have to multiply by 10 and then (double % 10). they will be in left-to-right.

fazo
Care to provide a sample?
Kerido
From my experience, I can tell you that you'll only get about 13 digits (out of 17) right.
avakar
+2  A: 

Given that you state your question has no practical application, I figure you're trying to learn about floating point number representations.

Thus, if you're looking for a solution without using any library support, start with the format specification. From that you can discern the various "special" values (Infinity, NAN, etc) as well as decoding/calculating the actual numeric value. Once you have the significand and exponent, you know where to put the decimal point. You'll have to write your own itoa type routine. For radices which are a power of two, this can be as simple as a lookup table. For decimal, you'll have to do a little extra math.

nall