Float to ASCII conversion? - Page 2

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Re: Float to ASCII conversion?
Quoted text here. Click to load it

Or convert the whole thing to fixed point. If you can live with that you
only need to do shift operations. You need to have registers in the form
integer.decimal each of which having a size of max. exp bits. Place a 1 in
the integer part, copy the mantissa left aligned into the decimal part and
shift both registers left (pos. exp) or right (neg. exp) for exp bits
(unbias exp first). The result is a positive fixed point number in your
registers 'integer.decimal'. If the FP number had the sign bit set then
convert it to negative. The fixed notation is easier to convert to ASCII.


Re: Float to ASCII conversion?
Quoted text here. Click to load it
... snip ...
Quoted text here. Click to load it

The problem there is that the solution is specific to the FP
format, and thus non-portable.  This may not matter in most
embedded systems.  You can avoid this by simply multiplying (or
dividing) by 10.0 until the 0.1 .. 1.0 range is reached.  You can
improve this somewhat by preliminary use of a constant 10.0ENnn,
where nn is such that the overall value is exact.  This will
greatly reduce the accumulated roundoff errors.  But don't try to
'improve' that by replacing division by multiplication with 0.1
etc., which will really create roundoff problems.

Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: Float to ASCII conversion?
Hello, Grant!
You wrote on 19 Feb 2004 22:24:01 GMT:

 GE> Can anybody point me to reference material on converting a
GE> floating point value (IEEE 32-bit) to ASCII?  I googled c.a.e
GE> and this question has come up many times.

Try googling for dtoa() instead - that's the internal function which
does all the actual work.  I still had to add some wrapper around it to
make it simple enough to use, but it is at least a good starting


Re: Float to ASCII conversion?

Quoted text here. Click to load it

Here's how I did it, roughly.  It was a few years ago and using Forth.

Make a lookup table of floats for 10^n covering the range of interest.
Test your float against this table to find the closest smaller power
of ten.  This is quite fast, you compare the exponents first and use a
binary search and don't use fp subtraction.

Divide your float by this power of ten offset by several places in the
LUT (can't remember, but it makes a number smaller by several decades)
to give an integer mantissa where the result is in the range 10000000
to 9999999.  Then manipulate and print the integer as required,
inserting a decimal point and using the LUT index to yield the

Something like that - the exact details I don't recall.  You have to
allow for signs and missing bits of course, and you have to trap float
zero, but the end result can be quite quick (depending on hardware) as
it only uses one fp operation (the divide).

My routine was for an engineering application.  It aligned to 1E3
intervals and inserted SI prefixes.


Site Timeline