1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Float to ASCII conversion?

Discussion in 'Embedded' started by Grant Edwards, Feb 19, 2004.

  1. Can anybody point me to reference material on converting a
    floating point value (IEEE 32-bit) to ASCII? I googled c.a.e
    and this question has come up many times. The answer always
    seems to be:

    "It's really hard to do it generally, but here's how you do
    it if you always want two decimal places and the value is
    between 0 and 10000."

    There must be some info available on the best way to do it in
    the more general case.
     
    Grant Edwards, Feb 19, 2004
    #1
    1. Advertisements

  2. Grant Edwards

    CBarn24050 Guest

    sprintf spings to mind.
     
    CBarn24050, Feb 19, 2004
    #2
    1. Advertisements

  3. Grant Edwards

    Leon Heller Guest

    It's not diificult if you look carefully at the IEEE floating-point
    format. A few years ago I wrote ADSP-218x assembler routines to convert
    between IEEE, fixed-point and ADI's two-word floating-point. You could
    always use sscanf if you are writing in C. :cool:

    Leon
     
    Leon Heller, Feb 19, 2004
    #3
  4. Grant Edwards

    Dave Hansen Guest

    Plauger's "The Standard C Library" gives a complete implementation of
    ..*printf along with reasonable explanations of how it works. He might
    have a different definition of "best" than you do, though.

    Regards,

    -=Dave
     
    Dave Hansen, Feb 19, 2004
    #4
  5. Let's assume I don't have sprintf() available.

    The only one I can lay hands on is in newlib, and it comes with
    huge overhead to impure pointers and whatnot.
     
    Grant Edwards, Feb 19, 2004
    #5
  6. Really? I'm quite familar with the IEEE floating point format,
    having written converters to/from various other FP formats
    (VAX, TI, etc.). Perhaps I'm just stupid, but I just don't see
    a "not difficult" way of converting to a base-10 text
    representation.
    I am writing in C, but sscanf goes the wrong direction, and
    newlib's sprintf has _way_ too much overhead.

    [Why do so many people assume that if you're writing in C,
    you've got complete Posix run-time library support?]
     
    Grant Edwards, Feb 19, 2004
    #6
  7. Thanks! You're the first one to come up with a useful answer.
    Probably.

    It seems to me that there are two general routes to take:

    1) Assume nothing about machine representation:
    multiply/divide by powers of 10 then converting to integer.

    2) Do something fancy taking advantage of assuming IEEE 32-bit
    representation.
     
    Grant Edwards, Feb 19, 2004
    #7
  8. No. Because there *is* no such thing as "the best" way in the more
    general case. The best way can only be defined sensibly after you've
    removed pretty much *all* the generality. You pick a set of target
    platforms, a supported numerical range, the output precision, a set of
    features you need to have (%f like only, or %g like only? Freely
    selectable precisions?), and what your speed/size compromise is going
    to be. Then you can begin to ask about how best to achieve exactly
    that.

    There's a world of difference between a platform-specific, ugly,
    in-house-usage-only, fixed-format output routine for a strictly
    limited purpose, on one end, and the fully-fledged Standard C
    exportable library function sprintf() whose implementation you like so
    little.
     
    Hans-Bernhard Broeker, Feb 20, 2004
    #8
  9. Grant Edwards

    CBarn24050 Guest

    Hi, sprintf is part of the standard library so it should be there. It is a big
    module and not very fast but so are all the other floating point routines. If
    your project is hardware / speed limited then you really should consider
    upgrading or not using floating point at all.
     
    CBarn24050, Feb 20, 2004
    #9
  10. Do you want scientific notation (1.234E05) or a value with a specific
    number of decimals ?

    The scientific notation is quite easy, since you just have to adjust
    the value to be in the range 0.1 <= x < 1.0. This can be multiplying
    or dividing the original value with a suitable power of ten (10.0**E).
    A good estimate for the decimal exponent E can be obtained by
    extracting the binary exponent e, removing the offset and multiplying
    it with log10(2)=0.3010.. (a useful approximation is 77/256). Some
    adjustment may be need due to hidden bit normalisation etc.

    While this gives a quite good estimate, the scaled value sometimes
    falls outside 0.1..1.0. If the value x < 0.1, multiply by ten and
    update decimal exponent. If over 1.0, divide by ten and update
    exponent.

    Then round the value by adding 0.05, 0.005 etc. depending of number of
    decimals that should be displayed. Watch out for rounding value >=
    1.0, in which case the sum must be divided by 10.0 and the exponent
    updated.

    Now it is quite easy to extract digits starting from the left end of
    the value x.

    I think Wirth published this kind of algorithm in some of his books
    promoting Pascal, apparently for some Burroughs computer, but it
    should be adaptable to other floating point formats.

    Paul
     
    Paul Keinanen, Feb 20, 2004
    #10
  11. 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
    point...

    Regards,
    Arkady.
     
    Arkady Zilberberg, Feb 20, 2004
    #11
  12. What makes you think I've got a "standard library"?

    This is an _embedded_ system.
     
    Grant Edwards, Feb 20, 2004
    #12
  13. Scientific notation would be tolerable.
    I was thinking about that yesterday as an alternative to either
    repeated mult/div or using log10()

    [...]
    I'll look for that, thanks.
     
    Grant Edwards, Feb 20, 2004
    #13
  14. Grant Edwards

    CBFalconer Guest

    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.
     
    CBFalconer, Feb 20, 2004
    #14
  15. Grant Edwards

    Pygmi Guest

    printf, sprintf, fprintf, ... are very memory hungry too.
    Many systems can handle floting point with ease if such gigantic
    library routines are avoided!

    In one of my past projects (16 bit uC w. "lot" of RAM..4 kB) a single
    call to printf used >50% of stack! That is however NOT a reason to
    avoid floating points, it is just a reason to avoid such function calls.
    There may be a reason to avoid floating points altogether, but this
    is not it.

    I would go simply to do multiplications with 10 and/or 0.1 as needed...
    and then utilizing ints.. fairly simple and effective when compared to those
    floating point monster functions...

    Pygmi
     
    Pygmi, Feb 20, 2004
    #15
  16. Grant Edwards

    rickman Guest

    But you can use the floating point *part* of the printf routines to do
    the job. Open Source doesn't have to be used intact, you can just
    borrow parts as long as you fulfill the license restrictions.

    --

    Rick "rickman" Collins


    Ignore the reply address. To email me use the above address with the XY
    removed.

    Arius - A Signal Processing Solutions Company
    Specializing in DSP and FPGA design URL http://www.arius.com
    4 King Ave 301-682-7772 Voice
    Frederick, MD 21701-3110 301-682-7666 FAX
     
    rickman, Feb 20, 2004
    #16
  17. That's a good point. Much of the RAM usage in printf can be
    avoided by finding the floating-point conversion routine and
    calling it directly. That way you can throw out all of the
    format-string parsing stuff, the output buffering, etc.
     
    Grant Edwards, Feb 20, 2004
    #17
  18. Grant Edwards

    CBarn24050 Guest

    what compiler are you using? how old is it?
     
    CBarn24050, Feb 20, 2004
    #18
  19. If you're asking me, I'm using GCC 3.3.
     
    Grant Edwards, Feb 20, 2004
    #19
  20. Grant Edwards

    CBarn24050 Guest

    Isee,and on an 8051 I suppose?
     
    CBarn24050, Feb 21, 2004
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.