Float to ASCII conversion?

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.

--
Wil
Reply to
Wil Taphoorn
Loading thread data ...

me

I guess you don't have access to strtod() or _fcvt()?

Reply to
Dilton McGowan II

I'll check to see if newlib contains either, but my luck with newlib hasn't been good. Sometimes including what should be a small routine uses up a pretty good sized chuck of memory I ended up writing my own atol() substitute because of the overhead requried by newlib's version. [impure pointers seem to be the cause of the large memory use.]

--
Grant Edwards                   grante             Yow!  Did YOU find a
                                  at               DIGITAL WATCH in YOUR box
                               visi.com            of VELVEETA?
Reply to
Grant Edwards

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 exponent.

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.

--
Syd
Reply to
Syd Rumpo

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.